views_php.module 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 (e.g. $data->nid).'),
  44. '$data' => t('Contains the retrieved record from the database (e.g. $data->nid).'),
  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. foreach ($handler->view->display_handler->get_handlers('field') as $field => $field_handler) {
  95. $items[] = l($variable_name . '->' . $field, '', array('fragment' => $container[$name]['#id'], 'external' => TRUE)) . ': ' . $field_handler->ui_name();
  96. }
  97. }
  98. }
  99. $container[$name . '_variables'] = array(
  100. '#type' => 'fieldset',
  101. '#title' => t('Available variables'),
  102. '#collapsible' => TRUE,
  103. '#collapsed' => TRUE,
  104. '#attributes' => array('class' => array('views-php-variables')),
  105. '#attached' => array(
  106. 'js' => array(drupal_get_path('module', 'views_php') . '/views_php.js'),
  107. )
  108. );
  109. $container[$name . '_variables']['variables'] = array(
  110. '#theme' => 'item_list',
  111. '#items' => $items,
  112. );
  113. if (!empty($checkbox)) {
  114. return array($checkbox_name => $checkbox, $name => $container);
  115. }
  116. return array($name => $container);
  117. }
  118. /**
  119. * Implements hook_views_pre_execute().
  120. */
  121. function views_php_views_pre_execute($view) {
  122. // Seize control over the query plugin if a views handler requested so.
  123. if (!empty($view->views_php)) {
  124. $query = new views_php_plugin_query();
  125. $query->php_wrap($view->query);
  126. }
  127. }
  128. /**
  129. * Implements hook_views_post_execute().
  130. */
  131. function views_php_views_post_execute($view) {
  132. // Restore original query plugin if it was wrapped.
  133. if ($view->query instanceof views_php_plugin_wrapper) {
  134. $view->query->php_unwrap();
  135. }
  136. }
  137. /**
  138. * Implements hook_views_post_build().
  139. */
  140. function views_php_views_post_build($view) {
  141. // Clear the limit and offset
  142. if (!empty($view->views_php) && !empty($view->build_info['query']) && is_object($view->build_info['query'])) {
  143. $view->build_info['query']->range();
  144. }
  145. }