You are here

function views_crosstab_table::options_form in Views Crosstab 7

Same name and namespace in other branches
  1. 6 views_crosstab_table.inc \views_crosstab_table::options_form()

Render the options form.

Parameters

array $form: Reference to the form array we're building.

array $form_state: Reference to the form state.

Overrides views_plugin_style_table::options_form

File

plugins/views_crosstab_table.inc, line 42
Plugin functions

Class

views_crosstab_table
Style plugin to transform a linear query into a crosstab table.

Code

function options_form(&$form, &$form_state) {
  parent::options_form($form, $form_state);
  $handlers = $this->display->handler
    ->get_handlers('field');
  $columns = $this
    ->sanitize_columns($this->options['columns']);
  $defaults_select = array_keys($columns);
  if (count($defaults_select) < 3) {
    drupal_set_message(t('At least 3 fields need to be added to your view to create a crosstab.'), 'error');
    return;
  }
  $crosstab_rows_default = array_shift($defaults_select);
  $crosstab_columns_default = array_shift($defaults_select);
  $crosstab_data_default = array_shift($defaults_select);

  // Define theme function to handling options format format.
  $form['#theme'] = 'views_crosstab_ui_table';
  foreach ($columns as $field => $column) {
    $safe = str_replace(array(
      '][',
      '_',
      ' ',
    ), '-', $field);
    $id = 'edit-style-options-columns-' . $safe;

    // Collect the row, column and data fields.
    // TODO: JS helper.
    $radio_id = drupal_html_id('edit-crosstab-rows-' . $field);
    $form['crosstab_rows'][$field] = array(
      '#type' => 'radio',
      '#return_value' => $field,
      '#parents' => array(
        'style_options',
        'crosstab_rows',
      ),
      '#id' => $radio_id,
      '#attributes' => array(
        'id' => $radio_id,
      ),
      '#default_value' => isset($this->options['crosstab_rows']) ? $this->options['crosstab_rows'] : $crosstab_rows_default,
      '#dependency' => array(
        $id => array(
          $field,
        ),
      ),
    );
    $radio_id = drupal_html_id('edit-crosstab-columns-' . $field);
    $form['crosstab_columns'][$field] = array(
      '#type' => 'radio',
      '#return_value' => $field,
      '#parents' => array(
        'style_options',
        'crosstab_columns',
      ),
      '#id' => $radio_id,
      '#attributes' => array(
        'id' => $radio_id,
      ),
      '#default_value' => isset($this->options['crosstab_columns']) ? $this->options['crosstab_columns'] : $crosstab_columns_default,
      '#dependency' => array(
        $id => array(
          $field,
        ),
      ),
    );
    $radio_id = drupal_html_id('edit-crosstab-data-' . $field);
    $form['crosstab_data'][$field] = array(
      '#type' => 'radio',
      '#return_value' => $field,
      '#parents' => array(
        'style_options',
        'crosstab_data',
      ),
      '#id' => $radio_id,
      '#attributes' => array(
        'id' => $radio_id,
      ),
      '#default_value' => isset($this->options['crosstab_data']) ? $this->options['crosstab_data'] : $crosstab_data_default,
      '#dependency' => array(
        $id => array(
          $field,
        ),
      ),
    );
  }

  // Aggregation operators to use on the data.
  $form['crosstab_operation'] = array(
    '#type' => 'select',
    '#title' => 'Crosstab operation',
    '#options' => array(
      'COUNT' => 'Count',
      'SUM' => 'Sum',
      'AVG' => 'Average',
      'MIN' => 'Minimum',
      'MAX' => 'Maximum',
    ),
    '#default_value' => $this->options['crosstab_operation'],
    '#weight' => -5,
  );
  $form['include_crosstab_operation_on_row'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add an extra column for aggregating entire row'),
    '#default_value' => $this->options['include_crosstab_operation_on_row'],
    '#weight' => -4,
  );
  $form['include_crosstab_operation_on_column'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add an extra row for aggregating each column'),
    '#default_value' => $this->options['include_crosstab_operation_on_column'],
    '#weight' => -4,
  );

  // Decimals for the functions.
  $form['crosstab_decimals'] = array(
    '#type' => 'select',
    '#title' => 'Round to N number of decimals.',
    '#options' => array(
      0,
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
    ),
    '#default_value' => $this->options['crosstab_decimals'],
    '#weight' => -3,
  );
}