<?php
// $Id$
/**
 * @author Bruno Massa http://drupal.org/user/67164
 * @file charts.module
 * Transform DATA into INFORMATION using beautiful CHARTS.
 *
 * @note only hooks are here.
 * @note For instructions about the API, see chart_api.txt file.
 */

/**
 * The main Chart API function, that calls any chart provider
 * to print the given data.
 *
 * @param $chart_provider
 *   String. The chart provider code. With this module you might install
 *   'google_charts', 'openflashchart' and 'fusioncharts'
 * @param &$data
 *   Array. The chart data, described on chart_api.txt
 * @return
 *   String. The HTML with the propper chart (might include Flash or
 *   JavaScript external files)
 */
function charts_chart($chart_provider, &$data) {
  // Get the previously saved data from Data Base
  $settings = variable_get('charts_settings', array());

  // Merge all series option to the main data array,
  // like color
  foreach (element_children($settings) as $series) {
    if (!empty($data[$series])) {
      $data[$series] = array_merge($settings[$series], $data[$series]);
    }
    unset($settings[$series]);
  }

  // Now, merge all the rest of the options saved by default
  $data = $settings + $data;

  $chart_provider = $chart_provider .'_chartsapi';
  if (function_exists($chart_provider)) {
    return $chart_provider($data);
  }
}

/**
 * Implementation of hook_menu().
 */
function charts_menu() {
  $items['admin/settings/charts'] = array(
    'file'            => 'charts.inc',
    'page callback'   => 'drupal_get_form',
    'page arguments'  => array('_charts_settings'),
    'title'           => 'Charts'
  );
  $items['admin/settings/charts/settings'] = array(
    'page arguments'  => array('_charts_settings'),
    'title'           => 'Settings',
    'type'            => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/settings/charts/testing'] = array(
    'file'            => 'charts.inc',
    'page callback'   => 'drupal_get_form',
    'page arguments'  => array('_charts_testing'),
    'title'           => 'Testing',
    'type'            => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_requirements().
 */
function charts_requirements($phase) {
  if ($phase == 'runtime' and !$modules = module_invoke_all('chartsinfo', 'list')) {
    $requirements['charts']['title']        = t('Charts');
    $requirements['charts']['value']        = t('No Charts provider installed');
    $requirements['charts']['severity']     = REQUIREMENT_ERROR;
    $requirements['charts']['description']  = t('Charts core module only provides a a common set of functions. You must install a Charts provider module to create charts.');

    return $requirements;
  }
}