<?php
/**
 * @file
 * Code for the Collections profile feature.
 */
include_once ('collections_profile.features.inc');

/**
* hook_node_view()
*/
function collections_profile_init(){
  drupal_add_css(drupal_get_path('module', 'collections_profile') . '/css/collections_profile.css');
}

/**
 * Implementation of hook_menu().
 */
function collections_profile_menu(){
  $items = array();
  $items['collections_profile/%/%/profile'] = array(
    'title' => 'Collections profile',
    'type' => MENU_LOCAL_TASK
  );
  return $items;
}

/**
 * Implements hook_menu_local_tasks_alter().
 * 
 * Add tabs to individual institute profile page
 */
function collections_profile_menu_local_tasks_alter(&$data, $router_item, $root_path){
  if($root_path == 'collections_profile/%/%/profile'){
    $nid = arg(1);
    $coll_code = arg(2);
    if(node_access('update', node_load($nid))){
      // Add a tab to view the collections-profile page for consistency sake. 
      $data['tabs'][0]['output'][] = array(
        '#theme' => 'menu_local_task',
        '#link' => array(
          'title' => t('View'),
          'href' => 'collections_profile/' . $nid . '/' . $coll_code . '/profile',
          'localized_options' => array(
            'attributes' => array(
              'title' => t('View')
            )
          )
        )
      );
      $data['tabs'][0]['output'][0]['#active'] = TRUE; //Make this tab active.
      // Add a tab linking to edit the collections-profile page.
      $data['tabs'][0]['output'][] = array(
        '#theme' => 'menu_local_task',
        '#link' => array(
          'title' => t('Edit'),
          'href' => 'node/' . $nid . '/edit',
          'localized_options' => array(
            'attributes' => array(
              'title' => t('Edit')
            ),
            'query' => array(
              'destination' => 'collections_profile/' . $nid . '/' . $coll_code . '/profile'
            )
          )
        )
      );
      $data['tabs'][0]['output'][]['#active'] = FALSE;
      $data['tabs'][0]['count'] = 2;
      // Add a tab linking to revisions the collections-profile page.
      $data['tabs'][0]['output'][] = array(
        '#theme' => 'menu_local_task',
        '#link' => array(
          'title' => t('Revisions'),
          'href' => 'node/' . $nid . '/revisions',
          'localized_options' => array(
            'attributes' => array(
              'title' => t('Revisions')
            )
          )
        )
      );
      $data['tabs'][0]['output'][]['#active'] = FALSE;
      $data['tabs'][0]['count'] = 2;
    }
  }
}

/**
 * hook_date_format_types
 */
function collections_profile_date_format_types(){
  $types = array();
  $types['year_only'] = t('Year only');
  return $types;
}

/**
 * hook_date_formats
 */
function collections_profile_date_formats(){
  $formats = array();
  $formats[] = array(
    'type' => 'year_only',
    'format' => 'Y', // 2013
    'locales' => array()
  );
  // save the ne date formats into variables
  foreach($formats as $format){
    variable_set('date_format_' . $format['type'], $format['format']);
  }
  return $formats;
}

function collections_profile_block_info(){
  $blocks['collection_by_year_graph'] = array(
    'info' => t('Specimens by year graph')
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function collections_profile_block_view($delta = ''){
  $block = array();
  switch($delta){
    case 'collection_by_year_graph':
      $block['subject'] = '';
      $block['content'] = _collections_profile_graph_content();
      break;
  }
  return $block;
}

function _collections_profile_graph_content(){
  $collection_code = arg(2);
  $content = array();
  $sql = 'SELECT YEAR(field_data_field_date_collected.field_date_collected_value) AS date_collected_year,
          COUNT(*) AS Total 
          FROM node, field_data_field_institution_code, field_data_field_date_collected
          WHERE node.nid = field_data_field_institution_code.entity_id 
          AND field_data_field_institution_code.deleted = 0
          AND node.nid = field_data_field_date_collected.entity_id AND field_data_field_date_collected.deleted = 0
          AND field_data_field_institution_code.field_institution_code_value = :collection_code
          AND node.status = 1 
          AND node.type IN (:type) 
          GROUP BY date_collected_year
          ORDER BY date_collected_year DESC';
  $args = array(
    ':collection_code' => $collection_code,
    ':type' => 'specimen_observation'
  );
  $data = db_query($sql, $args)->fetchAllKeyed();
  $data = collections_profile_stats_processs_data('Year', 'Count', $data, 'PieChart');
  if($data){
    $content[] = array(
      '#theme' => 'simple_google_chart',
      '#type' => 'PieChart',
      '#data' => $data,
      '#title' => t(''),
      '#options' => array(
        'is3D' => TRUE,
        'title' => 'Specimen collections by year',
        'height' => 250
      )
    );
  }
  return $content;
}
/*
 * Process a keyed array from an SQL query an turn into a data array for simple_google_charts
*/
function collections_profile_stats_processs_data($col1_title, $col2_title, $array, $type = 'PieChart'){
  $return = array();
  $return[] = array(
    $col1_title,
    $col2_title
  );
  switch($type){
    case 'BarChart':
    case 'ColumnChart':
      foreach($array as $key => $value){
        $return[] = array(
          (int)$key,
          (int)$value
        );
      }
      break;
    case 'PieChart':
    default:
      foreach($array as $key => $value){
        $return[] = array(
          (string)$key,
          (int)$value
        );
      }
  }
  if(count($return) > 1){
    return $return;
  }else{
    return FALSE;
  }
}