5bdfa17f97f1c9d719ee51bfba54805c2679cf42
[project/cck.git] / modules / content_multigroup / views / handlers / content_multigroup_handler_filter.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Defines a content_multigroup filter object in views.
7 *
8 * This filter allows you to associate the deltas in a multigroup so that
9 * views doesn't do a cross-join across all values in multivalue fields.
10 */
11 class content_multigroup_handler_filter extends views_handler_filter {
12 var $content_multigroup_fields;
13
14 function can_expose() {
15 return FALSE;
16 }
17
18 function admin_summary() {
19 return t('+ delta');
20 }
21
22 /**
23 * Get information about the multigroup.
24 */
25 function _get_multigroup() {
26 $groups = fieldgroup_groups($this->definition['content_type_name']);
27 return $groups[$this->definition['content_group_name']];
28 }
29
30 /**
31 * Get information about the fields in this multigroup.
32 */
33 function _get_multigroup_fields() {
34 if (!isset($this->content_multigroup_fields)) {
35 $group = $this->_get_multigroup();
36 $this->content_multigroup_fields = array();
37 foreach (array_keys($group['fields']) as $field_name) {
38 $field = content_fields($field_name, $this->definition['content_type_name']);
39 $table_alias = content_views_tablename($field);
40 $this->content_multigroup_fields[$table_alias] = $field;
41 }
42 }
43 return $this->content_multigroup_fields;
44 }
45
46 /**
47 * Define default value for master field.
48 */
49 function options(&$options) {
50 $multigroup_fields = $this->_get_multigroup_fields();
51 // Find the first required field.
52 foreach ($multigroup_fields as $table_alias => $field) {
53 if ($field['required']) {
54 $options['content_multigroup_master_field'] = $table_alias;
55 break;
56 }
57 }
58 // Default to first field in the multigroup.
59 if (empty($options['content_multigroup_master_field'])) {
60 $options['content_multigroup_master_field'] = current(array_keys($multigroup_fields));
61 }
62 }
63
64 /**
65 * Options from to ask the user for a master field.
66 */
67 function options_form(&$form, &$form_state) {
68 $group = $this->_get_multigroup();
69 $options = array();
70 foreach ($this->_get_multigroup_fields() as $table_alias => $field) {
71 $options[$table_alias] = t($field['widget']['label']);
72 }
73 $form['content_multigroup_master_field'] = array(
74 '#title' => t('Available fields in @group_label multigroup', array('@group_label' => t($group['label']))),
75 '#type' => 'radios',
76 '#options' => $options,
77 '#default_value' => $this->options['content_multigroup_master_field'],
78 '#description' => t('Select the field in this multigroup that will be used to build the primary join with the content table.'),
79 );
80 }
81
82 /**
83 * Add joins to the query to synchronize the fields in this multigroup.
84 */
85 function query() {
86 // Create the join between the master field table and the node table.
87 $base_alias = $this->query->ensure_table($this->options['content_multigroup_master_field'], $this->relationship);
88
89 // Now we want to join the master field table with all other tables
90 // related to fields in the same multigroup, but adding the delta
91 // key to the join condition. This is what allows us to keep delta
92 // values in sync for all fields in the same multigroup.
93 foreach ($this->_get_multigroup_fields() as $table_alias => $field) {
94 if ($table_alias != $this->options['content_multigroup_master_field']) {
95 $alias = $this->query->ensure_table($table_alias, $this->relationship);
96 $this->query->table_queue[$table_alias]['join']->extra = $base_alias .'.delta = '. $alias .'.delta';
97 }
98 }
99 }
100 }