aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/DataFieldI18NEntry.php
blob: 35dc234dc4e3afca58a04974983adfd49cc432a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
 * DataFieldI18NEntry.php
 * Provides functionality for datafields with i18n support.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Peter Thienel <thienel@data-quest.de>
 * @copyright   2017 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.1
 *
 */
abstract class DataFieldI18NEntry extends DataFieldEntry
{

    /**
     * Constructs this datafield
     *
     * @param DataField $datafield Underlying model
     * @param mixed $range_id Range id (or array with range id and secondary
     *                        range id)
     * @param mixed     $value     Value
     */
    public function __construct(?DataField $datafield = null, $rangeID = '', $value = null)
    {
        $this->model = $datafield;

        if (is_array($rangeID)) {
            $object_id = [$datafield->id, $rangeID[0], $rangeID[1]];
        } else {
            $object_id = [$datafield->id, $rangeID, ''];
        }
        $value = I18NStringDatafield::load($object_id, null, null);

        $this->rangeID = $rangeID;
        $this->value   = isset($value) ? $value : $datafield->default_value;
    }

    /**
     * Sets the prefered content language if this is an i18n datafield.
     *
     * @param string $language The prefered display language
     */
    public function setContentLanguage($language)
    {
        $languages = array_keys(Config::get()->CONTENT_LANGUAGES);
        if ($language && $language == reset($languages)) {
            $language = '';
        }
        if ($language && !Config::get()->CONTENT_LANGUAGES[$language]) {
            throw new InvalidArgumentException('Language not configured.');
        }

        $this->language = $language;
    }

    /**
     * Returns the display/rendered value of this datafield
     *
     * @param bool $entities Should html entities be encoded (defaults to true)
     * @return String containg the rendered value
     */
    public function getDisplayValue($entities = true)
    {
        if ($entities) {
            return htmlReady((string) $this->getValue(), true, true);
        }

        return (string) $this->getValue();
    }

    /**
     * Returns the input elements as html for this datafield
     *
     * @param String $name      Name prefix of the associated input
     * @param Array  $variables Additional variables
     * @return String containing the required html
     */
    public function getHTML($name = '', $variables = [])
    {
        return parent::getHTML($name, $variables + [
            'locale_names' => $this->getLocaleNames($name)
        ]);
    }

    /**
     * Sets the value from a post request
     *
     * @param mixed $submitted_value The value from request
     */
    public function setValueFromSubmit($submitted_value)
    {
        $metadata = [
            'object_id' => [
                $this->model->id,
                (string) $this->getRangeID(),
                (string) $this->getSecondRangeID()
            ],
            'table' => null,
            'field' => null
        ];
        $translations = $submitted_value;
        $base = $submitted_value['base'];
        unset($translations['base']);
        $i18n_entry = new I18NStringDatafield($base);
        $i18n_entry->setMetadata($metadata);
        $i18n_entry->setTranslations($translations);
        $i18n_entry->setOriginal($base);
        parent::setValueFromSubmit($i18n_entry);
    }

    /**
     * Stores this datafield entry
     *
     * @return int representing the number of changed entries
     */
    public function store()
    {
        $id = [
            $this->model->id,
            (string) $this->getRangeID(),
            (string) $this->getSecondRangeID(),
            ''
        ];
        $entry = new DatafieldEntryModelI18N($id);

        $old_value = I18NStringDatafield::load([$entry->datafield_id,
            $entry->range_id, $entry->sec_range_id], null, null);
        $entry->content = $this->getValue();
        if ($entry->content->original() == $this->model->default_value
                && count($entry->content->toArray()) == 0) {
            $result = $entry->delete();
        } else {
            $result = $entry->store();
        }

        if ($result) {
            NotificationCenter::postNotification('DatafieldDidUpdate', $this, [
                'changed'   => $result,
                'old_value' => $old_value,
            ]);
        }

        return $result;
    }

    /**
     * Returns an array containing the names for the html element by locale.
     *
     * @param string $name Base name of the element
     * @return array
     */
    protected function getLocaleNames($name)
    {
        $locale_names = [];
        foreach (array_keys($GLOBALS['CONTENT_LANGUAGES']) as $index => $locale) {
            $locale_names[$locale] = sprintf(
                '%s[%s][%s]',
                $name,
                $this->model->id,
                $index ? $locale : 'base'
            );
        }
        return $locale_names;
    }
}