Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Responsible for building field definitions and retrieving them as needed.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local\data_grid\field;
26
 
27
use block_dash\local\dash_framework\structure\field_interface;
28
use block_dash\local\data_grid\field\attribute\field_attribute_interface;
29
 
30
/**
31
 * Responsible for building field definitions and retrieving them as needed.
32
 *
33
 * @package block_dash
34
 */
35
class field_definition_factory implements field_definition_factory_interface {
36
 
37
    /**
38
     * Cache registered field definitions so they are only retrieved once.
39
     *
40
     * @var array
41
     */
42
    private static $fielddefintionregistry;
43
 
44
    /**
45
     * @var field_definition_interface[]
46
     */
47
    private static $fielddefinitions;
48
 
49
    /**
50
     * Register and return data registry.
51
     *
52
     * @return array
53
     */
54
    protected static function get_field_definition_registry() {
55
        if (is_null(self::$fielddefintionregistry)) {
56
            self::$fielddefintionregistry = [];
57
            if ($pluginsfunction = get_plugins_with_function('register_field_definitions')) {
58
                foreach ($pluginsfunction as $plugintype => $plugins) {
59
                    foreach ($plugins as $pluginfunction) {
60
                        foreach ($pluginfunction() as $fielddefinition) {
61
                            self::$fielddefintionregistry[$fielddefinition['name']] = $fielddefinition;
62
                        }
63
                    }
64
                }
65
            }
66
        }
67
 
68
        return self::$fielddefintionregistry;
69
    }
70
 
71
    /**
72
     * Returns all registered field definitions.
73
     *
74
     * @return field_definition_interface[]
75
     * @throws \coding_exception
76
     */
77
    public static function get_all_field_definitions() {
78
        if (is_null(self::$fielddefinitions)) {
79
            self::$fielddefinitions = [];
80
            foreach (self::get_field_definition_registry() as $info) {
81
                if (!isset($info['name'])) {
82
                    throw new \coding_exception('Standard SQL fields need a name defined.');
83
                }
84
                self::$fielddefinitions[$info['name']] = self::build_field_definition($info['name'], $info);
85
            }
86
        }
87
 
88
        return self::$fielddefinitions;
89
    }
90
 
91
    /**
92
     * Check if field definition exists.
93
     *
94
     * @param string $name
95
     * @return bool
96
     */
97
    public static function exists($name) {
98
        return isset(self::get_field_definition_registry()[$name]);
99
    }
100
 
101
    /**
102
     * Get field definition info.
103
     *
104
     * @param string $name
105
     * @return array|null
106
     */
107
    public static function get_field_definition_info($name) {
108
        if (self::exists($name)) {
109
            return self::get_field_definition_registry()[$name];
110
        }
111
 
112
        return null;
113
    }
114
 
115
    /**
116
     * Build field definition.
117
     *
118
     * @param string $name
119
     * @param array $info
120
     * @return field_definition_interface
121
     * @throws \coding_exception
122
     */
123
    public static function build_field_definition($name, array $info) {
124
        global $CFG;
125
 
126
        if (!self::exists($name)) {
127
            return null;
128
        }
129
 
130
        $fielddefinitioninfo = self::get_field_definition_info($name);
131
 
132
        if (isset($fielddefinitioninfo['factory']) && $fielddefinitioninfo['factory'] != self::class) {
133
            return $fielddefinitioninfo['factory']::build_field_definition($name, $info);
134
        }
135
 
136
        // Check for db driver specific select statements.
137
        if (isset($fielddefinitioninfo['select_' . $CFG->dbtype])) {
138
            $select = $fielddefinitioninfo['select_' . $CFG->dbtype];
139
        } else {
140
            // Otherwise default to agnostic select (not db specific).
141
            if (!isset($fielddefinitioninfo['select'])) {
142
                throw new \coding_exception('Standard SQL fields need a select defined: ' . $name);
143
            }
144
            $select = $fielddefinitioninfo['select'];
145
        }
146
 
147
        if (!isset($fielddefinitioninfo['title'])) {
148
            throw new \coding_exception('Standard SQL fields need a title defined: ' . $name);
149
        }
150
 
151
        $newfielddefinition = new sql_field_definition(
152
            $select,
153
            $fielddefinitioninfo['name'],
154
            $fielddefinitioninfo['title'],
155
            isset($fielddefinitioninfo['visibility']) ? $fielddefinitioninfo['visibility'] :
156
                field_definition_interface::VISIBILITY_VISIBLE,
157
            isset($fielddefinitioninfo['options']) ? $fielddefinitioninfo['options'] : []);
158
 
159
        if (isset($fielddefinitioninfo['tables'])) {
160
            $newfielddefinition->set_option('tables', $fielddefinitioninfo['tables']);
161
        }
162
 
163
        // Support adding attributes from configuration array.
164
        if (isset($fielddefinitioninfo['attributes'])) {
165
            foreach ($fielddefinitioninfo['attributes'] as $attribute) {
166
                /** @var field_attribute_interface $newattribute */
167
                $newattribute = new $attribute['type']();
168
                if (isset($attribute['options'])) {
169
                    $newattribute->set_options($attribute['options']);
170
                }
171
                $newfielddefinition->add_attribute($newattribute);
172
            }
173
        }
174
 
175
        return $newfielddefinition;
176
    }
177
 
178
    /**
179
     * Get field definitions by names. Maintain order.
180
     *
181
     * @param string[] $names Field definition names to retrieve.
182
     * @return field_definition_interface[]
183
     * @throws \coding_exception
184
     */
185
    public static function get_field_definitions(array $names) {
186
        $fielddefinitions = [];
187
        $all = self::get_all_field_definitions();
188
 
189
        foreach ($all as $fielddefinition) {
190
            if (in_array($fielddefinition->get_name(), $names)) {
191
                $fielddefinitions[array_search($fielddefinition->get_name(), $names)] = clone $fielddefinition;
192
            }
193
        }
194
 
195
        ksort($fielddefinitions);
196
 
197
        return $fielddefinitions;
198
    }
199
 
200
    /**
201
     * Get field definition by name.
202
     *
203
     * @param string $name Field definition name to retrieve.
204
     * @return field_definition_interface
205
     * @throws \coding_exception
206
     */
207
    public static function get_field_definition($name) {
208
        foreach (self::get_all_field_definitions() as $fielddefinition) {
209
            if ($fielddefinition->get_name() == $name) {
210
                return clone $fielddefinition;
211
            }
212
        }
213
 
214
        return null;
215
    }
216
 
217
    /**
218
     * Get options for form select field.
219
     *
220
     * @param field_interface[] $fields
221
     * @return array
222
     * @throws \coding_exception
223
     */
224
    public static function get_field_definition_options($fields) {
225
        $options = [];
226
        foreach ($fields as $field) {
227
 
228
            $title = $field->get_table()->get_table_name() . ': ' . $field->get_title();
229
 
230
            $options[$field->get_alias()] = $title;
231
        }
232
 
233
        return $options;
234
    }
235
}