You are here

quant.pages.inc in Quant 7

Page callbacks

File

quant.pages.inc
View source
<?php

/**
 * @file
 *   Page callbacks
 */

/**
 * The analytics page callback
 */
function quant_page() {
  $charts = array();

  // Get time period or interval
  $period = _quant_get_time_from_url();

  // Fetch all available quants
  if ($period) {
    $quants = quant_get_quants();
    foreach ($quants as $quant) {

      // Check to see if this quant should be shown
      if ($allowed = variable_get('quant_visible', array())) {
        if (isset($allowed[$quant->id]) && !$allowed[$quant->id]) {
          continue;
        }
      }

      // Process the quant
      $quant
        ->execute($period);
      $charts[] = $quant
        ->render();
    }
  }
  else {
    drupal_set_message(t('Invalid timeframe arguments provided.'), 'error');
  }

  // Get the time form
  $form = drupal_get_form('quant_time_form');

  // Theme and return the page
  $build = array(
    '#theme' => 'quant_page',
    '#form' => drupal_render($form),
    '#charts' => $charts,
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'quant') . '/css/quant.css',
      ),
    ),
  );
  return $build;
}

/**
 * Retrieve the time period or interval from the URL
 * 
 * @param $period
 *   If the set option is a period, the return value will
 *   be a single timestamp representing how far to go back
 *   in time from the current. If the set option is custom,
 *   the return value will be an array representing a given
 *   time interval. The first value will be the "from" timestamp
 *   and the second value will be the "to" timestamp. If neither
 *   can be determined, FALSE will be returned. If no queries are
 *   in the URL, a timestamp for a 1 month period will be returned.
 */
function _quant_get_time_from_url() {

  // Get the option
  if (isset($_GET['option'])) {
    switch ($_GET['option']) {
      case 'custom':
        $from = strtotime($_GET['from']);
        $to = strtotime($_GET['to']);
        if (is_numeric($from) && is_numeric($to)) {

          // Move the 'to' date to 1 second before midnight
          return array(
            $from,
            $to + 86399,
          );
        }
        break;
      case 'period':
        $period = strtotime('-' . str_replace('_', ' ', filter_xss($_GET['period'])));
        if (is_numeric($period)) {
          return $period;
        }
    }
  }
  else {
    return strtotime('-1 month');
  }
  return FALSE;
}

/**
 * Provide a simple form to change time periods
 */
function quant_time_form($form, &$form_state) {
  $period_options = array(
    '1_week' => t('1 week'),
    '2_weeks' => t('2 weeks'),
    '1_month' => t('1 month'),
    '3_months' => t('3 months'),
    '6_months' => t('6 months'),
    '1_year' => t('1 year'),
    '2_years' => t('2 years'),
  );
  $period = isset($_GET['period']) ? filter_xss($_GET['period']) : NULL;
  if (!$period || !array_key_exists($period, $period_options)) {
    $period = '1_month';

    // Default value
  }
  $form['message'] = array(
    '#type' => 'item',
    '#value' => t('Select a timeframe and click Update to see what\'s happening on your site during that time, as well as a total for all the activity during that timeframe.'),
  );
  $form['option'] = array(
    '#type' => 'radios',
    '#options' => array(
      'period' => '',
      'custom' => '',
    ),
    '#default_value' => isset($_GET['option']) ? filter_xss($_GET['option']) : 'period',
  );
  $form['period'] = array(
    '#type' => 'select',
    '#options' => $period_options,
    '#default_value' => $period,
  );
  $form['custom_from'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => isset($_GET['from']) ? filter_xss($_GET['from']) : '',
  );
  $form['custom_to'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => isset($_GET['to']) ? filter_xss($_GET['to']) : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  $form['#attached'] = array(
    'library' => array(
      array(
        'system',
        'ui.datepicker',
      ),
    ),
    'js' => array(
      drupal_get_path('module', 'quant') . '/js/quant-form.js',
    ),
  );
  return $form;
}

/**
 * Validate handler for quant_time_form()
 */
function quant_time_form_validate(&$form, &$form_state) {
  $values = $form_state['values'];

  // Make sure a time option is checked
  if (!$values['option']) {
    form_set_error('error', t('An option must be selected'));
  }

  // If custom option, make sure we have both dates
  if ($values['option'] == 'custom' && !($values['custom_from'] && $values['custom_to'])) {
    form_set_error('option', t('You must specify both dates'));
  }
  else {
    if ($values['option'] == 'custom') {

      // Convert the times
      $now = time();
      $from = strtotime($values['custom_from']);
      $to = strtotime($values['custom_to']);

      // Make sure from date exists
      if (!$from) {
        form_set_error('custom_from', t('The from date must be formatted correctly %format.', array(
          '%format' => '(MM/DD/YY)',
        )));
      }

      // Make sure to date exists
      if (!$to) {
        form_set_error('custom_to', t('The to date must be formatted correctly %format.', array(
          '%format' => '(MM/DD/YY)',
        )));
      }

      // Make sure from is less than to
      if ($from > $to) {
        form_set_error('custom_from', t('The from date must be before the to date'));
      }

      // Make sure to date is not past current time
      if ($to > $now) {
        form_set_error('custom_to', t('The to date must not be past today'));
      }
    }
  }
}

/**
 * Submit handler for quant_time_form()
 */
function quant_time_form_submit(&$form, &$form_state) {

  // Add a query to the URL which will be used to determine the query
  switch ($form_state['values']['option']) {
    case 'period':
      $form_state['redirect'] = array(
        current_path(),
        array(
          'query' => array(
            'option' => 'period',
            'period' => $form_state['values']['period'],
          ),
        ),
      );
      break;
    case 'custom':
      $form_state['redirect'] = array(
        current_path(),
        array(
          'query' => array(
            'option' => 'custom',
            'from' => $form_state['values']['custom_from'],
            'to' => $form_state['values']['custom_to'],
          ),
        ),
      );
      break;
  }
}

Functions

Namesort descending Description
quant_page The analytics page callback
quant_time_form Provide a simple form to change time periods
quant_time_form_submit Submit handler for quant_time_form()
quant_time_form_validate Validate handler for quant_time_form()
_quant_get_time_from_url Retrieve the time period or interval from the URL