<?php
/**
 * @file
 *
 * This module creates a form element that allows users to select
 * repeat rules for a date, and reworks the result into an iCal
 * RRULE string that can be stored in the database.
 *
 * The module also parses iCal RRULEs to create an array of dates
 * that meet their criteria.
 *
 * Other modules can use this API to add self-validating form elements
 * to their dates, and identify dates that meet the RRULE criteria.
 *
 */
/**
 * Implementation of hook_elements().
 */
function date_repeat_elements() {
  $type['date_repeat_rrule'] = array(
    '#input' => TRUE,
    '#process' => array('date_repeat_rrule_process'),
    '#element_validate' => array('date_repeat_rrule_validate'),
  );
  return $type;
}

/**
 * Implementation of hook_menu.
 */
function date_repeat_menu() {
  $items = array();
  $items['date_repeat_get_exception_form_ajax'] = array(
    'page callback' => 'date_repeat_get_exception_form_ajax',
    'page arguments' => array(1, 2),
    'file' => 'date_repeat_form.inc',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );
  return $items;
}

function date_repeat_theme() {
  return array(
    'date_repeat' => array('arguments' => array('element' => NULL)),
    'date_repeat_current_exceptions' => array('arguments' => array('element' => NULL)),
    'date_repeat_current_additions' => array('arguments' => array('element' => NULL)),
  );
}

/**
 * Helper function for FREQ options.
 */
function FREQ_options() {
  return array(
    'NONE' => t('-- Period'),
    'DAILY' => date_t('Days', 'datetime_plural'),
    'WEEKLY' => date_t('Weeks', 'datetime_plural'),
    'MONTHLY' => date_t('Months', 'datetime_plural'),
    'YEARLY' => date_t('Years', 'datetime_plural'),
  );
}

function INTERVAL_options() {
  $options = array(
    0 => t('-- Frequency'),
    1 => date_t('Every', 'date_order'),
  );
  for ($i = 2; $i < 367; $i++) {
    $options[$i] = t('Every @number', array('@number' => $i));
  }
  return $options;
}

/**
 * Helper function for FREQ options.
 *
 * Translated and untranslated arrays of the iCal day of week names.
 * We need the untranslated values for date_modify(), translated
 * values when displayed to user.
 */
function date_repeat_dow_day_options($translated = TRUE) {
  return array(
    'SU' => $translated ? date_t('Sunday', 'day_name') : 'Sunday',
    'MO' => $translated ? date_t('Monday', 'day_name') : 'Monday',
    'TU' => $translated ? date_t('Tuesday', 'day_name') : 'Tuesday',
    'WE' => $translated ? date_t('Wednesday', 'day_name') : 'Wednesday',
    'TH' => $translated ? date_t('Thursday', 'day_name') : 'Thursday',
    'FR' => $translated ? date_t('Friday', 'day_name') : 'Friday',
    'SA' => $translated ? date_t('Saturday', 'day_name') : 'Saturday',
  );
}

function date_repeat_dow_day_options_ordered($week_start) {
  $unordered = date_repeat_dow_day_options(FALSE);
  if (variable_get('date_first_day', 1) > 0) {
    for ($i = 1; $i <= variable_get('date_first_day', 1); $i++) {
      $last = array_shift($weekdays);
      array_push($weekdays, $last);
    }
  }
  return $weekdays;
}

/**
 * Helper function for BYDAY options.
 */
function date_repeat_dow_count_options() {
  return array('' => date_t('Every', 'date_order')) + date_order_translated();
}

/**
 * Helper function for BYDAY options.
 *
 * Creates options like -1SU and 2TU
 */
function date_repeat_dow_options() {
  $options = array();
  foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
    foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
      $options[$count_key . $dow_key] = $count_value .' '. $dow_value;
    }
  }
  return $options;
}

/**
 * Translate a day of week position to the iCal day name.
 *
 * Used with date_format($date, 'w') or get_variable('date_first_day'),
 * which return 0 for Sunday, 1 for Monday, etc.
 *
 * dow 2 becomes 'TU', dow 3 becomes 'WE', and so on.
 */
function date_repeat_dow2day($dow) {
  $days_of_week = array_keys(date_repeat_dow_day_options(FALSE));
  return $days_of_week[$dow];
}

/**
 * Shift the array of iCal day names into the right order
 * for a specific week start day.
 */
function date_repeat_days_ordered($week_start_day) {
  $days = array_flip(array_keys(date_repeat_dow_day_options(FALSE)));
  $start_position = $days[$week_start_day];
  $keys = array_flip($days);
  if ($start_position > 0) {
    for ($i = 1; $i <= $start_position; $i++) {
      $last = array_shift($keys);
      array_push($keys, $last);
    }
  }
  return $keys;
}

/**
 * Build a description of an iCal rule.
 *
 * Constructs a human-readable description of the rule.
 */
function date_repeat_rrule_description($rrule, $format = 'D M d Y') {
  // Empty or invalid value.
  if (empty($rrule) || !strstr($rrule, 'RRULE')) {
    return;
  }

  require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_ical.inc');
  require_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_calc.inc');

  // Make sure there will be an empty description for any unused parts.
  $description = array(
    '!interval' => '',
    '!byday' => '',
    '!bymonth' => '',
    '!count' => '',
    '!until' => '',
    '!except' => '',
    '!additional' => '',
    '!week_starts_on' => '',
    );
  $parts = date_repeat_split_rrule($rrule);
  $additions = $parts[2];
  $exceptions = $parts[1];
  $rrule = $parts[0];
  $interval = INTERVAL_options();
  switch ($rrule['FREQ']) {
    case 'WEEKLY':
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every week', 'every @count weeks') .' ';
      break;
    case 'MONTHLY':
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every month', 'every @count months') .' ';
      break;
    case 'YEARLY':
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every year', 'every @count years') .' ';
      break;
    default:
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every day', 'every @count days') .' ';
      break;
  }

  if (!empty($rrule['BYDAY'])) {
    $days = date_repeat_dow_day_options();
    $counts = date_repeat_dow_count_options();
    $results = array();
    foreach ($rrule['BYDAY'] as $byday) {
      $day = drupal_substr($byday, -2);
      $count = intval(str_replace(' '. $day, '', $byday));
      if ($count = intval(str_replace(' ' . $day, '', $byday))) {
        $results[] = trim(t('!repeats_every_interval on the !date_order !day_of_week', array('!repeats_every_interval ' => '', '!date_order' => strtolower($counts[drupal_substr($byday, 0, 2)]), '!day_of_week' => $days[$day])));
      }
      else {
        $results[] = trim(t('!repeats_every_interval every !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => $days[$day])));
      }
    }
    $description['!byday'] = implode(' '. t('and') .' ', $results);
  }
  if (!empty($rrule['BYMONTH'])) {
    if (sizeof($rrule['BYMONTH']) < 12) {
      $results = array();
      $months = date_month_names();
      foreach ($rrule['BYMONTH'] as $month) {
        $results[] = $months[$month];
      }
      if (!empty($rrule['BYMONTHDAY'])) {
        $description['!bymonth'] = trim(t('!repeats_every_interval on the !month_days of !month_names', array('!repeats_every_interval ' => '', '!month_days' => implode(', ', $rrule['BYMONTHDAY']), '!month_names' => implode(', ', $results))));
      }
      else {
        $description['!bymonth'] = trim(t('!repeats_every_interval on !month_names', array('!repeats_every_interval ' => '', '!month_names' => implode(', ', $results))));
      }
    }
  }
  if ($rrule['INTERVAL'] < 1) {
    $rrule['INTERVAL'] = 1;
  }
  if (!empty($rrule['COUNT'])) {
    $description['!count'] = trim(t('!repeats_every_interval !count times', array('!repeats_every_interval ' => '', '!count' => $rrule['COUNT'])));
  }
  if (!empty($rrule['UNTIL'])) {
    $until = date_ical_date($rrule['UNTIL'], 'UTC');
    date_timezone_set($until, date_default_timezone());
    $description['!until'] = trim(t('!repeats_every_interval until !until_date', array('!repeats_every_interval ' => '', '!until_date' => date_format_date($until, 'custom', $format))));
  }
  if ($exceptions) {
    $values = array();
    foreach ($exceptions as $exception) {
      $values[] = date_format_date(date_ical_date($exception), 'custom', $format);
    }
    $description['!except'] = trim(t('!repeats_every_interval except !except_dates', array('!repeats_every_interval ' => '', '!except_dates' => implode(', ', $values))));
  }
  if ($additions) {
    $values = array();
    foreach ($additions as $addition) {
      $values[] = date_format_date(date_ical_date($addition), 'custom', $format);
    }
    $description['!additional'] = trim(t('Also includes !additional_dates.', array('!additional_dates' => implode(', ', $values))));
  }
  if (!empty($rrule['WKST'])) {
    $day_names = date_repeat_dow_day_options();
    $description['!week_starts_on'] = trim(t('!repeats_every_interval where the week start on !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => $day_names[trim($rrule['WKST'])])));
  }
  return t('Repeats !interval !bymonth !byday !count !until !except. !additional', $description);
}

/**
 * Parse an iCal rule into a parsed RRULE array and an EXDATE array.
 */
function date_repeat_split_rrule($rrule) {
  $parts = explode("\n", str_replace("\r\n", "\n", $rrule));
  $rrule = array();
  $exceptions = array();
  $additions = array();
  foreach ($parts as $part) {
    if (strstr($part, 'RRULE')) {
      $RRULE = str_replace('RRULE:', '', $part);
      $rrule = (array) date_ical_parse_rrule('RRULE:', $RRULE);
    }
    elseif (strstr($part, 'EXDATE')) {
      $EXDATE = str_replace('EXDATE:', '', $part);
      $exceptions = (array) date_ical_parse_exceptions('EXDATE:', $EXDATE);
      unset($exceptions['DATA']);
    }
    elseif (strstr($part, 'RDATE')) {
      $RDATE = str_replace('RDATE:', '', $part);
      $additions = (array) date_ical_parse_exceptions('RDATE:', $RDATE);
      unset($additions['DATA']);
    }
  }
  return array($rrule, $exceptions, $additions);
}

/**
 * Analyze a RRULE and return dates that match it.
 */
function date_repeat_calc($rrule, $start, $end, $exceptions = array(), $timezone = NULL, $additions = array()) {
  require_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_calc.inc');
  return _date_repeat_calc($rrule, $start, $end, $exceptions, $timezone, $additions);
}

/**
 * Generate the repeat rule setting form.
 */
function date_repeat_rrule_process($element, $edit, $form_state, $form) {
  require_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_form.inc');
  return _date_repeat_rrule_process($element, $edit, $form_state, $form);
}
