You are here

function flot_fields_views_plugin_style::preprocess in Flot 7

Theme template preprocessor.

File

flot_views/views/flot_fields_views_plugin_style.inc, line 492
Style plugin for views

Class

flot_fields_views_plugin_style
@file Style plugin for views

Code

function preprocess(&$vars) {
  if (!isset($vars['addselectionfilter'])) {
    $vars['addselectionfilter'] = FALSE;
  }

  // Get flot fields, and bail if non present.
  $fields = $this->display->handler->handlers['field'];
  $fieldx = NULL;
  $group_fields = array();
  switch (get_class($this->view->query)) {
    case 'views_plugin_query_default':
      foreach ($fields as $fieldname => $field) {
        if ($field->options['flot']['axis'] == 'x') {
          $fieldx = $field;
        }
        elseif (isset($field->options['group_type']) && $field->options['group_type'] == 'group') {
          $group_fields[] = $field;
        }
      }
      break;
    case 'SearchApiViewsQuery':
      foreach ($fields as $fieldname => $field) {
        if ($field->options['flot']['axis'] == 'x') {
          $fieldx = $field;
        }
        elseif ($field->options['flot']['axis'] == 'y' && isset($field->options['flot']['y']['function']) && $field->options['flot']['y']['function'] == 'label') {
          $group_fields[] = $field;
        }
      }
      break;
    default:
      drupal_set_message('Unsupported query type: ' . get_class($this->view->query));
      break;
  }
  if (is_null($fieldx)) {
    return;
  }
  $yfields = array();
  foreach ($fields as $fieldname => $field) {
    if ($field->options['flot']['axis'] == 'y') {
      $yfields[] = $field;
    }
  }

  // min / max values
  $view = $this->view;
  $options = $this->options;
  $filters = $this->display->handler->handlers['filter'];

  // Parameters
  $size = !empty($options['size']) ? explode('x', $options['size']) : array(
    '200',
    '100',
  );
  if (!$vars['addselectionfilter'] && $options['zoomable']) {
    $vars['addselectionfilter'] = TRUE;
  }

  // DOM element options
  // By default no zoom selection
  $element = array();
  $element['style'] = is_numeric($size[0]) ? "width:{$size[0]}px;" : "width:{$size[0]};";
  $vars['flotwidth'] = $size[0];
  $element['style'] .= is_numeric($size[1]) ? "height:{$size[1]}px;" : "height:{$size[1]};";
  $selectionfilter_added = FALSE;
  foreach ($filters as $filter) {
    if ($filter->options['field'] == $fieldx->options['field'] && $filter->options['table'] == $fieldx->options['table']) {
      if ($filter->options['exposed'] && $filter->options['operator'] == 'between') {
        $vars['addselectionfilter'] = TRUE;
        $element['class'] = 'has-inbetween-exposed-filter';
        drupal_add_js(array(
          'flot' => array(
            'field_name' => $filter->options['expose']['identifier'],
          ),
        ), 'setting');
        drupal_add_js(drupal_get_path('module', 'flot_views') . '/views/flot_hide_exposed_form.js', array(
          'scope' => 'footer',
          'weight' => 4,
        ));
        drupal_add_css(drupal_get_path('module', 'flot') . '/flot.legend.css');
        $selectionfilter_added = TRUE;
        break;
      }
    }
  }
  $vars['element'] = $element;

  // input
  $input = array(
    'fieldx' => $fieldx,
    'yfields' => $yfields,
    'group_fields' => $group_fields,
  );

  // output
  $output = array();
  switch (get_class($this->view->query)) {
    case 'views_plugin_query_default':
      $this
        ->preprocess_views_plugin_query_default($vars, $input, $output);
      break;
    case 'SearchApiViewsQuery':
      $this
        ->preprocess_SearchApiViewsQuery($vars, $input, $output);
      break;
    default:
      drupal_set_message('Unsupported query type: ' . get_class($this->view->query));
      break;
  }
  $series = $output['series'];
  $series_options = $output['series_options'];
  $range = $output['range'];
  $ticks = $output['ticks'];
  $flotseries = array();
  $style = new flotStyle();

  // Convert to flot compatible format
  foreach ($series as $s => $serie) {
    $flotdata = new flotData($serie);
    $settings = array(
      'color' => $series_options[$s]['color'],
    );
    switch ($series_options[$s]['type']) {
      case 'point':
        $flotdata->points = new flotPoint($settings);
        break;
      case 'bar':
        $flotdata->bars = new flotBar($settings);
        break;
      case 'pie':
        $style
          ->createPie();
        break;
      case 'line':
      default:
        $flotdata->lines = new flotLine($settings);
        $flotdata->points = new flotPoint();
        break;
    }
    if (isset($options['yaxis']['label']) && $options['yaxis']['label'] != '') {
      $flotdata->label = $s;
    }
    $flotseries[] = $flotdata;
  }
  $vars['data'] = $flotseries;

  // cache the zoom series to always include the full range
  // not stable, need better solution
  if ($vars['addselectionfilter']) {
    $cid = 'views_flot:' . $this->view->name . ':' . $this->view->current_display;
    $cache = cache_get($cid);
    if ($cache && isset($cache->data) && !$this->rebuild_zoom_flot) {
      $zoomflotseries = $cache->data;
    }
    else {
      $zoomflotseries = $flotseries;
      cache_set($cid, $zoomflotseries, 'cache', CACHE_PERMANENT);
    }
    $vars['zoomdata'] = $zoomflotseries;
  }
  foreach (array(
    'xaxis',
    'yaxis',
  ) as $axis) {
    if (!isset($style->{$axis})) {
      $style->{$axis} = new stdClass();
    }
    $style->{$axis}->show = $options['show' . $axis] ? TRUE : FALSE;
    if ($style->{$axis}->show) {
      switch ($options[$axis]['granularity']) {
        case 'endpoints':
          $ticks = array(
            $range[$axis]['min'],
            $range[$axis]['max'],
          );
          $style
            ->axis_ticks($axis, $ticks);
          break;
        case 'auto':
          $style
            ->axis_range($axis, $range[$axis]);
          break;
        default:
          $style
            ->axis_range($axis, $range[$axis], $options[$axis]['granularity']);
          break;
      }
    }
    foreach ($options[$axis] as $key => $val) {
      if (!empty($val) && $val !== 0) {
        $style->{$axis}->{$key} = $val;
      }
    }
  }
  if ($fieldx->options['flot']['x']['mode'] == 'time') {
    $style->xaxis->mode = 'time';
  }
  if (!isset($style->legend)) {
    $style->legend = new stdClass();
  }
  $style->legend->show = !isset($options['showlegend']) || $options['showlegend'] == 'yes' ? TRUE : FALSE;
  if ($style->legend->show) {
    foreach ($options['legend'] as $key => $val) {
      if (!empty($val) && $val !== 0) {
        $style->legend->{$key} = $val;
      }
    }
    if (isset($style->legend->margin)) {
      $style->legend->margin = array(
        $style->legend->margin['x'],
        $style->legend->margin['y'],
      );
    }
  }
  $style->grid->hoverable = $options['hoverable'] ? TRUE : FALSE;
  $style->shadowSize = $options['shadowSize'];
  $vars['options'] = $style;
}