You are here

function node_recur_generate_dates_from_form in Node recur 7

Generate dates from a form state

Return value

An array of start and end dates, keyed by start and end

2 calls to node_recur_generate_dates_from_form()
node_recur_node_form_submit in ./node_recur.module
Submit handler for the node recur form on the node form
node_recur_node_recur_confirm in ./node_recur.pages.inc
Confirm form for node recur

File

./node_recur.module, line 492

Code

function node_recur_generate_dates_from_form($node, $form_state) {

  // Extract the option
  $option = $form_state['values']['option'];

  // Extract the days
  $days = array();
  foreach ($form_state['values']['days'] as $day => $value) {
    if ($value) {
      $days[] = $day;
    }
  }

  // Extract the frequency
  $frequency = $form_state['values']['frequency'];

  // Extract the period
  $period = $form_state['values']['period'];

  // Extract the until date
  $until = strtotime($form_state['values']['until']);

  // Move until date to 1 minute before midnight
  $until += 86399;

  // Extract weekend toggle
  $weekends = !$form_state['values']['exclude_weekends'];

  // Get the initial dates
  $start_date = node_recur_get_node_date_field_value($node);
  $end_date = node_recur_get_node_date_field_value($node, FALSE);

  // Initalize
  $start_dates = array();
  $end_dates = array();

  // Generate the start dates
  if ($start_date) {
    if ($option == 'days') {
      $start_dates = node_recur_generate_dates_days($node, $start_date, $days, $until);
    }
    else {
      if ($option == 'rules') {
        $start_dates = node_recur_generate_dates_rule($node, $start_date, $frequency, $period, $until, $weekends);
      }
    }
  }

  // Generate the end dates
  if ($end_date) {

    // Determine if the start and end dates are different days
    $days_apart = NULL;
    if ($start_date) {
      if (date('j', strtotime($start_date)) != date('j', strtotime($end_date))) {

        // Determine the amount of days
        $days_apart = floor((strtotime($end_date) - strtotime($start_date)) / 86400);

        // Adjust the until date
        $until += $days_apart * 86400;
      }
    }
    if ($option == 'days') {
      $end_dates = node_recur_generate_dates_days($node, $end_date, $days, $until, $days_apart);
    }
    else {
      if ($option == 'rules') {
        $end_dates = node_recur_generate_dates_rule($node, $end_date, $frequency, $period, $until, $weekends);
      }
    }
  }

  // Allow other modules to alter the dates
  $dates = array(
    'start' => $start_dates,
    'end' => $end_dates,
  );
  $variables = array(
    'node' => $node,
    'start_date' => $start_date,
    'end_date' => $end_date,
    'option' => $option,
    'until' => $until,
  );
  if ($option == 'days') {
    $variables['days'] = $days;
  }
  if ($option == 'rules') {
    $variables += array(
      'frequency' => $frequency,
      'period' => $period,
      'weekends' => $weekends,
    );
  }
  drupal_alter('node_recur_dates', $dates, $variables);
  return $dates;
}