View source
<?php
class views_complex_grouping_style_plugin extends views_plugin_style {
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$field_labels = $this->display->handler
->get_field_labels();
foreach ($form['grouping'] as $index => $info) {
$grouping_fields_default = isset($this->options['grouping'][$index]['grouping-complex']['grouping-fields']) ? $this->options['grouping'][$index]['grouping-complex']['grouping-fields'] : NULL;
$grouping_limit_default = isset($this->options['grouping'][$index]['grouping-complex']['grouping-limit']) ? $this->options['grouping'][$index]['grouping-complex']['grouping-limit'] : 1;
$grouping_offset_default = isset($this->options['grouping'][$index]['grouping-complex']['grouping-offset']) ? $this->options['grouping'][$index]['grouping-complex']['grouping-offset'] : 1;
$form['grouping'][$index]['grouping-complex'] = array(
'#type' => 'fieldset',
'#title' => t('Limit and extra fields for grouping field Nr.!num', array(
'!num' => $index + 1,
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'grouping-fields' => array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Selected'),
'#options' => $field_labels,
'#default_value' => $grouping_fields_default,
'#description' => t('Select which fields will be displayed alongside the field Nr.!num', array(
'!num' => $index + 1,
)),
),
'grouping-limit' => array(
'#type' => 'textfield',
'#title' => t('Items to display:'),
'#default_value' => $grouping_limit_default,
'#size' => 6,
'#element_validate' => array(
'views_complex_grouping_validate',
),
'#description' => t('The number of rows to show under the field Nr.!num. Leave 0 to show all of them.', array(
'!num' => $index + 1,
)),
),
'grouping-offset' => array(
'#type' => 'textfield',
'#title' => t('Offset:'),
'#default_value' => $grouping_offset_default,
'#size' => 6,
'#element_validate' => array(
'views_complex_grouping_validate',
),
'#description' => t('The row to start on.'),
),
);
}
}
function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
if (is_string($groupings)) {
$rendered = $group_rendered === NULL ? TRUE : $group_rendered;
$groupings = array(
array(
'field' => $groupings,
'rendered' => $rendered,
),
);
}
$this
->render_fields($this->view->result);
$sets = array();
if ($groupings) {
foreach ($records as $index => $row) {
$set =& $sets;
foreach ($groupings as $level => $info) {
$field = $info['field'];
$rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
$rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
$grouping = '';
$group_content = '';
if (isset($this->view->field[$field])) {
$group_content = $this
->get_field($index, $field);
if ($this->view->field[$field]->options['label']) {
$group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
}
if ($rendered) {
$grouping = $group_content;
if ($rendered_strip) {
$group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
}
}
else {
$grouping = $this
->get_field_value($index, $field);
if (!is_scalar($grouping)) {
$grouping = md5(serialize($grouping));
}
}
}
if (empty($set[$grouping])) {
$set[$grouping]['group'] = $group_content;
$set[$grouping]['rows'] = array();
$set[$grouping]['level'] = $level;
$set[$grouping]['fields'] = array();
foreach ($this->options['grouping'][$level]['grouping-complex']['grouping-fields'] as $field) {
$set[$grouping]['fields'][$field] = $this->rendered_fields[$index][$field];
}
}
$set =& $set[$grouping]['rows'];
}
$set[$index] = $row;
}
}
else {
$sets[''] = array(
'group' => '',
'rows' => $records,
);
}
if ($group_rendered === NULL) {
$old_style_sets = array();
foreach ($sets as $group) {
$old_style_sets[$group['group']] = $group['rows'];
}
$sets = $old_style_sets;
}
array_walk($sets, array(
$this,
'views_complex_grouping_limit_recursive',
));
return $sets;
}
function render_grouping_sets($sets, $level = 0) {
$output = '';
$branch = 0;
foreach ($sets as $set) {
$branch++;
$row = reset($set['rows']);
if (is_array($row) && isset($row['group'])) {
$output .= theme(views_theme_functions('views_complex_grouping_level', $this->view, $this->display), array(
'view' => $this->view,
'grouping' => $this->options['grouping'][$level],
'grouping_level' => $level + 1,
'grouping_branch' => $branch,
'rows' => $set['rows'],
'fields' => $set['fields'],
'title' => $set['group'],
));
}
else {
if ($this
->uses_row_plugin()) {
foreach ($set['rows'] as $index => $row) {
$this->view->row_index = $index;
$set['rows'][$index] = $this->row_plugin
->render($row);
}
}
$output .= theme($this
->theme_functions(), array(
'view' => $this->view,
'options' => $this->options,
'grouping_level' => $level + 1,
'grouping_branch' => $branch,
'rows' => $set['rows'],
'fields' => $set['fields'],
'title' => $set['group'],
));
}
}
unset($this->view->row_index);
return $output;
}
function views_complex_grouping_limit_recursive(&$group_data, $key = NULL, $level = 1) {
$settings = $this
->views_complex_grouping_settings($level - 1);
$settings['grouping-limit'] = $settings['grouping-limit'] != 0 ? $settings['grouping-limit'] : NULL;
$settings['grouping-offset'] = isset($settings['grouping-offset']) ? $settings['grouping-offset'] : 0;
$group_data['rows'] = array_slice($group_data['rows'], $settings['grouping-offset'], $settings['grouping-limit'], TRUE);
foreach ($group_data['rows'] as &$data) {
if (is_array($data) && isset($data['group']) && isset($data['rows'])) {
$this
->views_complex_grouping_limit_recursive($data, NULL, $level + 1);
}
}
}
function views_complex_grouping_settings($index) {
if ($this->options['grouping'][$index] && $this->options['grouping'][$index]['grouping-complex']) {
return $this->options['grouping'][$index]['grouping-complex'];
}
else {
return array(
'grouping-limit' => 0,
'grouping-offset' => 0,
);
}
}
}
function views_complex_grouping_validate($element, &$form_state) {
if (!is_numeric($element['#value'])) {
form_error($element, t('%element must be numeric.', array(
'%element' => $element['#title'],
)));
}
if ($element['#value'] < 0) {
form_error($element, t('%element cannot be negative.', array(
'%element' => $element['#title'],
)));
}
}