[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/calendar/calendar_ical/ -> calendar_plugin_style_ical.inc (source)

   1  <?php
   2  // $Id: calendar_plugin_style_ical.inc,v 1.1.2.17 2009/04/28 22:47:29 karens Exp $
   3  /**
   4   * Default style plugin to render an iCal feed.
   5   */
   6  class calendar_plugin_style_ical extends views_plugin_style_rss {
   7    function init(&$view, &$display) {
   8      parent::init($view, $display);
   9      $fields = $display->handler->default_display->options['fields'];
  10      $this->options['fields'] = $fields;
  11    }
  12    
  13    function query() {
  14      // We need these values for the iCal feed.
  15      $this->view->query->add_field('node', 'title');
  16      $this->view->query->add_field('node', 'type');
  17      parent::query();
  18    }
  19    
  20    function attach_to($display_id, $path, $title) {
  21      $display = $this->view->display[$display_id]->handler;
  22      $url_options = array();
  23      $input = $this->view->get_exposed_input();
  24      if ($input) {
  25        $url_options['query'] = $input;
  26      }
  27      
  28      // TODO adjust this to pick up default values when no arg is set?
  29      $url = url($this->view->get_url(NULL, $path), $url_options);
  30      if (empty($this->view->feed_icon)) {
  31        $this->view->feed_icon = '';
  32      }
  33      $this->view->feed_icon .= theme('calendar_ical_icon', $url);
  34  
  35      drupal_add_link(array(
  36        'rel' => 'alternate',
  37        'type' => 'application/calendar',
  38        'title' => $title,
  39        'href' => $url
  40      ));
  41    }
  42  
  43    /**
  44     * Set default options
  45     */
  46    function options(&$options) {
  47      parent::options($options);
  48      $options['summary_field'] = 'title';
  49      $options['description_field'] = '';
  50      $options['location_field'] = '';
  51      $options['fields'] = array();
  52    }
  53    
  54    function option_definition() {
  55      $options = parent::option_definition();
  56  
  57      $options['summary_field'] = array('default' => '', 'translatable' => TRUE);
  58      $options['description_field'] = array('default' => '', 'translatable' => TRUE);
  59      $options['location_field'] = array('default' => '', 'translatable' => TRUE);
  60      
  61      return $options;
  62    }
  63  
  64    function options_form(&$form, &$form_state) {
  65      $options = array('' => '');
  66      foreach ($this->options['fields'] as $field) {
  67        $handler = views_get_handler($field['table'], $field['field'], 'field');
  68        $options[$field['field']] = $handler->ui_name();
  69      }
  70      $form['#prefix'] = '<div class="form-item">'. t("Map the View fields to the values they should represent in the iCal feed. Only fields that have been added to the view are available to use in this way. You can add additional fields to the view and mark them 'Exclude from display' if you only want them in the iCal feed.") .'</div>';
  71      
  72      $form['summary_field'] = array(
  73        '#type' => 'select',
  74        '#title' => t('Title'),
  75        '#default_value' => !empty($this->options['summary_field']) ? $this->options['summary_field'] : 'title',
  76        '#options' => $options,
  77        '#required' => TRUE,
  78        );
  79      $form['description_field'] = array(
  80        '#type' => 'select',
  81        '#title' => t('Description'),
  82        '#default_value' => $this->options['description_field'],
  83        '#options' => $options,
  84        );
  85      $form['location_field'] = array(
  86        '#type' => 'select',
  87        '#title' => t('Location'),
  88        '#default_value' => $this->options['location_field'],
  89        '#options' => $options,
  90        );
  91        
  92    }
  93  
  94     /**
  95     * Style validation.
  96     */
  97    function validate() {
  98      $errors = parent::validate();
  99      
 100      $style = $this->display->display_options['style_plugin'];
 101      
 102      $arguments = $this->display->handler->get_option('arguments');
 103      $filters = $this->display->handler->get_option('filters');
 104      
 105      if (!array_key_exists('date_argument', $arguments) && !array_key_exists('date_filter', $filters)) {
 106        if (empty($this->view->date_info->arg_missing)) {
 107          $errors[$style] = t("The @style style requires a Date argument or a Date filter.", array('@style' => $style));      
 108        }
 109        $this->view->date_info->arg_missing = TRUE;
 110      }
 111      if (array_key_exists('date_argument', $arguments) && 
 112      ($arguments['date_argument']['default_action'] != 'default' || $arguments['date_argument']['default_argument_type'] != 'date')) {
 113        if (empty($this->view->date_info->arg_missing_default)) {
 114          $errors[] = calendar_errors('missing_argument_default');      
 115        }
 116        $this->view->date_info->arg_missing_default = TRUE;
 117      }
 118       
 119      if (empty($this->options['summary_field'])) {
 120        $errors[] = $errors[$style] = t("The @style style requires a Title field for the iCal export.", array('@style' => $style));
 121      }
 122      // Make sure date fields are not set up to 'Group multiple values' 
 123      // in the calendar style.
 124      if ($style == 'calendar_style') {
 125        $view_fields = date_api_fields($this->view->base_table);
 126        $view_fields = $view_fields['name'];
 127        $fields = $this->display->handler->get_option('fields');
 128        foreach ($fields as $column => $field) {
 129          $field_name = $field['table'] .".". $field['field'];
 130          if (!empty($field['multiple'])) {
 131            $cck_fields = content_fields();
 132            $real_name = $view_fields[$field_name]['real_field_name'];
 133            if ($cck_fields[$real_name]['multiple'] && !empty($field['multiple']['group'])) {
 134              $errors[] = t("The date field '@field' used by the display '@display_title' cannot be set to 'Group multiple values'.", array('@field' => $view_fields[$field_name]['label'], '@display_title' => $this->display->display_title));
 135            }
 136          }
 137        }
 138      }
 139      return $errors;
 140    }
 141   
 142    function render() {
 143      require_once('./'. drupal_get_path('module', 'calendar') .'/includes/calendar.inc');
 144      
 145      // Transfer the style options to the view object so they
 146      // can be easily accessed in the theme.
 147      $style_options = $this->options;
 148      $this->view->date_info->summary_field = $style_options['summary_field'];
 149      $this->view->date_info->description_field = $style_options['description_field'];
 150      $this->view->date_info->location_field = $style_options['location_field'];
 151      
 152      // Evaluate our argument values and figure out which 
 153      // calendar display we need to create.
 154      $i = 0;   
 155      foreach ($this->view->argument as $id => $argument) {
 156        if ($argument->field == 'date_argument') {
 157          // TODO Decide if we want to provide a date here or not.
 158          // Adding this now is to prevent fatal errors later if the
 159          // view is used in unexpected ways without a date being set.
 160          if (empty($argument->min_date)) {
 161            $value = $argument->get_default_argument();
 162            $range = $argument->date_handler->arg_range($value);
 163            $argument->min_date = $range[0];
 164            $argument->max_date = $range[1];
 165          }
 166          $this->view->date_info->granularity = !empty($argument->granularity) ? $argument->granularity : $argument->options['granularity'];
 167          $this->view->date_info->date_arg = !empty($this->view->args) ? $this->view->args[$argument->position] : '';
 168          $this->view->date_info->date_arg_pos = $i;
 169          $this->view->date_info->year = isset($argument->year) ? $argument->year : NULL;
 170          $this->view->date_info->month = isset($argument->month) ? $argument->month: NULL;
 171          $this->view->date_info->day = isset($argument->day) ? $argument->day : NULL;
 172          $this->view->date_info->week = isset($argument->week) ? $argument->week : NULL;
 173          $this->view->date_info->min_date = $argument->min_date;
 174          $this->view->date_info->max_date = $argument->max_date;
 175                  
 176          // Stop after the first date argument, if there is more than one.
 177          break;
 178        }
 179        $i++;
 180      }
 181      
 182      // The ical display might have date filters instead of arguments.
 183      // If we missed getting a min date from date arguments, try date filters.
 184      if (empty($this->view->date_info->min_date)) {
 185        foreach ($this->view->filter as $id => $filter) {
 186          if ($filter->field == 'date_filter') {
 187            // TODO Decide if we want to provide a date here or not.
 188            // Adding this now is to prevent fatal errors later if the
 189            // view is used in unexpected ways without a date being set.
 190            if (empty($filter->min_date)) {
 191              $value = $filter->default_value('value');
 192              $range = $filter->date_handler->arg_range($value);
 193              $filter->min_date = $range[0];
 194              $filter->max_date = $range[1];
 195            }
 196            $this->view->date_info->granularity = !empty($filter->granularity) ? $filter->granularity : $filter->options['granularity'];
 197            $this->view->date_info->year = isset($filter->year) ? $filter->year : NULL;
 198            $this->view->date_info->month = isset($filter->month) ? $filter->month: NULL;
 199            $this->view->date_info->day = isset($filter->day) ? $filter->day : NULL;
 200            $this->view->date_info->week = isset($filter->week) ? $filter->week : NULL;
 201            $this->view->date_info->min_date = $filter->min_date;
 202            $this->view->date_info->max_date = $filter->max_date;
 203            if (empty($this->view->date_info->date_fields)) {
 204              $this->view->date_info->date_fields = array();
 205            }
 206            $this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, array_keys($filter->options['date_fields']));
 207            
 208            // Stop after the first date filter, if there is more than one.
 209            break;
 210          }
 211        }
 212        $i++;
 213      }
 214         
 215      // Render each field into an output array.
 216      $items = array();
 217      $calendar_fields = date_api_fields($this->view->base_table);
 218      $calendar_fields = array_keys($calendar_fields['alias']);
 219      
 220      foreach ($this->view->result as $num => $row) {
 221        $items[$num] = $row;
 222        // Store the raw date values before formatting the results.
 223        foreach ($row as $key => $value) {
 224          if (in_array($key, $calendar_fields)) {
 225            $items[$num]->calendar_fields->$key = $value;
 226          }
 227        }
 228        foreach ($this->view->field as $name => $field) {
 229          // Some fields, like the node edit and delete links, have no alias.
 230          $field_alias = $field->field_alias != 'unknown' ? $field->field_alias : $name; 
 231          if (!empty($field) && is_object($field)) {
 232            $field_output = $field->theme($row);
 233            $items[$num]->{$field_alias} = $field_output;
 234          }
 235        }
 236      }
 237      
 238      // Massage the resulting items into formatted calendar items.
 239      $items = calendar_build_nodes($this->view, $items);
 240      
 241      // Merge in items from other sources.
 242      foreach (module_implements('calendar_add_items') as $module) {
 243        $function = $module .'_calendar_add_items';
 244        if (function_exists($function)) {
 245          if ($feeds = $function($this->view)) {
 246            foreach ($feeds as $feed) {
 247              $items = $feed;
 248            }
 249          }
 250        }
 251      }
 252      return theme($this->theme_functions(), $this->view, $this->options, $items);
 253    }
 254  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7