source: sipes/modules_contrib/views_or/views_or_handler_field.inc @ 6e81fb4

stableversion-3.0
Last change on this file since 6e81fb4 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100644
File size: 2.8 KB
Línea 
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 */
13class 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 */
40class 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 */
85class 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}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.