base.inc

  1. 3.x includes/base.inc
  2. 2.x includes/base.inc

Provides the basic object definitions used by plugins and handlers.

File

includes/base.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Provides the basic object definitions used by plugins and handlers.
  6. */
  7. /**
  8. * Basic definition for many views objects
  9. */
  10. class views_object {
  11. /**
  12. * Except for displays, options for the object will be held here.
  13. */
  14. var $options = array();
  15. /**
  16. * Information about options for all kinds of purposes will be held here.
  17. * @code
  18. * 'option_name' => array(
  19. * - 'default' => default value,
  20. * - 'translatable' => TRUE/FALSE (wrap in t() on export if true),
  21. * - 'contains' => array of items this contains, with its own defaults, etc.
  22. * If contains is set, the default will be ignored and assumed to
  23. * be array()
  24. *
  25. * ),
  26. * @endcode
  27. * Each option may have any of the following functions:
  28. * - export_option_OPTIONNAME -- Special export handling if necessary.
  29. * - translate_option_OPTIONNAME -- Special handling for translating data
  30. * within the option, if necessary.
  31. */
  32. function option_definition() { return array(); }
  33. /**
  34. * Views handlers use a special construct function so that we can more
  35. * easily construct them with variable arguments.
  36. */
  37. function construct() { $this->set_default_options(); }
  38. /**
  39. * Set default options on this object. Called by the constructor in a
  40. * complex chain to deal with backward compatibility.
  41. */
  42. function options() { }
  43. /**
  44. * Set default options.
  45. * For backward compatibility, it sends the options array; this is a
  46. * feature that will likely disappear at some point.
  47. */
  48. function set_default_options() {
  49. $this->_set_option_defaults($this->options, $this->option_definition());
  50. // Retained for complex defaults plus backward compatibility.
  51. $this->options($this->options);
  52. }
  53. function _set_option_defaults(&$storage, $options, $level = 0) {
  54. foreach ($options as $option => $definition) {
  55. if (isset($definition['contains']) && is_array($definition['contains'])) {
  56. $storage[$option] = array();
  57. $this->_set_option_defaults($storage[$option], $definition['contains'], $level++);
  58. }
  59. elseif (!empty($definition['translatable']) && !empty($definition['default'])) {
  60. $storage[$option] = t($definition['default']);
  61. }
  62. else {
  63. $storage[$option] = isset($definition['default']) ? $definition['default'] : NULL;
  64. }
  65. }
  66. }
  67. /**
  68. * Unpack options over our existing defaults, drilling down into arrays
  69. * so that defaults don't get totally blown away.
  70. */
  71. function unpack_options(&$storage, $options, $definition = NULL, $check = TRUE) {
  72. if ($check && !is_array($options)) {
  73. return;
  74. }
  75. if (!isset($definition)) {
  76. $definition = $this->option_definition();
  77. }
  78. foreach ($options as $key => $value) {
  79. if (is_array($value)) {
  80. if (!isset($storage[$key]) || !is_array($storage[$key])) {
  81. $storage[$key] = array();
  82. }
  83. $this->unpack_options($storage[$key], $value, isset($definition[$key]['contains']) ? $definition[$key]['contains'] : array(), FALSE);
  84. }
  85. else if (!empty($definition[$key]['translatable']) && !empty($value)) {
  86. $storage[$key] = t($value);
  87. }
  88. else {
  89. $storage[$key] = $value;
  90. }
  91. }
  92. }
  93. /**
  94. * Let the handler know what its full definition is.
  95. */
  96. function set_definition($definition) {
  97. $this->definition = $definition;
  98. if (isset($definition['field'])) {
  99. $this->real_field = $definition['field'];
  100. }
  101. }
  102. function destroy() {
  103. if (isset($this->view)) {
  104. unset($this->view);
  105. }
  106. if (isset($this->display)) {
  107. unset($this->display);
  108. }
  109. if (isset($this->query)) {
  110. unset($this->query);
  111. }
  112. }
  113. }