aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/I18NStringDatafield.php
blob: 60078722981bd88230901f66a1f36b718abe693a (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
<?php
/**
 * I18NStringDatafield.php
 * Class to handle i18n content of datafields.
 *
 * 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
 *
 */

class I18NStringDatafield extends I18NString
{

    /**
     * Sets the metadata (database info object_id) of this i18n datafield.
     *
     * @param array $metadata Database info for object_id.
     */
    public function setMetadata($metadata)
    {
        if (is_null($metadata['table'])) {
            $this->metadata = $metadata;
        }
    }

    /**
     * Return an array containg the text in all additional languages.
     *
     * @return array
     */
    public function toArray()
    {
        if (is_null($this->lang)) {
            $object_id = $this->metadata['object_id'];
            $this->lang = self::fetchDataForField($object_id, null, null);
        }
        return $this->lang;
    }

    /**
     * Stores the i18n String manually in the database
     *
     */
    public function storeTranslations()
    {
        if (is_array($this->lang)) {
            $object_id = $this->metadata['object_id'];
            /* Replace translations */
            DatafieldEntryModel::deleteBySQL(
                "`datafield_id` = ? AND `range_id` = ? AND `sec_range_id` = ? AND `lang` <> ''",
                [$object_id[0], $object_id[1], $object_id[2]]
            );
            foreach ($this->lang as $lang => $value) {
                if (strlen($value)) {
                    DatafieldEntryModel::create(
                        [
                            'datafield_id' => $object_id[0],
                            'range_id' => $object_id[1],
                            'sec_range_id' => $object_id[2],
                            'content' => (string) $value,
                            'lang' => (string) $lang,
                        ]
                    );
                }
            }
        }
    }

    /**
     * Returns an I18NString object by given object_id, table and field.
     *
     * @param string $object_id The id of the object with i18n fields.
     * @param string $table The name of the table with the original values.
     * @param string $field The name of the i18n field.
     * @param string $base Sets the original value or retrieve it from database
     * if null.
     * @return I18NString The I18NString object.
     */
    public static function load($object_id, $table = '', $field = '', $base = null)
    {
        if ($base === null) {
            $df = DatafieldEntryModel::findOneBySQL(
                "`datafield_id` = ? AND `range_id` = ? AND `sec_range_id` = ? AND `lang` = ''",
                [$object_id[0], $object_id[1], $object_id[2]]
            );
            $base = $df ? $df->content : '';
        }
        $table = null;
        $field = null;
        return new self(
            $base,
            self::fetchDataForField($object_id, $table, $field),
            compact('object_id', 'table', 'field')
        );
    }

    /**
     * Retrieves all translations for one field.
     *
     * @param string $object_id The id of the object with i18n fields.
     * @param string $table The name of the table with the original values.
     * @param string $field The name oof the i18n field.
     * @return array An array with language as key and translation as value.
     */
    public static function fetchDataForField($object_id, $table, $field)
    {
        $result = [];

        DatafieldEntryModel::findEachBySQL(
            function (DatafieldEntryModel $model) use (&$result) {
                $result[$model->lang] = $model->content;
           },
            "`datafield_id` = ? AND `range_id` = ? AND `sec_range_id` = ? AND `lang` <> ''",
            [$object_id[0], $object_id[1], $object_id[2]]
        );

        return $result;
    }

    /**
     * This function is not used in the context of datafields, so it always
     * returns an empty array.
     *
     * @param string $object_id The id of the object with i18n fields.
     * @param string $table The name of the table with the original values.
     * @return array An empty array.
     */
    public static function fetchDataForRow($object_id, $table)
    {
        return [];
    }

    /**
     * Removes all translations by given object id and table name. Accepts the
     * language as third parameter to remove only translations to this language.
     *
     * @param string $object_id The id of the sorm object.
     * @param string $table The table name.
     * @param string $lang Optional name of language.
     * @return int The number of deleted translations.
     */
    public static function removeAllTranslations($object_id, $table, $lang = null)
    {
        $db = DBManager::get();
        if ($lang) {
            return $db->execute('DELETE FROM `datafield_entries` '
                    . 'WHERE `datafield_id` = ? '
                    . 'AND `range_id` = ? '
                    . 'AND `sec_range_id` = ? '
                    . 'AND `lang` = ?',
                    [$object_id[0], $object_id[1], $object_id[2], $lang]);
        }
        return $db->execute('DELETE FROM `datafield_entries` '
                . 'WHERE `datafield_id` = ? '
                . 'AND `range_id` = ? '
                . 'AND `sec_range_id` = ? '
                . 'AND `table` = ?',
                $object_id);
    }

}