You are here

quant.admin.inc in Quant 7

Admin callbacks

File

quant.admin.inc
View source
<?php

/**
 * @file
 *   Admin callbacks
 */

/**
 * Provide admin settings form
 */
function quant_admin_settings($form, &$form_state) {
  $form['view'] = array(
    '#type' => 'fieldset',
    '#title' => t('View analytics'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['view']['view_link'] = array(
    '#markup' => l(t('Click here to view the analytics page'), 'analytics'),
  );
  $form['display'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['display']['quant_visible'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Show analytics for the following site items'),
    '#default_value' => _quant_quant_option_defaults(),
    '#options' => _quant_quant_options(),
    '#description' => t('The checked objects will be shown on the analytics page. If a quant object is missing from this list, try clearing the cache.'),
  );
  $form['display']['quant_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Chart width'),
    '#default_value' => variable_get('quant_width', 500),
    '#size' => 6,
    '#maxlength' => 4,
    '#required' => TRUE,
    '#description' => t('Specify the graph width in pixels. The Chart API will reject charts that are too wide.'),
  );
  $form['display']['quant_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Chart height'),
    '#default_value' => variable_get('quant_height', 200),
    '#size' => 6,
    '#maxlength' => 4,
    '#required' => TRUE,
    '#description' => t('Specify the graph height in pixels. The Chart API will reject charts that are too tall.'),
  );
  $form['display']['quant_chart'] = array(
    '#type' => 'radios',
    '#title' => t('Chart plugin'),
    '#options' => _quant_chart_options(),
    '#default_value' => variable_get('quant_chart', 'table'),
    '#description' => t('Choose the plugin to render the charts in.'),
  );

  // Add settings for all chart plugins
  foreach (quant_get_quant_charts() as $plugin) {
    if ($chart_form = $plugin->chart
      ->adminSettings()) {
      $form['chart_plugin_options'][$plugin->id] = array(
        '#type' => 'fieldset',
        '#title' => $plugin->name,
        '#description' => $plugin->description,
      );
      $form['chart_plugin_options'][$plugin->id] += $chart_form;
    }
  }
  if (isset($form['chart_plugin_options'])) {
    $form['chart_plugin_options'] += array(
      '#type' => 'fieldset',
      '#title' => t('Chart plugin options'),
      '#collapsed' => TRUE,
      '#collapsible' => TRUE,
    );
  }
  return system_settings_form($form);
}

/**
 * Validate the admin settings form
 */
function quant_admin_settings_validate(&$form, &$form_state) {

  // Check width
  if (!is_numeric($form_state['values']['quant_width']) || strlen($form_state['values']['quant_width']) > 4 || strlen($form_state['values']['quant_width']) < 2) {
    form_set_error('quant_width', t('The width must be a number that is between 2 and 4 digits'));
  }

  // Check height
  if (!is_numeric($form_state['values']['quant_height']) || strlen($form_state['values']['quant_height']) > 4 || strlen($form_state['values']['quant_height']) < 2) {
    form_set_error('quant_height', t('The height must be a number that is between 2 and 4 digits'));
  }

  // Allow chart plugins to validate
  foreach (quant_get_quant_charts() as $plugin) {
    $plugin->chart
      ->adminSettingsValidate($form, $form_state);
  }
}

/**
 * Submit the admin settings form
 */
function quant_admin_settings_submit(&$form, &$form_state) {

  // Allow chart plugins to handle submission
  foreach (quant_get_quant_charts() as $plugin) {
    $plugin->chart
      ->adminSettingsSubmit($form, $form_state);
  }
}

/**
 * Helper function to provide quant options
 */
function _quant_quant_options() {

  // Get available quants
  $options = array();
  $quants = quant_get_quants();
  foreach ($quants as $quant) {
    $options[$quant->id] = $quant->label;
  }
  return $options;
}

/**
 * Helper function to provide quant option defaults
 */
function _quant_quant_option_defaults() {
  $default = array();
  $options = _quant_quant_options();

  // See if visibility settings haven't been set yet
  if (!($default = variable_get('quant_visible', array()))) {
    return array_keys($options);
  }
  else {

    // Iterate the available options to see if there is one that is
    // not present in the visibility options. That can happen if a new
    // quant is available but the settings were set before it was
    foreach ($options as $id => $name) {
      if (!isset($default[$id])) {
        $default[$id] = $id;
      }
    }
  }
  return $default;
}

/**
 * Helper function to provide chart plugin options
 */
function _quant_chart_options() {
  $options = array();
  foreach (quant_get_quant_charts() as $id => $chart) {
    $options[$id] = $chart->name . '<br/>' . $chart->description;
  }
  return $options;
}

Functions

Namesort descending Description
quant_admin_settings Provide admin settings form
quant_admin_settings_submit Submit the admin settings form
quant_admin_settings_validate Validate the admin settings form
_quant_chart_options Helper function to provide chart plugin options
_quant_quant_options Helper function to provide quant options
_quant_quant_option_defaults Helper function to provide quant option defaults