You are here

public function views_aggregator_plugin_style_table::options_form in Views Aggregator Plus 7

Create the tabular form with the aggregation options.

Overrides views_plugin_style_table::options_form

File

views/views_aggregator_plugin_style_table.inc, line 46
views_aggregator_plugin_style_table.inc

Class

views_aggregator_plugin_style_table
Style plugin to render each item as a row in a table.

Code

public function options_form(&$form, &$form_state) {
  $handlers = $this->display->handler
    ->get_handlers('field');
  $columns = $this
    ->sanitize_columns($this->options['columns']);
  foreach ($columns as $field => $column) {
    if ($field == $column) {

      // Make all columns potentially sortable, including Math Expressions.
      // Do this before calling parent::options_form($form, $form_state).
      $handlers[$field]->definition['click sortable'] = TRUE;
    }
  }

  // Note: bulk of form is provided by superclass views_plugin_style_table.
  parent::options_form($form, $form_state);

  // See function views_aggregator_theme().
  $form['#theme'] = 'views_aggregator_plugin_style_table';

  // Views style of grouping (splitting table into many) interferes, so
  // get rid of the form.
  unset($form['grouping']);
  $form['description_markup'] = array(
    '#markup' => '<div class="description form-item">' . t('Column aggregation functions may be enabled independently of group aggregation functions. Every group aggregation function, except <em>Filter rows (by regexp)</em>, requires exactly <strong>one</strong> field to be assigned the <em>Group and compress</em> function. With that done, select any of the other aggregation functions for some or all of the fields. Functions marked with an asterisk take an optional parameter. For the aggregation functions <em>Enumerate, Range</em> and <em>Tally</em> the optional parameter is a delimiter to separate items. <br/>You may combine multiple fields into the same render column. If you do, the separator specified will be used to separate the fields. You can control column order and field labels in the Fields section of the main configuration page. For the column aggregation function <em>Math expression</em> you may use the tokens from that same page as documented in the "Replacement patterns" in the section "Rewrite the output of this field".') . '</div>',
  );
  foreach ($columns as $field => $column) {
    $form['info'][$field]['has_aggr'] = array(
      '#type' => 'checkbox',
      '#title' => t('Apply group function'),
      '#default_value' => isset($this->options['info'][$field]['has_aggr']) ? $this->options['info'][$field]['has_aggr'] : FALSE,
    );
    $group_options = array();
    $column_options = array();
    foreach (views_aggregator_get_aggregation_functions_info() as $function => $display_names) {
      if (!empty($display_names['group'])) {
        $group_options[$function] = $display_names['group'];
      }
      if (!empty($display_names['column'])) {
        $column_options[$function] = $display_names['column'];
      }
    }
    $form['info'][$field]['aggr'] = array(
      '#type' => 'select',
      '#options' => $group_options,
      '#multiple' => TRUE,
      '#default_value' => empty($this->options['info'][$field]['aggr']) ? array(
        'views_aggregator_first',
      ) : $this->options['info'][$field]['aggr'],
      '#states' => array(
        'visible' => array(
          'input[name="style_options[info][' . $field . '][has_aggr]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );

    // Optional parameter for the selected aggregation function.
    $parameter_label = t('Parameter');
    $form['info'][$field]['aggr_par'] = array(
      '#type' => 'textfield',
      '#size' => 23,
      '#title' => $parameter_label,
      '#default_value' => isset($this->options['info'][$field]['aggr_par']) ? $this->options['info'][$field]['aggr_par'] : '',
      '#states' => array(
        'visible' => array(
          'input[name="style_options[info][' . $field . '][has_aggr]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['info'][$field]['has_aggr_column'] = array(
      '#type' => 'checkbox',
      '#title' => t('Apply column function'),
      '#default_value' => isset($this->options['info'][$field]['has_aggr_column']) ? $this->options['info'][$field]['has_aggr_column'] : FALSE,
    );
    $form['info'][$field]['aggr_column'] = array(
      '#type' => 'select',
      '#options' => $column_options,
      '#multiple' => FALSE,
      '#default_value' => empty($this->options['info'][$field]['aggr_column']) ? 'views_aggregator_sum' : $this->options['info'][$field]['aggr_column'],
      '#states' => array(
        'visible' => array(
          'input[name="style_options[info][' . $field . '][has_aggr_column]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );

    // Optional parameter for the selected column aggregation function.
    $form['info'][$field]['aggr_par_column'] = array(
      '#type' => 'textfield',
      '#size' => 24,
      '#title' => $parameter_label,
      '#default_value' => isset($this->options['info'][$field]['aggr_par_column']) ? $this->options['info'][$field]['aggr_par_column'] : '',
      '#states' => array(
        'visible' => array(
          'input[name="style_options[info][' . $field . '][has_aggr_column]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General aggregation options'),
    '#weight' => -3,
  );
  $form['general']['use_field_handler'] = array(
    '#title' => '<strong>' . t('Use advanced numeric handler') . '</strong>',
    '#type' => 'checkbox',
    '#description' => t('If checked, try to use the advanced options instead of simple numeric rendering.') . '<br/>' . t('<em>Warning:</em> may not work with some numeric fields or settings, so verify that the results are as expected after enabling this option.'),
    '#default_value' => $this->options['general']['use_field_handler'],
  );
  $form['group_aggregation'] = array(
    '#type' => 'fieldset',
    '#title' => t('Group aggregation options'),
    '#weight' => -2,
  );
  $form['group_aggregation']['grouping_field_class'] = array(
    '#title' => t('Grouping field cell class'),
    '#type' => 'textfield',
    '#description' => t('The CSS class to provide on each cell of the column belonging to the field that is being <em>Grouped and compressed</em>.'),
    '#default_value' => $this->options['group_aggregation']['grouping_field_class'],
  );
  $form['column_aggregation'] = array(
    '#type' => 'fieldset',
    '#title' => t('Column aggregation options'),
    '#weight' => -1,
  );
  $form['column_aggregation']['totals_row_position'] = array(
    '#title' => t('Column aggregation row position'),
    '#type' => 'checkboxes',
    '#options' => array(
      1 => t('in the table header'),
      2 => t('in the table footer'),
    ),
    '#default_value' => $this->options['column_aggregation']['totals_row_position'],
  );
  $form['column_aggregation']['totals_per_page'] = array(
    '#title' => t('Column aggregation row applies to'),
    '#type' => 'radios',
    '#options' => array(
      1 => t('the page shown, if a pager is enabled'),
      0 => t('the entire result set'),
    ),
    '#description' => t('If your view does not have a pager, then the two options are equivalent.'),
    '#default_value' => $this->options['column_aggregation']['totals_per_page'],
    '#weight' => 1,
  );
  $form['column_aggregation']['precision'] = array(
    '#title' => t('Column aggregation row default numeric precision'),
    '#type' => 'textfield',
    '#size' => 3,
    '#description' => t('The number of decimals to use for column aggregations whose precisions are not defined elsewhere -- for example aggregations on Views PHP numbers.'),
    '#default_value' => $this->options['column_aggregation']['precision'],
    '#weight' => 2,
  );
  $form['column_aggregation']['totals_row_class'] = array(
    '#title' => t('Column aggregation row class'),
    '#type' => 'textfield',
    '#description' => t('The CSS class to provide on the row containing the column aggregations.'),
    '#default_value' => $this->options['column_aggregation']['totals_row_class'],
    '#weight' => 3,
  );
}