[177a560] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * @file |
---|
| 5 | * Field classes. |
---|
| 6 | * |
---|
| 7 | * These classes are always used together, so we keep them in the same file. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Base class for Views Or field handlers. |
---|
| 12 | */ |
---|
| 13 | class views_or_handler_field extends views_handler_field { |
---|
| 14 | /** |
---|
| 15 | * Set default values for form elements. |
---|
| 16 | */ |
---|
| 17 | function option_definition() { |
---|
| 18 | $options['exclude'] = array('default' => TRUE); |
---|
| 19 | return $options; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * Remove unnecessary form elements. |
---|
| 24 | */ |
---|
| 25 | function options_form(&$form, &$form_state) { |
---|
| 26 | $form['relationship']['#access'] = FALSE; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Remove advanced rendering options from form. |
---|
| 31 | */ |
---|
| 32 | function allow_advanced_render() { |
---|
| 33 | return FALSE; |
---|
| 34 | } |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Field handler to start a group of alternatives. |
---|
| 39 | */ |
---|
| 40 | class views_or_handler_field_begin_alternatives extends views_or_handler_field { |
---|
| 41 | /** |
---|
| 42 | * Save existing fields and start a new field group. |
---|
| 43 | */ |
---|
| 44 | function query() { |
---|
| 45 | $base = $this->query->base_field; |
---|
| 46 | $fields = $this->query->fields; |
---|
| 47 | if (isset($fields[$base])) { |
---|
| 48 | $this->query->fields = array($base => $fields[$base]); |
---|
| 49 | unset($fields[$base]); |
---|
| 50 | } |
---|
| 51 | else { |
---|
| 52 | $this->query->fields = array(); |
---|
| 53 | } |
---|
| 54 | $this->query->views_or = $fields; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Replace individual fields in the output with the COALESCE group. |
---|
| 59 | */ |
---|
| 60 | function pre_render($values) { |
---|
| 61 | $coalesce = FALSE; |
---|
| 62 | foreach ($this->view->field as $id => $field) { |
---|
| 63 | if ($field->field == 'views_or_begin') { |
---|
| 64 | $coalesce = TRUE; // We are in a COALESCE group. |
---|
| 65 | $first = FALSE; // This is not the first field in the group. |
---|
| 66 | } |
---|
| 67 | if ($coalesce) { |
---|
| 68 | if (!$first) { |
---|
| 69 | // Remove all fields except the first. |
---|
| 70 | unset($this->view->field[$id]); |
---|
| 71 | } |
---|
| 72 | // The next field will be the first in the group. |
---|
| 73 | $first = $field->field == 'views_or_begin' ? TRUE : FALSE; |
---|
| 74 | } |
---|
| 75 | if ($field->field == 'views_or_end') { |
---|
| 76 | $coalesce = FALSE; // We have reached the end of the COALESCE group. |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Field handler to end a group of alternatives. |
---|
| 84 | */ |
---|
| 85 | class views_or_handler_field_end_alternatives extends views_or_handler_field { |
---|
| 86 | /** |
---|
| 87 | * Replace individual fields in the query with the COALESCE group. |
---|
| 88 | */ |
---|
| 89 | function query() { |
---|
| 90 | $base = $this->query->base_field; |
---|
| 91 | $fields = $this->query->fields; |
---|
| 92 | if (isset($fields[$base])) { |
---|
| 93 | $this->query->fields = array($base => $fields[$base]); |
---|
| 94 | unset($fields[$base]); |
---|
| 95 | } |
---|
| 96 | $this->query->fields = array_merge($this->query->fields, $this->query->views_or); |
---|
| 97 | if (!empty($fields)) { |
---|
| 98 | $coalesce = array(); |
---|
| 99 | $alias = NULL; |
---|
| 100 | foreach ($fields as $field) { |
---|
| 101 | $coalesce[] = "$field[table].$field[field]"; |
---|
| 102 | $alias = isset($alias) ? $alias : $field['alias']; |
---|
| 103 | } |
---|
| 104 | $this->query->add_field(NULL, 'COALESCE('. implode(', ', $coalesce) .')', $alias); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | } |
---|