| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: views_groupby_handler_field_groupfields.inc,v 1.4.2.3 2009/10/23 20:43:33 inadarei Exp $ 3 4 /** @file 5 * Group By Views Field Handler Implementation 6 */ 7 8 /** 9 * Field handler 10 */ 11 class views_groupby_handler_field_groupfields extends views_handler_field { 12 /** 13 * Constructor to provide additional field to add. 14 * 15 * This constructer assumes the calais_term table. If using another 16 * table, we'll need to be more specific. 17 */ 18 function construct() { 19 parent::construct(); 20 // $this->additional_fields['nid'] = 'nid'; 21 22 // $this->additional_fields['tdid'] = 'tdid'; 23 // $this->additional_fields['vid'] = 'vid'; 24 } 25 26 /** function option_definition() { 27 $options = parent::option_definition(); 28 $options['link_to'] = array('default' => FALSE); 29 return $options; 30 } 31 **/ 32 33 /** 34 * Provide link to taxonomy option 35 */ 36 function options_form(&$form, &$form_state) { 37 parent::options_form($form, $form_state); 38 39 unset($form['label']); 40 unset($form['alter']); 41 42 // Make the this utility field invisible ;) 43 $form['exclude']['#type'] = 'hidden'; 44 $form['exclude']['#default_value'] = '1'; 45 46 // dpm($this->view); 47 48 $curr_disp = $this->view->current_display; 49 $options_fields = $this->_get_views_fields(); 50 51 $def_val = $this->options['views_groupby_fields_to_group']; 52 $def_val = (empty($def_val) || !is_array($def_val)) ? array(t('-- None --')) : $def_val; 53 54 $form['views_groupby_fields_to_group'] = array( 55 '#title' => t('Fields to Group On'), 56 '#type' => 'select', 57 '#options' => $options_fields, 58 '#multiple' => TRUE, 59 '#required' => TRUE, 60 '#description' => t('Select fields to group by. Attention: You need to first select these fields as Views Fields!'), 61 '#default_value' => $def_val, 62 ); 63 64 65 $options_sql_func = array( 'count' => t('Count') ); 66 $form['views_groupby_sql_function'] = array( 67 '#title' => t('SQL Aggregation Function'), 68 '#type' => 'select', 69 '#options' => $options_sql_func, 70 '#multiple' => FALSE, 71 '#required' => TRUE, 72 '#description' => t('Please select SQL aggregation function to use with grouping. Only \'COUNT\' is supported currently'), 73 '#default_value' => $this->options['views_groupby_sql_function'], 74 ); 75 76 $def_val2 = $this->options['views_groupby_fields_to_aggregate']; 77 $def_val2 = (empty($def_val2) || !is_array($def_val2)) ? array(t('-- None --')) : $def_val2; 78 79 $form['views_groupby_fields_to_aggregate'] = array( 80 '#title' => t('Fields to Aggregate with the SQL function'), 81 '#type' => 'select', 82 '#options' => $options_fields, 83 '#multiple' => TRUE, 84 '#required' => TRUE, 85 '#description' => t('Attention: in ANSI SQL you may not select fields that are used in grouping! If you do, Views will ignore them anyway'), 86 '#default_value' => $def_val2, 87 ); 88 89 $def_val3 = $this->options['views_groupby_field_sortby']; 90 $def_val3 = (empty($def_val3)) ? '' : $def_val3; 91 92 $form['views_groupby_field_sortby'] = array( 93 '#title' => t('Field to Sort Resultset On (after SQL Function is applied)'), 94 '#type' => 'select', 95 '#options' => $options_fields, 96 '#multiple' => FALSE, 97 '#required' => TRUE, 98 '#description' => t('Attention: usual Views sorting is ignored when you are using SQL aggregation'), 99 '#default_value' => $def_val3, 100 ); 101 102 $options_order_direction = array( 'asc' => t('Ascending'), 'desc' => t('Descending') ); 103 $form['views_groupby_sortby_direction'] = array( 104 '#title' => t('Sorting Direction'), 105 '#type' => 'select', 106 '#options' => $options_order_direction, 107 '#multiple' => FALSE, 108 '#required' => TRUE, 109 //'#description' => t('Please select SQL aggregation function to use with grouping. Only \'COUNT\' is supported currently'), 110 '#default_value' => $this->options['views_groupby_sortby_direction'], 111 ); 112 113 114 } 115 116 function _get_views_fields($just_aliases=FALSE) { 117 118 $curr_disp = $this->view->current_display; 119 $handlers = $this->view->display[$curr_disp]->handler->get_handlers('field'); 120 121 $avail_fields = array(); 122 if (is_array($handlers)) { 123 foreach ($handlers as $key => $val) { 124 if ($key == 'views_sql_groupedfields') continue; // May not select ourseleves! 125 126 $unique_id = $val->options['id']; 127 128 if ($just_aliases) { 129 $avail_fields[] = $val->field_alias; 130 continue; 131 } 132 133 $field_alias = $val->options['table'] . '_' . $val->options['field']; 134 $relationship = ''; 135 136 $all_relationships = $this->_get_relationships (); 137 $rel = $val->options['relationship']; 138 $rel = $all_relationships[$rel]; 139 140 if (!empty($rel)) { 141 $field_name = $rel->fieldprefix . '.' . $val->options['field']; // reserved, not used 142 $field_alias = $rel->fieldprefix . '_' . $val->options['field']; 143 $relationship = (empty($rel)) ? '' : '[' . $rel->label . '] '; 144 } 145 146 $field_name = $relationship . $val->definition['group'] . ': ' . $val->definition['title']; 147 $avail_fields[$unique_id] = $field_name; 148 149 } 150 } 151 152 return $avail_fields; 153 } 154 155 function _get_relationships() { 156 $default_rels = array(); 157 $curr_disp_rels = array(); 158 159 $curr_disp = $this->view->current_display; 160 161 $default_rels = $this->view->display['default']->handler->options['relationships']; 162 $curr_displ_rels = $this->view->display[$curr_disp]->handler->options['relationships']; 163 164 $default_rels = (is_array($default_rels)) ? $default_rels : array(); 165 $curr_displ_rels = (is_array($curr_displ_rels)) ? $curr_displ_rels : array(); 166 167 $relationships = array_merge($default_rels, $curr_displ_rels); 168 169 $all_rels = array(); 170 $base_table = $this->view->base_table; 171 172 if (is_array($relationships)) { 173 foreach ($relationships as $key => $val) { 174 $obj = new stdClass(); 175 $obj->fieldprefix = $base_table . '_' . $val['table']; 176 $obj->label = $val['label']; 177 $obj->table = $val['table']; 178 $obj->field = $val['field']; 179 $all_rels[$key] = $obj; 180 } 181 } 182 183 return $all_rels; 184 185 } 186 187 /** 188 * This may only be called from query() function because field aliases 189 * are not generated prior to that in the lifecycle (e.g. in the options_form function). 190 */ 191 function _unique_to_alias($unique_id) { 192 193 $curr_disp = $this->view->current_display; 194 $handlers = $this->view->display[$curr_disp]->handler->get_handlers('field'); 195 196 return $handlers[$unique_id]->field_alias; 197 } 198 199 /** 200 * When settings arrays are saved by option forms, field aliases are not 201 * yet available, in the Views lifecycle, so we save settings using "id" 202 * fields. Before we can use them in a query, we have to transform back 203 * to sql aliases. This is what this function does. 204 */ 205 function _fix_settings_array($from_array) { 206 207 $to_array = NULL; 208 if (is_array($from_array)) { 209 $to_array = array(); 210 foreach ($from_array as $val) { 211 $to_val = $this->_unique_to_alias($val); 212 $to_array[] = $to_val; 213 } 214 } else if (!empty($from_array)) { // if single value 215 $to_array = array(); 216 $to_array[] = $this->_unique_to_alias($from_array); 217 } 218 219 return $to_array; 220 } 221 222 /** 223 * Rewrite query to include aggregation 224 */ 225 function query() { 226 227 $to_group = $this->options['views_groupby_fields_to_group']; 228 $to_group = $this->_fix_settings_array($to_group); 229 $to_aggregate = $this->options['views_groupby_fields_to_aggregate']; 230 $to_aggregate = $this->_fix_settings_array($to_aggregate); 231 232 $sql_func = $this->options['views_groupby_sql_function']; 233 $orderby = $this->options['views_groupby_field_sortby']; 234 $orderby = $this->_unique_to_alias($orderby); 235 $orderby_dir = $this->options['views_groupby_sortby_direction']; 236 237 /** If group_by or to_aggregate lists are empty nothing 238 * should be done. 239 **/ 240 if ( (!is_array($to_group) || sizeof($to_group)<1) || 241 (!is_array($to_aggregate) || sizeof($to_aggregate)<1) ) { 242 return; 243 } 244 245 /** 246 * Prevent the default behaviour of including the primary field in 247 * groupying by clause. That defeats the purpose of grouping since 248 * primary field is unique on almost every row. 249 */ 250 //$btable = $this->view->base_table; 251 //$bfield = $this->view->base_field; 252 //$this->query->add_field($btable, $bfield, $bfield, array('count' => TRUE)); 253 254 if ( is_array($to_group) ) { 255 $sz_to_group = ''; $count = 0; 256 foreach ($to_group as $field) { 257 if ($count) { 258 $sz_to_group .= ', '; 259 } 260 $count++; 261 $sz_to_group .= $field; 262 } 263 $this->query->add_groupby($sz_to_group); 264 } 265 266 $views_fields = $this->_get_views_fields($just_aliases=TRUE); 267 268 /** Add all other fields into count, to make sure they do not mess up group_by **/ 269 if (is_array($this->query->fields)) { 270 foreach ($this->query->fields as $key => $field) { 271 if ( !in_array($key, $to_group) && 272 !in_array($key, $to_aggregate) ) { 273 274 //if ($key != $bfield) { 275 // unset($this->query->fields[$key]); 276 //} 277 } 278 else if (in_array($key, $to_aggregate)) { 279 $this->query->fields[$key][$sql_func] = TRUE; 280 $this->query->fields[$key]['alias'] = $key; 281 } 282 } 283 } 284 285 //dpm($this->query); 286 //----$this->query->fields[$bfield]['count'] = true; 287 //----$this->query->add_field($btable, $bfield, $btable . '_' . $bfield, array('count' => TRUE)); 288 289 $this->query->add_orderby(NULL, NULL, $orderby_dir, $orderby); 290 } 291 292 function render($values) { 293 // dpm($values); 294 return 'test'; 295 } 296 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |