You are here

function fullcalendar_views_pre_view in FullCalendar 7.2

Same name and namespace in other branches
  1. 8.5 fullcalendar.views_execution.inc \fullcalendar_views_pre_view()
  2. 8 fullcalendar.views_execution.inc \fullcalendar_views_pre_view()
  3. 8.2 fullcalendar.views_execution.inc \fullcalendar_views_pre_view()
  4. 8.3 fullcalendar.views_execution.inc \fullcalendar_views_pre_view()
  5. 8.4 fullcalendar.views_execution.inc \fullcalendar_views_pre_view()

Implements hook_views_pre_view().

Add an argument that provides the current date for each date field present.

File

includes/views/fullcalendar.views.inc, line 74
Contains Views module hooks.

Code

function fullcalendar_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->display_handler
    ->get_option('style_plugin') != 'fullcalendar') {
    return;
  }

  // Get the current view settings.
  $view
    ->init_style();
  $settings = $view->style_plugin->options;
  fullcalendar_include_api();
  $fullcalendar_options = module_invoke_all('fullcalendar_options_info');
  uasort($fullcalendar_options, 'drupal_sort_weight');
  foreach (array_intersect(array_keys($fullcalendar_options), module_implements('fullcalendar_options_pre_view')) as $module) {
    $function = $module . '_fullcalendar_options_pre_view';
    $function($settings, $view);
  }
  $settings['ajax'] = FALSE;

  // If we're not using ajax, we're done.
  if (!$view->display_handler
    ->get_option('use_ajax')) {

    // Set the new view settings.
    $view->style_plugin->options = $settings;
    return;
  }
  $settings['fullcalendar_fields_count'] = 0;
  $exposed_input = $view
    ->get_exposed_input();

  // Loop through each date field and provide an argument for it.
  foreach ($view->display_handler
    ->get_handlers('field') as $field_id => $field) {
    if (!fullcalendar_field_is_date($field)) {
      continue;
    }
    $settings['ajax'] = TRUE;

    // Add an exposed filter for the date field.
    $field_value_id = $field->real_field . str_replace($field->field, '', $field_id);
    if (isset($exposed_input[$field_value_id])) {
      $timestamp = (strtotime($exposed_input[$field_value_id]['min']['date']) + strtotime($exposed_input[$field_value_id]['max']['date'])) / 2;
      $min = date('Y-m', $timestamp);
      $max = date('Y-m', strtotime($min . ' +1 month'));
    }
    else {
      $min = date('Y-m', mktime(0, 0, 0, $settings['date']['month'] + 1, 1, $settings['date']['year']));
      $max = date('Y-m', mktime(0, 0, 0, $settings['date']['month'] + 2, 1, $settings['date']['year']));
    }
    $min = date('Y-m-d', strtotime($min . ' -2 weeks'));
    $max = date('Y-m-d', strtotime($max . ' +2 weeks'));
    $options = array(
      'exposed' => TRUE,
      'form_type' => 'date_select',
      'operator' => 'between',
      'default_date' => $min,
      'default_to_date' => $max,
      'group' => 0,
    );
    if (!empty($field->options['relationship'])) {
      $options['relationship'] = $field->options['relationship'];
    }
    $option_id = $view
      ->add_item($display_id, 'filter', 'field_data_' . $field->field, $field->field . '_value', $options);
    $settings['fullcalendar_fields'][$option_id] = drupal_html_class($option_id);
    $settings['fullcalendar_fields_count']++;
    $view
      ->set_item_option($display_id, 'filter', $option_id, 'expose', array(
      'identifier' => $option_id,
      'operator' => $option_id . '_op',
    ));
  }
  if (isset($timestamp)) {
    $settings['date']['date'] = date('d', $timestamp);
    $settings['date']['month'] = date('n', $timestamp) - 1;
    $settings['date']['year'] = date('Y', $timestamp);
  }
  $view->style_plugin->options = $settings;
}