<?php
/**
 * @file
 * This is the file description for Perfart module.
 *
 * In this more verbose, multi-line description, you can specify what this
 * file does exactly. Make sure to wrap your documentation in column 78 so
 * that the file can be displayed nicely in default-sized consoles.
 */

define('PERFART_ELMTS_BY_PAGE', 100);

/**
 * Implements hook_menu().
 */
function perfart_menu() {
  $items = array();

  $base = array(
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'perfart.pages.inc',
  );

  $items['perfart/ajax/dates'] = $base+array(
    'title' => 'List of dates',
    'page callback' => 'perfart_get_dates',
    'page arguments' => array(3),
  );

  $items['perfart/ajax/perf'] = $base+array(
    'title' => 'content of one perf',
    'page callback' => 'perfart_get_perf',
    'page arguments' => array(3),
  );

  $items['perfart/ajax/home'] = $base+array(
    'title' => 'content of home page',
    'page callback' => 'perfart_get_home',
    'page arguments' => array(),
  );

  $items['perfart/performances'] = $base+array(
    'title' => 'List of performances',
    'page callback' => 'perfart_get_performances',
    'page arguments' => array(),
  );

  $items['perfart/search'] = $base+array(
    'title' => 'search performances',
    'page callback' => 'perfart_search',
    'page arguments' => array(),
  );

  $items['perfart/search_api'] = $base+array(
    'title' => 'search performances (through search_api)',
    'page callback' => 'perfart_search_api',
    'page arguments' => array(),
  );

  // $items['perfart/ajax/filters'] = array(
  //   'title' => 'List of filters blocks (taxonomy terms)',
  //   'page callback' => 'perfart_get_filters',
  //   'access callback' => TRUE,
  //   'file' => 'perfart.pages.inc',
  //   'page arguments' => array(),


  // );

  // $items['perfart/update_cer'] = array(
  //   'title' => 'Updating Corresponding entity reference',
  //   'page callback' => 'perfart_update_cer',
  //   'access callback' => TRUE,
  //   'file' => 'perfart.pages.inc',
  //   'page arguments' => array(2),
  // );

  return $items;
}


/**
* Implements hook_ctools_plugin_directory -
* This lets ctools know to scan my module for a content_type plugin file
* Detailed docks in ctools/ctools.api.php 
*/ 
function perfart_ctools_plugin_directory($owner, $plugin_type) {
  // we'll be nice and limit scandir() calls
  if ($owner == 'ctools' && $plugin_type == 'content_types') {
    return 'plugins/content_types';
  }
}

/**
 * Implements hook_entity_info_alter().
 */
function perfart_entity_info_alter(&$entity_info) {
  // Set the controller class for nodes to an alternate implementation of the
  // DrupalEntityController interface.
  
  // dsm($entity_info, '$entity_info');
  $entity_info['node']['view modes']['short_infos'] = array(
    'label' => t('Short infos'),
    'custom settings' => TRUE,
  );
  $entity_info['node']['view modes']['views_table_cel'] = array(
    'label' => t('Views table cel'),
    'custom settings' => TRUE,
  );
  $entity_info['node']['view modes']['gridready'] = array(
    'label' => t('Grid ready (timeline)'),
    'custom settings' => TRUE,
  );
}

/**
* Implements hook_preprocess_node().
*/
function perfart_preprocess_node(&$vars) {
  // dsm($vars, '$vars node');
  // if($vars['view_mode'] == 'short_infos') {
  //   $vars['theme_hook_suggestions'][] = 'node__short_infos';
  //   $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__short_infos';
  // }
  // if($vars['view_mode'] == 'views_table_cel') {
  //   $vars['theme_hook_suggestions'][] = 'node__cel';
  //   $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__cel';
  // }

  $vars['theme_hook_suggestions'][] = 'node__'.$vars['view_mode'];
  $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__' . $vars['view_mode'];
}

function perfart_preprocess_field(&$vars) {
  // dsm($vars, '$vars field');
  $vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#view_mode'];
  $vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_type'] . '__' . $vars['element']['#view_mode'];
  $vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_name'] . '__' . $vars['element']['#view_mode'];
  }

/**
 * Implements hook_theme().
 */
function perfart_theme($existing, $type, $theme, $path) {
  $items = array();
  $path = drupal_get_path('module', 'perfart') .'/templates';
  
  $items['node__document__short_infos'] = array(
    'render element' => 'form',
    'path' => $path,
    'template' => 'node--document--short_infos',
    // 'preprocess functions' => array(
    //   'rubik_preprocess_form_confirm'
    // ),
  );

  $items['node__document__cel'] = array(
    'render element' => 'form',
    'path' => $path,
    'template' => 'node--document--cel',
  );
  
  /*
    TODO themable timeline item content
  */
  // $items['timeline__item_content'] = array(
  //   'render element' => 'form',
  //   'path' => $path,
  //   'template' => 'timeline--item-content',
  // );
  
    // $items['perfart_performances'] = array(
    //   'path' => $path,
    //   'template' => 'perfart-performances',
    // );


  return $items;
  }

/**
 * Implements hook_init().
 */
function perfart_init() {
  $query = new EffectuationEntityFieldQuery;
  $count = $query->count()->execute();
  
  $pages = ceil($count / PERFART_ELMTS_BY_PAGE);
  
  //  drupal_set_message("pages : ".$pages);
  
  drupal_add_js(array('perfart' => array(
    'effectuations_pages' => $pages,
    'strings'=>array(
       'topologies'=> t('topologies'),
       'places'=> t('places'),
       'peoples' => t('peoples'),
    ),
  )), 'setting');
}

/**
 * Implements hook_menu_get_item_alter().
 */
// function perfart_menu_get_item_alter(&$router_item, $path, $original_map){
//   dsm($router_item, 'router_item');
//   dsm($path, 'path');
//   dsm($original_map, 'original_map');
// }


