<?php

/**
 * @file
 * Provides YUI popup calendar and option to replace text box with drop down date selection
 *
 * Usage:
 *
 * <code>
 * yuicalendar_build_form(&$form, $id, $date, $weight, $drop_down, $date_only);
 * </code>
 */

/**
 * Implementation of hook_form_alter().
 *
 * Capture author date fields and add the YUI calendar to them.
 */
function yuicalendar_form_alter(&$form, $form_state, $form_id) {
  if ($form_id === 'comment_form' && isset($form['admin']) && isset($form['admin']['date'])) {
    $form['admin']['author']['#weight'] = -4;
    yuicalendar_build_form($form['admin'], 'date', strtotime($form['admin']['date']['#default_value']), -3, variable_get('yuicalendar_edit_drop_down', '1'), NULL);
  }
  elseif (isset($form['type']) && $form['type']['#value'].'_node_form' === $form_id && isset($form['author']) && isset($form['author']['date'])) {
    yuicalendar_build_form($form['author'], 'date', $form['created']['#value'], 5, variable_get('yuicalendar_edit_drop_down', '1'), NULL);
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function yuicalendar_nodeapi($node, $op) {
  switch ($op) {
    case 'validate':
      if (isset($node->yuicalendar)) {
        yuicalendar_load();
      }
  }
}

/**
 * Alter a form element such that it produces a YUI calendar hybrid.
 *
 * @param $form
 *   The form that contains the element to be transformed.
 * @param $id
 *   The id of the element to be transformed (must be a textfield or a date).
 * @param $date
 *   The date of the form element.
 * @param $weight
 *   The weight (needed so that the support elements will be placed in the dom properly)
 * @param $drop_down
 *   Whether to make the field a series of drop down menus or a simple text field (calendar only).
 * @param $date_only
 *   Whether to process as only a date field (without the time).
 * @return
 *   Returns TRUE
 */
function yuicalendar_build_form(&$form, $id, $date, $weight, $drop_down = NULL, $date_only = NULL, $timezone = NULL) {
  // Make sure the form type is appropriate (date or textfield)
  if ($form[$id]['#type'] !== 'textfield' && $form[$id]['#type'] !== 'date') {
    return;
  }

  // Load the calendar core
  yuicalendar_load();

  // Apply the requested settings
  if ($drop_down !== NULL) {
    $form[$id.'-drop-down'] = Array(
      '#type' => 'hidden',
      '#value' => $drop_down);
  }
  if ($date_only !== NULL) {
    $form[$id.'-date-only'] = Array(
      '#type' => 'hidden',
      '#value' => $date_only);
  }

  $form['yuicalendar'] = Array('#type' => 'hidden');
  
  // Wrap and set the weight of the form element being transformed into a calendar/date
  $form[$id]['#attributes'] = array('class' => 'yuicalendar');
  $form[$id]['#prefix'] = '<div class="yuicalendar-wrapper"><div class="yuicalendar-date">';
  $form[$id]['#suffix'] = '</div>';
  $form[$id]['#weight'] = $weight;

  // Add additional supporting form elements
  $year = $yearNow = gmdate('Y');
  $years = Array();
  while ($year >= 1969) {
    $years[$year] = $year;
    $year--;
  }

  $form[$id . '-year'] = Array(
    '#type' => 'select',
    '#weight' => $weight + .05,
    '#attributes' => array('style' => 'display: none;'),
    '#default_value' => (int)gmdate('Y', $date),
    '#prefix' => '<div class="yuicalendar-date">',
    '#suffix' => '</div>',
    '#options' => $years);
  $form[$id . '-month'] = Array(
    '#type' => 'select',
    '#weight' => $weight + .10,
    '#attributes' => array('style' => 'display: none;'),
    '#default_value' => (int)gmdate('m', $date),
    '#prefix' => '<div class="yuicalendar-date">',
    '#suffix' => '</div>',
    '#options' => Array(1 => t('JAN'), 2 => t('FEB'), 3 => t('MAR'), 4  => t('APR'), 5 => t('MAY'), 6 => t('JUN'), 7 => t('JUL'), 8 => t('AUG'), 9 => t('SEP'), 10 => t('OCT'), 11 => t('NOV'), 12 => t('DEC')));
  $form[$id . '-day'] = Array(
    '#type' => 'select',
    '#weight' => $weight + .15,
    '#attributes' => array('style' => 'display: none;'),
    '#default_value' => (int)(gmdate('d', $date) - 1),
    '#prefix' => '<div class="yuicalendar-date">',
    '#suffix' => '</div>',
    '#options' => Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31));
  $form[$id . '-time'] = Array(
    '#type' => 'textfield',
    '#weight' => $weight + .20,
    '#attributes' => array('style' => 'display: none;'),
    '#default_value' => gmdate('H:i:s', $date),
    '#size' => 12,
    '#maxlength' => 12,
    '#prefix' => '<div class="yuicalendar-date">',
    '#suffix' => '</div></div>');
  $form[$id . '-timezone'] = Array(
    '#type' => 'hidden',
    '#weight' => $weight + .25,
    '#default_value' => ($timezone !== NULL ? gmdate('O', $date) : ''));

  return TRUE;
}

/*
 * Settings form as implemented by hook_menu
 */
function yuicalendar_admin_settings() {
  $form['yuicalendar_edit_drop_down'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use drop down for dates on edit/create nodes or comments.'),
    '#description' => t('Provides a drop down for dates rather than a text box.'),
    '#default_value' => variable_get('yuicalendar_edit_drop_down', '1'),
  );

  return system_settings_form($form);
}

/**
 * Implemention of hook_menu().
 */
function yuicalendar_menu() {
    $items['admin/settings/yuicalendar'] = array(
    'title' => t('YUI Calendar Settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('yuicalendar_admin_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
    );

  return $items;
}

/**
 * Load needed files.
 */
function yuicalendar_load() {
  static $loaded = FALSE;

  if ($loaded) {
    return;
  }

  $path = drupal_get_path('module', 'yuicalendar');

  $yui_source = variable_get('yui_source', 'http://yui.yahooapis.com/2.5.1');

  yui_add_js('calendar', $yui_source, '/build/yahoo/yahoo-min.js');
  yui_add_js('calendar', $yui_source, '/build/yahoo-dom-event/yahoo-dom-event.js');
  yui_add_js('calendar', $yui_source, '/build/animation/animation-min.js');
  yui_add_js('calendar', $yui_source, '/build/calendar/calendar-min.js');
  yui_add_js('calendar', $yui_source, '/build/autocomplete/autocomplete-min.js');
  yui_add_js('calendar', $yui_source, '/build/yahoo/yahoo-min.js');
  yui_add_css('calendar', $yui_source, '/build/calendar/assets/skins/sam/calendar.css');
  yui_add_css('calendar', $yui_source, '/build/autocomplete/assets/skins/sam/autocomplete.css');

  drupal_add_js($path . '/yuicalendar_init.js');
  drupal_add_js($path . '/yuicalendar.js');
  drupal_add_css($path . '/yuicalendar.css');

  $loaded = TRUE;
}
