<?php

/**
 * @file
 * Provides the gRaphaël JavaScript vector graph/chart library to other modules.
 *
 * This module does not do a lot on its own, but it is a centralised way 
 * to load the gRaphaël files for use by multiple modules, and may in 
 * time grow to have helper functions for using gRaphaël on your page.
 */


/**
 * Load gRaphaël on a page.
 *
 * @param mixed $files
 *   Array of file names to load, one or more of bar, hbar, dot, line, pie.
 *   A single file name can be passed as a string.
 *   Whatever the name, the core file (g.raphael.js) will always be loaded.
 * @param boolean $minified
 *   (optional) Whether to load the minified or the uncompressed version. Default
 *   is TRUE for loading the minified version.
 * @param string $scope
 *   (optional) The location in which you want to place the script. Possible
 *   values are 'header' and 'footer' by default. If your theme implements
 *   different locations, however, you can also use these.
 * @param boolean $defer
 *   (optional) If set to TRUE, the defer attribute is set on the <script> tag.
 *   Defaults to FALSE. This parameter is not used with $type == 'setting'.
 */
function graphael_add($files = array(), $minified = TRUE, $scope = 'header', $defer = FALSE) {
  // Add Raphaël with the same parameters. This will essentially be a 
  // no-op if it is already loaded.
  raphael_add($minified, $scope, $defer);

  static $loaded_files;
  if (!is_array($loaded_files)) {
    $loaded_files = array();
  }

  // Convert file to an array if it's not one already.
  if (!is_array($files)) {
    $files = array($files);
  }
  
  // Add the core file to the array of files, if its not there already.
  if (!array_search('raphael', $files)) {
    array_unshift($files, 'raphael');
  }

  foreach ($files as $file) {
    if ($file == 'hbar') {
      $file = 'bar';
    }
    if (in_array($file, array('raphael', 'bar', 'dot', 'line', 'pie')) && !isset($loaded_files[$file])) {
      $file_name = ($minified) ? "g.$file-min.js" : "g.$file.js";
      $js_path = variable_get('graphael_path', RAPHAEL_PATH) . '/graphael/js/' . $file_name;
      drupal_add_js($js_path, array(
        'scope' => $scope,
        'group' => JS_LIBRARY,
        'defer' => $defer,
      ));
      $loaded_files[$file] = $js_path;
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function graphael_theme() {
  return array(
    'graphael' => array(
      'path' => drupal_get_path('module', 'graphael'),
      'template' => 'graphael',
      'variables' => array(
        'method' => NULL,
        'values' => NULL,
        'params' => array(),
        'extend' => array(),
        'attr' => array(),
      ),
    ),
    'graphael_views_ui_style_plugin_chart' => array(
      'variables' => array('form' => NULL),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function graphael_menu() {
  $items = array();

  $items['admin/config/content/graphael'] = array(
    'title' => 'gRaphaël',
    'description' => 'Settings for gRaphaël.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('graphael_admin_settings_form'),
    'access arguments' => array('administer site configuration'),
    'file' => 'graphael.admin.inc',
  );

  return $items;
}

/**
 * Preprocessor for theme('graphael').
 */
function template_preprocess_graphael(&$vars) {
  // Add required JS.
  $path = drupal_get_path('module', 'graphael');
  drupal_add_js($path .'/graphael.jquery.js');
  drupal_add_js($path .'/graphael.js');
  graphael_add($vars['method']);

  // Auto id assignment.
  static $autoid = 0;
  if (!isset($vars['attr']['id'])) {
    $autoid++;
    $vars['attr']['id'] = 'graphael-graph-'. $autoid;
  }

  // Class that the graphael Drupal behavior will use.
  if (isset($vars['attr']['class'])) {
    $vars['attr']['class'] .= ' graphael-graph';
  }
  else {
    $vars['attr']['class'] = 'graphael-graph';
  }

  // Default dimensions.
  $vars['attr']['style'] = isset($vars['attr']['style']) ? $vars['attr']['style'] : 'height:200px; width:400px;';
}

