1 | <?php |
---|
2 | /** |
---|
3 | * @file |
---|
4 | * |
---|
5 | * Provides the basic object definitions used by plugins and handlers. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Basic definition for many views objects |
---|
10 | */ |
---|
11 | class views_object { |
---|
12 | /** |
---|
13 | * Except for displays, options for the object will be held here. |
---|
14 | */ |
---|
15 | var $options = array(); |
---|
16 | |
---|
17 | /** |
---|
18 | * The top object of a view. |
---|
19 | * |
---|
20 | * @var view |
---|
21 | */ |
---|
22 | var $view = NULL; |
---|
23 | |
---|
24 | /** |
---|
25 | * Handler's definition |
---|
26 | * |
---|
27 | * @var array |
---|
28 | */ |
---|
29 | var $definition; |
---|
30 | |
---|
31 | /** |
---|
32 | * Information about options for all kinds of purposes will be held here. |
---|
33 | * @code |
---|
34 | * 'option_name' => array( |
---|
35 | * - 'default' => default value, |
---|
36 | * - 'translatable' => TRUE/FALSE (wrap in t() on export if true), |
---|
37 | * - 'contains' => array of items this contains, with its own defaults, etc. |
---|
38 | * If contains is set, the default will be ignored and assumed to |
---|
39 | * be array() |
---|
40 | * |
---|
41 | * ), |
---|
42 | * @endcode |
---|
43 | * Each option may have any of the following functions: |
---|
44 | * - export_option_OPTIONNAME -- Special export handling if necessary. |
---|
45 | * - translate_option_OPTIONNAME -- Special handling for translating data |
---|
46 | * within the option, if necessary. |
---|
47 | */ |
---|
48 | function option_definition() { return array(); } |
---|
49 | |
---|
50 | /** |
---|
51 | * Views handlers use a special construct function so that we can more |
---|
52 | * easily construct them with variable arguments. |
---|
53 | */ |
---|
54 | function construct() { $this->set_default_options(); } |
---|
55 | |
---|
56 | /** |
---|
57 | * Set default options on this object. Called by the constructor in a |
---|
58 | * complex chain to deal with backward compatibility. |
---|
59 | */ |
---|
60 | function options() { } |
---|
61 | |
---|
62 | /** |
---|
63 | * Set default options. |
---|
64 | * For backward compatibility, it sends the options array; this is a |
---|
65 | * feature that will likely disappear at some point. |
---|
66 | */ |
---|
67 | function set_default_options() { |
---|
68 | $this->_set_option_defaults($this->options, $this->option_definition()); |
---|
69 | |
---|
70 | // Retained for complex defaults plus backward compatibility. |
---|
71 | $this->options($this->options); |
---|
72 | } |
---|
73 | |
---|
74 | function _set_option_defaults(&$storage, $options, $level = 0) { |
---|
75 | foreach ($options as $option => $definition) { |
---|
76 | if (isset($definition['contains']) && is_array($definition['contains'])) { |
---|
77 | $storage[$option] = array(); |
---|
78 | $this->_set_option_defaults($storage[$option], $definition['contains'], $level++); |
---|
79 | } |
---|
80 | elseif (!empty($definition['translatable']) && !empty($definition['default'])) { |
---|
81 | $storage[$option] = t($definition['default']); |
---|
82 | } |
---|
83 | else { |
---|
84 | $storage[$option] = isset($definition['default']) ? $definition['default'] : NULL; |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Unpack options over our existing defaults, drilling down into arrays |
---|
91 | * so that defaults don't get totally blown away. |
---|
92 | */ |
---|
93 | function unpack_options(&$storage, $options, $definition = NULL, $all = TRUE, $check = TRUE, $localization_keys = array()) { |
---|
94 | if ($check && !is_array($options)) { |
---|
95 | return; |
---|
96 | } |
---|
97 | |
---|
98 | if (!isset($definition)) { |
---|
99 | $definition = $this->option_definition(); |
---|
100 | } |
---|
101 | |
---|
102 | if (!empty($this->view)) { |
---|
103 | // Ensure we have a localization plugin. |
---|
104 | $this->view->init_localization(); |
---|
105 | |
---|
106 | // Set up default localization keys. Handlers and such set this for us |
---|
107 | if (empty($localization_keys) && isset($this->localization_keys)) { |
---|
108 | $localization_keys = $this->localization_keys; |
---|
109 | } |
---|
110 | // but plugins don't because there isn't a common init() these days. |
---|
111 | else if (!empty($this->is_plugin)) { |
---|
112 | if ($this->plugin_type != 'display') { |
---|
113 | $localization_keys = array($this->view->current_display); |
---|
114 | $localization_keys[] = $this->plugin_type; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | foreach ($options as $key => $value) { |
---|
120 | if (is_array($value)) { |
---|
121 | // Ignore arrays with no definition. |
---|
122 | if (!$all && empty($definition[$key])) { |
---|
123 | continue; |
---|
124 | } |
---|
125 | |
---|
126 | if (!isset($storage[$key]) || !is_array($storage[$key])) { |
---|
127 | $storage[$key] = array(); |
---|
128 | } |
---|
129 | |
---|
130 | // If we're just unpacking our known options, and we're dropping an |
---|
131 | // unknown array (as might happen for a dependent plugin fields) go |
---|
132 | // ahead and drop that in. |
---|
133 | if (!$all && isset($definition[$key]) && !isset($definition[$key]['contains'])) { |
---|
134 | $storage[$key] = $value; |
---|
135 | continue; |
---|
136 | } |
---|
137 | |
---|
138 | $this->unpack_options($storage[$key], $value, isset($definition[$key]['contains']) ? $definition[$key]['contains'] : array(), $all, FALSE, array_merge($localization_keys, array($key))); |
---|
139 | } |
---|
140 | // Don't localize strings during editing. When editing, we need to work with |
---|
141 | // the original data, not the translated version. |
---|
142 | else if (empty($this->view->editing) && !empty($definition[$key]['translatable']) && !empty($value) || !empty($definition['contains'][$key]['translatable']) && !empty($value)) { |
---|
143 | if (!empty($this->view) && $this->view->is_translatable()) { |
---|
144 | // Allow other modules to make changes to the string before it's |
---|
145 | // sent for translation. |
---|
146 | // The $keys array is built from the view name, any localization keys |
---|
147 | // sent in, and the name of the property being processed. |
---|
148 | $format = NULL; |
---|
149 | if (isset($definition[$key]['format_key']) && isset($options[$definition[$key]['format_key']])) { |
---|
150 | $format = $options[$definition[$key]['format_key']]; |
---|
151 | } |
---|
152 | $translation_data = array( |
---|
153 | 'value' => $value, |
---|
154 | 'format' => $format, |
---|
155 | 'keys' => array_merge(array($this->view->name), $localization_keys, array($key)), |
---|
156 | ); |
---|
157 | $storage[$key] = $this->view->localization_plugin->translate($translation_data); |
---|
158 | } |
---|
159 | // Otherwise, this is a code-based string, so we can use t(). |
---|
160 | else { |
---|
161 | $storage[$key] = t($value); |
---|
162 | } |
---|
163 | } |
---|
164 | else if ($all || !empty($definition[$key])) { |
---|
165 | $storage[$key] = $value; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Let the handler know what its full definition is. |
---|
172 | */ |
---|
173 | function set_definition($definition) { |
---|
174 | $this->definition = $definition; |
---|
175 | if (isset($definition['field'])) { |
---|
176 | $this->real_field = $definition['field']; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | function destroy() { |
---|
181 | if (isset($this->view)) { |
---|
182 | unset($this->view); |
---|
183 | } |
---|
184 | |
---|
185 | if (isset($this->display)) { |
---|
186 | unset($this->display); |
---|
187 | } |
---|
188 | |
---|
189 | if (isset($this->query)) { |
---|
190 | unset($this->query); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | function export_options($indent, $prefix) { |
---|
195 | $output = ''; |
---|
196 | foreach ($this->option_definition() as $option => $definition) { |
---|
197 | $output .= $this->export_option($indent, $prefix, $this->options, $option, $definition, array()); |
---|
198 | } |
---|
199 | |
---|
200 | return $output; |
---|
201 | } |
---|
202 | |
---|
203 | function export_option($indent, $prefix, $storage, $option, $definition, $parents) { |
---|
204 | // Do not export options for which we have no settings. |
---|
205 | if (!isset($storage[$option])) { |
---|
206 | return; |
---|
207 | } |
---|
208 | |
---|
209 | if (isset($definition['export'])) { |
---|
210 | if ($definition['export'] === FALSE) { |
---|
211 | return; |
---|
212 | } |
---|
213 | |
---|
214 | // Special handling for some items |
---|
215 | if (method_exists($this, $definition['export'])) { |
---|
216 | return $this->{$definition['export']}($indent, $prefix, $storage, $option, $definition, $parents); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | // Add the current option to the parents tree. |
---|
221 | $parents[] = $option; |
---|
222 | $output = ''; |
---|
223 | |
---|
224 | // If it has child items, export those separately. |
---|
225 | if (isset($definition['contains'])) { |
---|
226 | foreach ($definition['contains'] as $sub_option => $sub_definition) { |
---|
227 | $output .= $this->export_option($indent, $prefix, $storage[$option], $sub_option, $sub_definition, $parents); |
---|
228 | } |
---|
229 | } |
---|
230 | // Otherwise export just this item. |
---|
231 | else { |
---|
232 | $default = isset($definition['default']) ? $definition['default'] : NULL; |
---|
233 | $value = $storage[$option]; |
---|
234 | if (isset($definition['bool'])) { |
---|
235 | $value = (bool) $value; |
---|
236 | } |
---|
237 | |
---|
238 | if ($value !== $default) { |
---|
239 | $output .= $indent . $prefix . "['" . implode("']['", $parents) . "'] = "; |
---|
240 | if (isset($definition['bool'])) { |
---|
241 | $output .= empty($storage[$option]) ? 'FALSE' : 'TRUE'; |
---|
242 | } |
---|
243 | else { |
---|
244 | $output .= views_var_export($storage[$option], $indent); |
---|
245 | } |
---|
246 | |
---|
247 | $output .= ";\n"; |
---|
248 | } |
---|
249 | } |
---|
250 | return $output; |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * Unpacks each handler to store translatable texts. |
---|
255 | */ |
---|
256 | function unpack_translatables(&$translatable, $parents = array()) { |
---|
257 | foreach ($this->option_definition() as $option => $definition) { |
---|
258 | $this->unpack_translatable($translatable, $this->options, $option, $definition, $parents, array()); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Unpack a single option definition. |
---|
264 | * |
---|
265 | * This function run's through all suboptions recursive. |
---|
266 | * |
---|
267 | * @param $translatable |
---|
268 | * Stores all availible translatable items. |
---|
269 | * @param $storage |
---|
270 | * @param $option |
---|
271 | * @param $definition |
---|
272 | * @param $parents |
---|
273 | * @param $keys |
---|
274 | */ |
---|
275 | function unpack_translatable(&$translatable, $storage, $option, $definition, $parents, $keys = array()) { |
---|
276 | // Do not export options for which we have no settings. |
---|
277 | if (!isset($storage[$option])) { |
---|
278 | return; |
---|
279 | } |
---|
280 | |
---|
281 | // Special handling for some items |
---|
282 | if (isset($definition['unpack_translatable']) && method_exists($this, $definition['unpack_translatable'])) { |
---|
283 | return $this->{$definition['unpack_translatable']}($translatable, $storage, $option, $definition, $parents, $keys); |
---|
284 | } |
---|
285 | |
---|
286 | if (isset($definition['translatable'])) { |
---|
287 | if ($definition['translatable'] === FALSE) { |
---|
288 | return; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | // Add the current option to the parents tree. |
---|
293 | $parents[] = $option; |
---|
294 | |
---|
295 | // If it has child items, unpack those separately. |
---|
296 | if (isset($definition['contains'])) { |
---|
297 | foreach ($definition['contains'] as $sub_option => $sub_definition) { |
---|
298 | $translation_keys = array_merge($keys, array($sub_option)); |
---|
299 | $this->unpack_translatable($translatable, $storage[$option], $sub_option, $sub_definition, $parents, $translation_keys); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | // @todo Figure out this double definition stuff. |
---|
304 | $options = $storage[$option]; |
---|
305 | if (is_array($options)) { |
---|
306 | foreach ($options as $key => $value) { |
---|
307 | $translation_keys = array_merge($keys, array($key)); |
---|
308 | if (is_array($value)) { |
---|
309 | $this->unpack_translatable($translatable, $options, $key, $definition, $parents, $translation_keys); |
---|
310 | } |
---|
311 | else if (!empty($definition[$key]['translatable']) && !empty($value)) { |
---|
312 | // Build source data and add to the array |
---|
313 | $format = NULL; |
---|
314 | if (isset($definition['format_key']) && isset($options[$definition['format_key']])) { |
---|
315 | $format = $options[$definition['format_key']]; |
---|
316 | } |
---|
317 | $translatable[] = array( |
---|
318 | 'value' => $value, |
---|
319 | 'keys' => $translation_keys, |
---|
320 | 'format' => $format, |
---|
321 | ); |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | else if (!empty($definition['translatable']) && !empty($options)) { |
---|
326 | $value = $options; |
---|
327 | // Build source data and add to the array |
---|
328 | $format = NULL; |
---|
329 | if (isset($definition['format_key']) && isset($options[$definition['format_key']])) { |
---|
330 | $format = $options[$definition['format_key']]; |
---|
331 | } |
---|
332 | $translatable[] = array( |
---|
333 | 'value' => $value, |
---|
334 | 'keys' => isset($translation_keys) ? $translation_keys : $parents, |
---|
335 | 'format' => $format, |
---|
336 | ); |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|