<?php

/**
 * @file
 * Table to chart module
 */

/**
 * Implements hook_library().
 */
function table_chart_library() {
  $libraries['tabletochart'] = array(
    'title' => 'Table to Chart',
    'version' => '1.0',
    'js' => array(
      drupal_get_path('module', 'table_chart') . '/table_chart.js' => array(
        'scope' => 'footer',
      ),
    ),
    'dependencies' => array(
      // Require jQuery UI core by System module.
      array('table_chart', 'morrisjs'),
      // Require our other library.
      array('table_chart', 'tabletojson'),
    ),
  );
  // Raphael
  $libraries['raphaeljs'] = array(
    'title' => 'Raphael',
    'version' => '2.1.2',
    'website' => 'http://raphaeljs.com/',
    'js' => array(
      libraries_get_path('raphael') . '/raphael-min.js' => array(
        'scope' => 'footer',
      ),
    ),
  );
  // Morris.js
  $libraries['morrisjs'] = array(
    'title' => 'Morris.js',
    'version' => '0.4.3',
    'website' => 'http://www.oesmith.co.uk/morris.js/',
    'js' => array(
      libraries_get_path('morrisjs') . '/morris.min.js' => array(
        'scope' => 'footer',
      ),
    ),
    'css' => array(
      libraries_get_path('morrisjs') . '/morris.css' => array(
        'type' => 'file',
      ),
    ),
    'dependencies' => array(
      // Require jQuery UI core by System module.
      array('table_chart', 'raphaeljs'),
    ),
  );
  // Table to JSON.
  $libraries['tabletojson'] = array(
    'title' => 'Table to JSON',
    'website' => 'http://lightswitch05.github.io/table-to-json/',
    'version' => '0.6',
    'js' => array(
      libraries_get_path('tabletojson') . '/lib/jquery.tabletojson.min.js' => array(
        'scope' => 'footer',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_views_api().
 */
function table_chart_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'table_chart') . '/views',
  );
}

/**
 * Implements hook_preprocess_views_view_table().
 */
function table_chart_preprocess_views_view_table(&$vars) {
  if (!empty($vars['view'])) {
    $attributes = $vars['view']->display_handler->get_option('attributes');
    $attributes = table_chart_attributes_keyed_array($attributes);
    if (!empty($attributes)) {
      $vars['attributes_array'] = $vars['attributes_array'] + $attributes;
    }
  }
}

/**
* Implements hook_views_aggregator_results_table().
*/
function table_chart_preprocess_views_aggregator_results_table(&$vars) {
  table_chart_preprocess_views_view_table($vars);
}

/**
 * Convert the attributes string into an array compatible with drupal_attributes
 *
 * @param string $attributes
 *  String settings for the attributes
 * @return array
 *  Returns the array of valid attributes.
 */
function table_chart_attributes_keyed_array($attributes) {
  $tmp = explode("\n", $attributes);
  $tmp = array_filter($tmp, 'trim');

  $keyed_attributes = array();
  foreach ($tmp as $attribute) {
    parse_str($attribute, $array);
    if (is_array($array)) {
      foreach ($array as $key => $value) {
        $keyed_attributes['data-' . $key] = $value;
      }
    }
  }

  return $keyed_attributes;
}