• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

includes/base.inc

Go to the documentation of this file.
00001 <?php
00011 class views_object {
00015   var $options = array();
00016 
00022   var $view = NULL;
00023 
00029   var $definition;
00030 
00048   function option_definition() { return array(); }
00049 
00054   function construct() { $this->set_default_options(); }
00055 
00060   function options(&$options) { }
00061 
00067   function set_default_options() {
00068     $this->_set_option_defaults($this->options, $this->option_definition());
00069 
00070     // Retained for complex defaults plus backward compatibility.
00071     $this->options($this->options);
00072   }
00073 
00074   function _set_option_defaults(&$storage, $options, $level = 0) {
00075     foreach ($options as $option => $definition) {
00076       if (isset($definition['contains']) && is_array($definition['contains'])) {
00077         $storage[$option] = array();
00078         $this->_set_option_defaults($storage[$option], $definition['contains'], $level++);
00079       }
00080       elseif (!empty($definition['translatable']) && !empty($definition['default'])) {
00081         $storage[$option] = t($definition['default']);
00082       }
00083       else {
00084         $storage[$option] = isset($definition['default']) ? $definition['default'] : NULL;
00085       }
00086     }
00087   }
00088 
00093   function unpack_options(&$storage, $options, $definition = NULL, $all = TRUE, $check = TRUE, $localization_keys = array()) {
00094     if ($check && !is_array($options)) {
00095       return;
00096     }
00097 
00098     if (!isset($definition)) {
00099       $definition = $this->option_definition();
00100     }
00101 
00102     if (!empty($this->view)) {
00103       // Ensure we have a localization plugin.
00104       $this->view->init_localization();
00105 
00106       // Set up default localization keys. Handlers and such set this for us
00107       if (empty($localization_keys) && isset($this->localization_keys)) {
00108         $localization_keys = $this->localization_keys;
00109       }
00110       // but plugins don't because there isn't a common init() these days.
00111       else if (!empty($this->is_plugin)) {
00112         if ($this->plugin_type != 'display') {
00113           $localization_keys = array($this->view->current_display);
00114           $localization_keys[] = $this->plugin_type;
00115         }
00116       }
00117     }
00118 
00119     foreach ($options as $key => $value) {
00120       if (is_array($value)) {
00121         // Ignore arrays with no definition.
00122         if (!$all && empty($definition[$key])) {
00123           continue;
00124         }
00125 
00126         if (!isset($storage[$key]) || !is_array($storage[$key])) {
00127           $storage[$key] = array();
00128         }
00129 
00130         // If we're just unpacking our known options, and we're dropping an
00131         // unknown array (as might happen for a dependent plugin fields) go
00132         // ahead and drop that in.
00133         if (!$all && isset($definition[$key]) && !isset($definition[$key]['contains'])) {
00134           $storage[$key] = $value;
00135           continue;
00136         }
00137 
00138         $this->unpack_options($storage[$key], $value, isset($definition[$key]['contains']) ? $definition[$key]['contains'] : array(), $all, FALSE, array_merge($localization_keys, array($key)));
00139       }
00140       // Don't localize strings during editing. When editing, we need to work with
00141       // the original data, not the translated version.
00142       else if (empty($this->view->editing) && !empty($definition[$key]['translatable']) && !empty($value) || !empty($definition['contains'][$key]['translatable']) && !empty($value)) {
00143         if (!empty($this->view) && $this->view->is_translatable()) {
00144           // Allow other modules to make changes to the string before it's
00145           // sent for translation.
00146           // The $keys array is built from the view name, any localization keys
00147           // sent in, and the name of the property being processed.
00148           $format = NULL;
00149           if (isset($definition[$key]['format_key']) && isset($options[$definition[$key]['format_key']])) {
00150             $format = $options[$definition[$key]['format_key']];
00151           }
00152           $translation_data = array(
00153             'value' => $value,
00154             'format' => $format,
00155             'keys' => array_merge(array($this->view->name), $localization_keys, array($key)),
00156           );
00157           $storage[$key] = $this->view->localization_plugin->translate($translation_data);
00158         }
00159         // Otherwise, this is a code-based string, so we can use t().
00160         else {
00161           $storage[$key] = t($value);
00162         }
00163       }
00164       else if ($all || !empty($definition[$key])) {
00165         $storage[$key] = $value;
00166       }
00167     }
00168   }
00169 
00173   function set_definition($definition) {
00174     $this->definition = $definition;
00175     if (isset($definition['field'])) {
00176       $this->real_field = $definition['field'];
00177     }
00178   }
00179 
00180   function destroy() {
00181     if (isset($this->view)) {
00182       unset($this->view);
00183     }
00184 
00185     if (isset($this->display)) {
00186       unset($this->display);
00187     }
00188 
00189     if (isset($this->query)) {
00190       unset($this->query);
00191     }
00192   }
00193 
00194   function export_options($indent, $prefix) {
00195     $output = '';
00196     foreach ($this->option_definition() as $option => $definition) {
00197       $output .= $this->export_option($indent, $prefix, $this->options, $option, $definition, array());
00198     }
00199 
00200     return $output;
00201   }
00202 
00203   function export_option($indent, $prefix, $storage, $option, $definition, $parents) {
00204     // Do not export options for which we have no settings.
00205     if (!isset($storage[$option])) {
00206       return;
00207     }
00208 
00209     if (isset($definition['export'])) {
00210       if ($definition['export'] === FALSE) {
00211         return;
00212       }
00213 
00214       // Special handling for some items
00215       if (method_exists($this, $definition['export'])) {
00216         return $this->{$definition['export']}($indent, $prefix, $storage, $option, $definition, $parents);
00217       }
00218     }
00219 
00220     // Add the current option to the parents tree.
00221     $parents[] = $option;
00222     $output = '';
00223 
00224     // If it has child items, export those separately.
00225     if (isset($definition['contains'])) {
00226       foreach ($definition['contains'] as $sub_option => $sub_definition) {
00227         $output .= $this->export_option($indent, $prefix, $storage[$option], $sub_option, $sub_definition, $parents);
00228       }
00229     }
00230     // Otherwise export just this item.
00231     else {
00232       $default = isset($definition['default']) ? $definition['default'] : NULL;
00233       $value = $storage[$option];
00234       if (isset($definition['bool'])) {
00235         $value = (bool) $value;
00236       }
00237 
00238       if ($value !== $default) {
00239         $output .= $indent . $prefix . "['" . implode("']['", $parents) . "'] = ";
00240         if (isset($definition['bool'])) {
00241           $output .= empty($storage[$option]) ? 'FALSE' : 'TRUE';
00242         }
00243         else {
00244           $output .= views_var_export($storage[$option], $indent);
00245         }
00246 
00247         $output .= ";\n";
00248       }
00249     }
00250     return $output;
00251   }
00252 
00256   function unpack_translatables(&$translatable, $parents = array()) {
00257     foreach ($this->option_definition() as $option => $definition) {
00258       $this->unpack_translatable($translatable, $this->options, $option, $definition, $parents, array());
00259     }
00260   }
00261 
00275   function unpack_translatable(&$translatable, $storage, $option, $definition, $parents, $keys = array()) {
00276     // Do not export options for which we have no settings.
00277     if (!isset($storage[$option])) {
00278       return;
00279     }
00280 
00281     // Special handling for some items
00282     if (isset($definition['unpack_translatable']) && method_exists($this, $definition['unpack_translatable'])) {
00283       return $this->{$definition['unpack_translatable']}($translatable, $storage, $option, $definition, $parents, $keys);
00284     }
00285 
00286     if (isset($definition['translatable'])) {
00287       if ($definition['translatable'] === FALSE) {
00288         return;
00289       }
00290     }
00291 
00292     // Add the current option to the parents tree.
00293     $parents[] = $option;
00294 
00295     // If it has child items, unpack those separately.
00296     if (isset($definition['contains'])) {
00297       foreach ($definition['contains'] as $sub_option => $sub_definition) {
00298         $translation_keys = array_merge($keys, array($sub_option));
00299         $this->unpack_translatable($translatable, $storage[$option], $sub_option, $sub_definition, $parents, $translation_keys);
00300       }
00301     }
00302 
00303     // @todo Figure out this double definition stuff.
00304     $options = $storage[$option];
00305     if (is_array($options)) {
00306       foreach ($options as $key => $value) {
00307         $translation_keys = array_merge($keys, array($key));
00308         if (is_array($value)) {
00309           $this->unpack_translatable($translatable, $options, $key, $definition, $parents, $translation_keys);
00310         }
00311         else if (!empty($definition[$key]['translatable']) && !empty($value)) {
00312           // Build source data and add to the array
00313           $format = NULL;
00314           if (isset($definition['format_key']) && isset($options[$definition['format_key']])) {
00315             $format = $options[$definition['format_key']];
00316           }
00317           $translatable[] = array(
00318             'value' => $value,
00319             'keys' => $translation_keys,
00320             'format' => $format,
00321           );
00322         }
00323       }
00324     }
00325     else if (!empty($definition['translatable']) && !empty($options)) {
00326       $value = $options;
00327       // Build source data and add to the array
00328       $format = NULL;
00329       if (isset($definition['format_key']) && isset($storage[$definition['format_key']])) {
00330         $format = $options[$definition['format_key']];
00331       }
00332       $translatable[] = array(
00333         'value' => $value,
00334         'keys' => isset($translation_keys) ? $translation_keys : $parents,
00335         'format' => $format,
00336       );
00337     }
00338   }
00339 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1