workflow_views_handler_filter_sid.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @file
  4. * Provide views filter handler for workflow.module.
  5. */
  6. /**
  7. * Filter by state.
  8. */
  9. class workflow_views_handler_filter_sid extends views_handler_filter_in_operator {
  10. var $value_form_type = 'select';
  11. function get_value_options() {
  12. if (isset($this->value_options)) {
  13. return;
  14. }
  15. if (!isset($this->value_options)) {
  16. // Show the possible State options.
  17. $this->value_options = array();
  18. $this->value_title = t('Workflow state');
  19. $all = (bool) $this->options['expose']['workflow_include_all'];
  20. $wid = isset($this->options['expose']['workflow_reduce_wid']) ? $this->options['expose']['workflow_reduce_wid'] : 0;
  21. $states = array();
  22. // Count the workflows to determine grouping.
  23. // Even if $wid is not set, we may only have 1 workflow.
  24. $grouped = FALSE;
  25. $workflows = workflow_load_multiple($wid ? array($wid) : FALSE);
  26. $count = count($workflows);
  27. if ($count > 1) {
  28. $states += array('' => t('No state'));
  29. $states += array('ANY' => t('A state'));
  30. $grouped = TRUE;
  31. }
  32. $states += workflow_get_workflow_state_names($wid, $grouped, $all);
  33. $this->value_options = $states;
  34. }
  35. return $this->value_options;
  36. }
  37. function expose_options() {
  38. parent::expose_options();
  39. $this->options['expose']['workflow_reduce_wid'] = '';
  40. $this->options['expose']['workflow_include_all'] = FALSE;
  41. }
  42. function expose_form(&$form, &$form_state) {
  43. $workflows[''] = t('- Select a value -');
  44. foreach (workflow_load_multiple() as $workflow) {
  45. $workflows[$workflow->wid] = $workflow->label();
  46. }
  47. $form['expose']['workflow_reduce_wid'] = array(
  48. '#type' => 'select',
  49. '#title' => t('Workflows'),
  50. '#options' => $workflows,
  51. '#description' => t('Select which workflow the states are given from.'),
  52. '#default_value' => $this->options['expose']['workflow_reduce_wid'],
  53. );
  54. $form['expose']['workflow_include_all'] = array(
  55. '#type' => 'checkbox',
  56. '#title' => "Include '(creation)' and inactive states?",
  57. '#default_value' => $this->options['expose']['workflow_include_all'],
  58. );
  59. parent::expose_form($form, $form_state);
  60. }
  61. function option_definition() {
  62. $options = parent::option_definition();
  63. $options['expose']['contains']['workflow_reduce_wid'] = array('default' => '');
  64. $options['expose']['contains']['workflow_include_all'] = array('default' => FALSE, 'bool' => TRUE);
  65. return $options;
  66. }
  67. function admin_summary() {
  68. if ($this->is_a_group()) {
  69. return t('grouped');
  70. }
  71. if (!empty($this->options['exposed'])) {
  72. return t('exposed');
  73. }
  74. $info = $this->operators();
  75. $this->get_value_options();
  76. if (!is_array($this->value)) {
  77. return;
  78. }
  79. $operator = check_plain($info[$this->operator]['short']);
  80. $values = '';
  81. if (in_array($this->operator, $this->operator_values(1))) {
  82. // !!! here unlike views_handler_filter_in_operator class.
  83. $options_sids = array();
  84. foreach ($this->value_options as $key => $value) {
  85. if (is_array($value)) {
  86. foreach ($value as $k => $v) {
  87. $options_sids[$k] = $v;
  88. }
  89. }
  90. else {
  91. $options_sids[$key] = $value;
  92. }
  93. }
  94. // Remove every element which is not known.
  95. foreach ($this->value as $value) {
  96. if (!isset($options_sids[$value])) {// !!! Unlike views_handler_filter_in_operator class.
  97. unset($this->value[$value]);
  98. }
  99. }
  100. // Choose different kind of ouput for 0, a single and multiple values.
  101. if (count($this->value) == 0) {
  102. $values = t('Unknown');
  103. }
  104. elseif (count($this->value) == 1) {
  105. // If any, use the 'single' short name of the operator instead.
  106. if (isset($info[$this->operator]['short_single'])) {
  107. $operator = check_plain($info[$this->operator]['short_single']);
  108. }
  109. $keys = $this->value;
  110. $value = array_shift($keys);
  111. if (isset($options_sids[$value])) {// !!! Unlike views_handler_filter_in_operator class.
  112. $values = check_plain($options_sids[$value]);
  113. }
  114. else {
  115. $values = '';
  116. }
  117. }
  118. else {
  119. foreach ($this->value as $value) {
  120. if ($values !== '') {
  121. $values .= ', ';
  122. }
  123. if (drupal_strlen($values) > 8) {
  124. $values .= '...';
  125. break;
  126. }
  127. if (isset($options_sids[$value])) {// !!! Unlike views_handler_filter_in_operator class.
  128. $values .= check_plain($options_sids[$value]);
  129. }
  130. }
  131. }
  132. }
  133. return $operator . (($values !== '') ? ' ' . $values : '');
  134. }
  135. function query() {
  136. $value = $this->is_a_group() && !$this->options['expose']['multiple'] ? drupal_array_merge_deep_array($this->value) : $this->value;
  137. if (empty($value)) {
  138. return;
  139. }
  140. $this->ensure_my_table();
  141. if (count($value) == 1) {
  142. if (current($value) == '') {
  143. $value = NULL;
  144. $this->operator = ($this->operator == 'in') ? 'IS NULL' : 'IS NOT NULL';
  145. }
  146. elseif (current($value) == 'ANY') {
  147. $value = NULL;
  148. $this->operator = ($this->operator == 'in') ? 'IS NOT NULL' : 'IS NULL';
  149. }
  150. else {
  151. $this->operator = ($this->operator == 'in') ? '= ' : '!= ';
  152. }
  153. }
  154. if ($this->operator == 'empty') {
  155. $value = NULL;
  156. $this->operator = 'IS NULL';
  157. }
  158. elseif ($this->operator == 'not empty') {
  159. $value = NULL;
  160. $this->operator = 'IS NOT NULL';
  161. }
  162. $this->query->add_where($this->options['group'], $this->table_alias . '.' . $this->real_field, $value, $this->operator);
  163. }
  164. }