views_php_handler_field.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * A handler to provide a field that is constructed by the administrator using PHP.
  4. *
  5. * @ingroup views_field_handlers
  6. */
  7. class views_php_handler_field extends views_handler_field {
  8. const CLICK_SORT_DISABLED = 0;
  9. const CLICK_SORT_NUMERIC = 1;
  10. const CLICK_SORT_ALPHA = 2;
  11. const CLICK_SORT_ALPHA_CASE = 3;
  12. const CLICK_SORT_NAT = 4;
  13. const CLICK_SORT_NAT_CASE = 5;
  14. const CLICK_SORT_PHP = 6;
  15. protected $php_static_variable = NULL;
  16. /**
  17. * Implements views_object#option_definition().
  18. */
  19. function option_definition() {
  20. $options = parent::option_definition();
  21. $options['use_php_setup'] = array('default' => FALSE);
  22. $options['php_setup'] = array('default' => '');
  23. $options['php_value'] = array('default' => '');
  24. $options['php_output'] = array('default' => '');
  25. $options['use_php_click_sortable'] = array('default' => self::CLICK_SORT_DISABLED);
  26. $options['php_click_sortable'] = array('default' => FALSE);
  27. return $options;
  28. }
  29. /**
  30. * Implements views_handler#options_form().
  31. */
  32. function options_form(&$form, &$form_state) {
  33. parent::options_form($form, $form_state);
  34. $form += views_php_form_element($this,
  35. array('use_php_setup', t('Use setup code'), t('If checked, you can provide PHP code to be run once right before execution of the view. This may be useful to define functions to be re-used in the value and/or output code.')),
  36. array('php_setup', t('Setup code'), t('Code to run right before execution of the view.'), FALSE),
  37. array('$view', '$handler', '$static')
  38. );
  39. $form += views_php_form_element($this,
  40. FALSE,
  41. array('php_value', t('Value code'), t('Code to construct the value of this field.'), FALSE),
  42. array('$view', '$handler', '$static', '$row', '$data')
  43. );
  44. $form += views_php_form_element($this,
  45. array('use_php_click_sortable', t('Enable click sort'), t('If checked, you can use PHP code to enable click sort on the field.')),
  46. array('php_click_sortable', t('Click sort code'), t('The comparison code must return an integer less than, equal to, or greater than zero if the first row should respectively appear before, stay where it was compared to, or appear after the second row.'), FALSE),
  47. array(
  48. '$view', '$handler', '$static',
  49. '$row1' => t('Data of row.'),
  50. '$row2' => t('Data of row to compare against.'),
  51. )
  52. );
  53. $form['use_php_click_sortable']['#type'] = 'select';
  54. $form['use_php_click_sortable']['#options'] = array(
  55. self::CLICK_SORT_DISABLED => t('No'),
  56. self::CLICK_SORT_NUMERIC => t('Sort numerically'),
  57. self::CLICK_SORT_ALPHA => t('Sort alphabetically'),
  58. self::CLICK_SORT_ALPHA_CASE => t('Sort alphabetically (case insensitive)'),
  59. self::CLICK_SORT_NAT => t('Sort using a "natural order" algorithm'),
  60. self::CLICK_SORT_NAT_CASE => t('Sort using a "natural order" algorithm (case insensitive)'),
  61. self::CLICK_SORT_PHP => t('Sort using custom PHP code'),
  62. );
  63. $form['use_php_click_sortable']['#default_value'] = $this->options['use_php_click_sortable'];
  64. $form['php_click_sortable']['#states'] = array(
  65. 'visible' => array(
  66. ':input[name="options[use_php_click_sortable]"]' => array('value' => (string)self::CLICK_SORT_PHP),
  67. ),
  68. );
  69. $form += views_php_form_element($this,
  70. FALSE,
  71. array('php_output', t('Output code'), t('Code to construct the output of this field.'), TRUE),
  72. array('$view', '$handler', '$static', '$row', '$data', '$value' => t('Value of this field.'))
  73. );
  74. }
  75. /**
  76. * Implements views_handler_field#query().
  77. *
  78. * @see views_php_views_pre_execute()
  79. */
  80. function query() {
  81. // Provide an field alias but don't actually alter the query.
  82. $this->field_alias = 'views_php_' . $this->position;
  83. // Inform views_php_views_pre_execute() to seize control over the query.
  84. $this->view->views_php = TRUE;
  85. }
  86. /**
  87. * Implements views_handler_field#click_sortable().
  88. */
  89. function click_sortable() {
  90. return $this->options['use_php_click_sortable'] != self::CLICK_SORT_DISABLED;
  91. }
  92. /**
  93. * Implements views_handler_field#click_sort().
  94. *
  95. * @see self::php_post_execute()
  96. */
  97. function click_sort($order) {
  98. $this->php_click_sort_order = $order;
  99. }
  100. /**
  101. *
  102. * @see views_php_views_pre_execute()
  103. * @see self::php_post_execute()
  104. */
  105. function php_pre_execute() {
  106. // Ecexute static PHP code.
  107. if (!empty($this->options['php_setup'])) {
  108. $function = create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
  109. ob_start();
  110. $function($this->view, $this, $this->php_static_variable);
  111. ob_end_clean();
  112. }
  113. }
  114. /**
  115. *
  116. * @see views_php_views_post_execute()
  117. */
  118. function php_post_execute() {
  119. // Ecexute value PHP code.
  120. if (!empty($this->options['php_value'])) {
  121. $function = create_function('$view, $handler, &$static, $row, $data', $this->options['php_value'] . ';');
  122. ob_start();
  123. foreach ($this->view->result as $i => &$row) {
  124. $normalized_row = new stdClass;
  125. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  126. $normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
  127. }
  128. $row->{$this->field_alias} = $function($this->view, $this, $this->php_static_variable, $normalized_row, $row);
  129. }
  130. ob_end_clean();
  131. }
  132. // If we're sorting, do the actual sorting then fix the results as per the pager info.
  133. if (!empty($this->options['use_php_click_sortable']) && !empty($this->php_click_sort_order)) {
  134. if ($this->options['use_php_click_sortable'] == self::CLICK_SORT_PHP) {
  135. if (!empty($this->options['php_click_sortable'])) {
  136. $this->php_click_sort_function = create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_click_sortable'] . ';');
  137. }
  138. }
  139. else {
  140. $predefined = array(
  141. self::CLICK_SORT_NUMERIC => array($this, 'php_click_sort_numeric'),
  142. self::CLICK_SORT_ALPHA => 'strcasecmp',
  143. self::CLICK_SORT_ALPHA_CASE => 'strcmp',
  144. self::CLICK_SORT_NAT => 'strnatcasecmp',
  145. self::CLICK_SORT_NAT_CASE => 'strnatcmp',
  146. );
  147. $this->php_click_sort_function = $predefined[$this->options['use_php_click_sortable']];
  148. }
  149. if (isset($this->php_click_sort_function)) {
  150. usort($this->view->result, array($this, 'php_click_sort'));
  151. }
  152. }
  153. }
  154. /**
  155. * Helper function; usort() callback for click sort support.
  156. */
  157. function php_click_sort($row1, $row2) {
  158. $factor = strtoupper($this->php_click_sort_order) == 'ASC' ? 1 : -1;
  159. $function = $this->php_click_sort_function;
  160. if ($this->options['use_php_click_sortable'] == self::CLICK_SORT_PHP) {
  161. foreach (array('row1' => 'normalized_row1', 'row2' => 'normalized_row2') as $name => $normalized_name) {
  162. $$normalized_name = new stdClass;
  163. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  164. $$normalized_name->$field = isset($$name->{$handler->field_alias}) ? $$name->{$handler->field_alias} : NULL;
  165. }
  166. }
  167. ob_start();
  168. $result = (int)$function($this->view, $this, $this->php_static_variable, $normalized_row1, $normalized_row2);
  169. ob_end_clean();
  170. }
  171. else {
  172. $result = $function($row1->{$this->field_alias}, $row2->{$this->field_alias});
  173. }
  174. return $factor * $result;
  175. }
  176. /**
  177. * Helper function; usort callback for numeric sort.
  178. */
  179. function php_click_sort_numeric($a, $b) {
  180. return $a - $b;
  181. }
  182. /**
  183. * Implements views_handler_field#pre_render().
  184. */
  185. function pre_render(&$values) {
  186. if (!empty($this->options['php_output'])) {
  187. $this->php_output_lamda_function = create_function('$view, $handler, &$static, $row, $data, $value', ' ?>' . $this->options['php_output'] . '<?php ');
  188. }
  189. }
  190. /**
  191. * Implements views_handler_field#render().
  192. */
  193. function render($values) {
  194. // Ecexute output PHP code.
  195. if (!empty($this->options['php_output']) && isset($this->php_output_lamda_function)) {
  196. $normalized_row = new stdClass;
  197. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  198. $normalized_row->$field = isset($values->{$handler->field_alias}) ? $values->{$handler->field_alias} : NULL;
  199. }
  200. $function = $this->php_output_lamda_function;
  201. ob_start();
  202. $function($this->view, $this, $this->php_static_variable, $normalized_row, $values, isset($values->{$this->field_alias}) ? $values->{$this->field_alias} : NULL);
  203. $value = ob_get_clean();
  204. }
  205. else {
  206. if (isset($values->{$this->field_alias})) {
  207. $value = check_plain($values->{$this->field_alias});
  208. }
  209. }
  210. if (isset($value)) {
  211. return $value;
  212. }
  213. }
  214. }