base.inc

  1. nittany7 modules/contrib/views/includes/base.inc
  2. nittany7 themes/contrib/omega/alpha/includes/base.inc
  3. nittany7 modules/contrib/entityreference/plugins/selection/base.inc
  4. cis7 modules/contrib/entityreference/plugins/selection/base.inc
  5. cis7 modules/contrib/views/includes/base.inc
  6. mooc7 modules/contrib/entityreference/plugins/selection/base.inc
  7. mooc7 modules/contrib/views/includes/base.inc

Provides the basic object definitions used by plugins and handlers.

Classes

Namesort descending Description
views_object Basic definition for many views objects.

File

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