views_php_handler_sort.inc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * A handler to sort a view using PHP defined by the administrator.
  4. *
  5. * @ingroup views_sort_handlers
  6. */
  7. class views_php_handler_sort extends views_handler_sort {
  8. protected $php_static_variable = NULL;
  9. /**
  10. * Implements views_object#option_definition().
  11. */
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['use_php_setup'] = array('default' => FALSE);
  15. $options['php_setup'] = array('default' => '');
  16. $options['php_sort'] = array('default' => '');
  17. return $options;
  18. }
  19. /**
  20. * Implements views_handler#option_definition().
  21. */
  22. function options_form(&$form, &$form_state) {
  23. parent::options_form($form, $form_state);
  24. $form += views_php_form_element($this,
  25. 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.')),
  26. array('php_setup', t('Setup code'), t('Code to run right before execution of the view.'), FALSE),
  27. array('$view', '$handler', '$static')
  28. );
  29. $form += views_php_form_element($this,
  30. FALSE,
  31. array('php_sort', t('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),
  32. array(
  33. '$view', '$handler', '$static',
  34. '$row1' => t('Data of row.'),
  35. '$row2' => t('Data of row to compare against.'),
  36. )
  37. );
  38. }
  39. /**
  40. * Implements views_handler_sort#query().
  41. */
  42. function query() {
  43. // Inform views_php_views_pre_execute() to seize control over the query.
  44. $this->view->views_php = TRUE;
  45. }
  46. /**
  47. *
  48. * @see views_php_views_pre_execute()
  49. */
  50. function php_pre_execute() {
  51. // Ecexute static PHP code.
  52. if (!empty($this->options['php_setup'])) {
  53. $function = create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
  54. ob_start();
  55. $function($this->view, $this, $this->php_static_variable);
  56. ob_end_clean();
  57. }
  58. }
  59. /**
  60. *
  61. * @see views_php_views_post_execute()
  62. */
  63. function php_post_execute() {
  64. if (!empty($this->options['php_sort']) && $this->view->style_plugin->build_sort()) {
  65. $this->php_sort_function = create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_sort'] . ';');
  66. ob_start();
  67. usort($this->view->result, array($this, 'php_sort'));
  68. ob_end_clean();
  69. }
  70. }
  71. /**
  72. * Helper function; usort() callback for sort support.
  73. */
  74. function php_sort($row1, $row2) {
  75. $factor = strtoupper($this->options['order']) == 'ASC' ? 1 : -1;
  76. $function = $this->php_sort_function;
  77. foreach (array('row1' => 'normalized_row1', 'row2' => 'normalized_row2') as $name => $normalized_name) {
  78. $$normalized_name = new stdClass;
  79. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  80. $$normalized_name->$field = isset($$name->{$handler->field_alias}) ? $$name->{$handler->field_alias} : NULL;
  81. }
  82. }
  83. $result = (int)$function($this->view, $this, $this->php_static_variable, $normalized_row1, $normalized_row2);
  84. return $factor * $result;
  85. }
  86. }