views_php.module 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @file
  4. * Allows to use PHP in views.
  5. */
  6. /**
  7. * Implements hook_views_api().
  8. */
  9. function views_php_views_api() {
  10. return array(
  11. 'api' => 3,
  12. );
  13. }
  14. /**
  15. * Menu access callback function; use PHP code to determine whether a user as
  16. * access.
  17. */
  18. function views_php_check_access($php_access, $view_name, $display_id, $account = NULL) {
  19. global $user;
  20. static $function = array();
  21. if (!isset($account)) {
  22. $account = $user;
  23. }
  24. if (!isset($function[$view_name . ':' . $display_id])) {
  25. $function[$view_name . ':' . $display_id] = create_function('$view_name, $display_id, $account', $php_access . ';');
  26. }
  27. ob_start();
  28. $access = (bool) $function[$view_name . ':' . $display_id]($view_name, $display_id, $account);
  29. ob_end_clean();
  30. return $access;
  31. }
  32. /**
  33. * Helper function; builds form for PHP code options of views handlers/plugins.
  34. */
  35. function views_php_form_element($handler, $checkbox = FALSE, $input, $variables = array()) {
  36. static $default_variables;
  37. if (!isset($default_variables)) {
  38. $default_variables = array(
  39. '$view' => t('The view object.'),
  40. '$handler' => t('The handler object.'),
  41. '$plugin' => t('The plugin object.'),
  42. '$static' => t('A variable that can be used to store reusable data per row.'),
  43. '$row' => t('Contains the retrieved record from the database per row.'),
  44. '$data' => t('Contains the retrieved record from the database for all rows.'),
  45. '$results' => t('Array containing the view\'s result.'),
  46. '$cache' => t('The cache object.'),
  47. );
  48. }
  49. list($name, $title, $description, $use_delimiters) = $input;
  50. $container = array(
  51. '#type' => 'container',
  52. // @todo #tree => FALSE doesn't work here.
  53. '#parents' => array('options'),
  54. );
  55. if (!empty($checkbox)) {
  56. list($checkbox_name, $checkbox_title, $checkbox_description) = $checkbox;
  57. $checkbox = array(
  58. '#type' => 'checkbox',
  59. '#title' => $checkbox_title,
  60. '#description' => $checkbox_description,
  61. '#default_value' => $handler->options[$checkbox_name] && !empty($handler->options[$name]),
  62. );
  63. $container['#states'] = array(
  64. 'invisible' => array(
  65. 'input[name="options[use_' . $name . ']"]' => array('checked' => FALSE),
  66. ),
  67. );
  68. }
  69. $container[$name] = array(
  70. '#type' => 'textarea',
  71. '#id' => drupal_html_id('edit-options-' . $name),
  72. '#title' => $title,
  73. '#default_value' => $handler->options[$name],
  74. '#rows' => 5,
  75. '#description' => $description . ' <strong>' . ($use_delimiters
  76. ? t('Use &lt;?php ?&gt; delimiters to enclose PHP code.')
  77. : t('Do not use &lt;?php ?&gt; delimiters.'))
  78. . '</strong>',
  79. );
  80. // Only users with use PHP permission can set/modify input.
  81. if (!user_access('use PHP for settings')) {
  82. $container[$name]['#disabled'] = TRUE;
  83. $container[$name]['#value'] = $container[$name]['#default_value'];
  84. $container[$name]['#description'] .= ' <strong>' . t('You do not have permission to modify this.') . '</strong>';
  85. }
  86. $items = array();
  87. foreach ($variables as $variable_name => $description) {
  88. if (is_int($variable_name)) {
  89. $variable_name = $description;
  90. $description = isset($default_variables[$description]) ? $default_variables[$description] : '';
  91. }
  92. $items[] = l($variable_name, '', array('fragment' => $container[$name]['#id'], 'external' => TRUE)) . ': ' . $description;
  93. if (strpos($variable_name, '$row') === 0) {
  94. $php_value = ($input[0] == 'php_value') ? true : false;
  95. foreach ($handler->view->display_handler->get_handlers('field') as $field => $field_handler) {
  96. // Do not add fields that will not have data when evaluating the value code. This occurs because
  97. // the value code is evaluated in hook_views_post_execute(), but field data is made available in hook_views_pre_render(),
  98. // which is called after hook_views_post_execute().
  99. if ($php_value && $field_handler->table != $field_handler->view->base_table) {
  100. continue;
  101. }
  102. $items[] = l($variable_name . '->' . $field, '', array('fragment' => $container[$name]['#id'], 'external' => TRUE)) . ': ' . $field_handler->ui_name();
  103. }
  104. }
  105. }
  106. $container[$name . '_variables'] = array(
  107. '#type' => 'fieldset',
  108. '#title' => t('Available variables'),
  109. '#collapsible' => TRUE,
  110. '#collapsed' => TRUE,
  111. '#attributes' => array('class' => array('views-php-variables')),
  112. '#attached' => array(
  113. 'js' => array(drupal_get_path('module', 'views_php') . '/views_php.js'),
  114. )
  115. );
  116. $container[$name . '_variables']['variables'] = array(
  117. '#theme' => 'item_list',
  118. '#items' => $items,
  119. );
  120. if (!empty($checkbox)) {
  121. return array($checkbox_name => $checkbox, $name => $container);
  122. }
  123. return array($name => $container);
  124. }
  125. /**
  126. * Implements hook_views_pre_execute().
  127. */
  128. function views_php_views_pre_execute($view) {
  129. // Seize control over the query plugin if a views handler requested so.
  130. if (!empty($view->views_php)) {
  131. $query = new views_php_plugin_query();
  132. $query->php_wrap($view->query);
  133. }
  134. }
  135. /**
  136. * Implements hook_views_post_execute().
  137. */
  138. function views_php_views_post_render($view) {
  139. // Restore original query plugin if it was wrapped.
  140. if ($view->query instanceof views_php_plugin_wrapper) {
  141. $view->query->php_unwrap();
  142. }
  143. }