[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/calendar/includes/ -> calendar_plugin_style.inc (source)

   1  <?php
   2  // $Id: calendar_plugin_style.inc,v 1.1.2.18 2011/01/03 02:39:05 karens Exp $
   3  /**
   4   * Style plugin to create the calendar navigation and links.
   5   * 
   6   * Used by the main calendar page and calendar block displays.
   7   */
   8  class calendar_plugin_style extends views_plugin_style {
   9    /**
  10     * Init will be called after construct, when the plugin is attached to a
  11     * view and a display. 
  12     */
  13     function init(&$view, &$display, $options = NULL) {
  14       parent::init($view, $display, $options);
  15       if (!isset($view->date_info)) {
  16         $view->date_info = new StdClass();
  17       }
  18       $view->date_info->display_types = $this->display_types();
  19     }
  20      
  21    function display_types($granularity = NULL, $option_type = 'names') {
  22      $ids = array();
  23      $names = array();
  24      foreach (calendar_display_types() as $name => $type) {
  25        foreach ($this->view->display as $id => $display) {
  26          if ($display->display_plugin == 'calendar_period') {
  27            if (!empty($display->display_options['calendar_type']) && $display->display_options['calendar_type'] == $name) {
  28              $attachments = array_filter($display->display_options['displays']);
  29              if (isset($attachments['calendar_1'])) {
  30                $ids[$name] = $id;
  31                $names[$name] = $display->display_title;
  32              }
  33            }
  34          }
  35        }
  36      }
  37      if ($granularity) {
  38        return $$option_type[$granularity];
  39      }
  40      return $$option_type;
  41    }
  42    
  43    /**
  44     * Calendar argument date fields used in this view.
  45     */
  46    function date_fields() {
  47      $date_fields = array();
  48      $calendar_fields = date_api_fields($this->view->base_table);
  49      $arguments = $this->display->handler->get_option('arguments');
  50      foreach ($arguments as $name => $argument) {
  51        if (isset($argument['date_fields'])) {
  52          foreach ($argument['date_fields'] as $date_field) {
  53            $field = $calendar_fields['name'][$date_field];
  54            $handler = views_get_handler($field['table_name'], $field['field_name'], 'field');
  55            if ($handler) {
  56              $date_fields[$date_field] = $field;
  57              $date_fields[$date_field]['name'] = $handler->ui_name();
  58            }
  59          }
  60        }
  61      }
  62      return ($date_fields);
  63    }
  64    
  65    /**
  66     * Style validation.
  67     */
  68    function validate() {
  69      $errors = parent::validate();
  70      if (empty($this->display->display_options['style_plugin'])) {
  71        return $errors;
  72      }
  73      $style = $this->display->display_options['style_plugin'];
  74      
  75      $arguments = $this->display->handler->get_option('arguments');
  76      if (!in_array('date_argument', array_keys($arguments))) {
  77        if (empty($this->view->date_info->arg_missing)) {
  78          $errors[$style] = t("The @style style requires a Date argument.", array('@style' => $style));      
  79        }
  80        $this->view->date_info->arg_missing = TRUE;
  81        $this->date_info->arg_fields = array();
  82      }
  83      else {
  84        $this->date_info->arg_fields = $arguments['date_argument']['date_fields'];
  85        if ($arguments['date_argument']['default_action'] != 'default' || $arguments['date_argument']['default_argument_type'] != 'date') {
  86          if (empty($this->view->date_info->arg_missing_default)) {
  87            $errors[] = calendar_errors('missing_argument_default');      
  88          }
  89          $this->view->date_info->arg_missing_default = TRUE;
  90        }
  91      }
  92      
  93      // Make sure date fields are not set up to 'Group multiple values' 
  94      // in the calendar style.
  95      if ($style == 'calendar_style') {
  96        $view_fields = date_api_fields($this->view->base_table);
  97        $view_fields = $view_fields['name'];
  98        $fields = $this->display->handler->get_option('fields');
  99        $has_fields = FALSE;
 100        foreach ($fields as $column => $field) {
 101          $field_name = $field['table'] .".". $field['field'];
 102          if (in_array($field_name, $this->date_info->arg_fields)) {
 103            $has_fields = TRUE;
 104          }
 105          if (!empty($field['multiple']) && array_key_exists($field_name, $view_fields)) {
 106            $cck_fields = content_fields();
 107            $real_name = $view_fields[$field_name]['real_field_name'];
 108            if ($cck_fields[$real_name]['multiple'] && !empty($field['multiple']['group'])) {
 109              $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));
 110            }
 111          }
 112        }
 113        // The calendar needs the values from the date fields to split
 114        // the nodes into calendar cells, so make sure the field gets
 115        // added into the query.
 116        if (!$has_fields) {
 117          $errors[] = t('The date argument date fields must be added to this query. You can exclude them if you do not want them displayed in the calendar.');
 118        }
 119      }
 120      return $errors;
 121    }
 122  
 123    function query() {
 124      
 125      require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_sql.inc');
 126          
 127      $style_options = $this->view->style_plugin->options;
 128      
 129      // Evaluate our argument values and figure out which 
 130      // calendar display we need to create.
 131      $i = 0;   
 132      foreach ($this->view->argument as $id => $argument) {
 133        if ($argument->field == 'date_argument') {
 134          // TODO Decide if we want to provide a date here or not.
 135          // Adding this now is to prevent fatal errors later if the
 136          // view is used in unexpected ways without a date being set.
 137          if (empty($argument->min_date)) {
 138            $value = $argument->get_default_argument();
 139            $range = $argument->date_handler->arg_range($value);
 140            $argument->min_date = $range[0];
 141            $argument->max_date = $range[1];
 142          }
 143          $this->view->date_info->granularity = !empty($argument->granularity) ? $argument->granularity : $argument->options['granularity'];
 144          $this->view->date_info->date_arg = !empty($this->view->args) && count($this->view->args) > $argument->position ? $this->view->args[$argument->position] : '';
 145          $this->view->date_info->date_arg_pos = $i;
 146          $this->view->date_info->year = isset($argument->year) ? $argument->year : NULL;
 147          $this->view->date_info->month = isset($argument->month) ? $argument->month: NULL;
 148          $this->view->date_info->day = isset($argument->day) ? $argument->day : NULL;
 149          $this->view->date_info->week = isset($argument->week) ? $argument->week : NULL;
 150          $this->view->date_info->min_date = $argument->min_date;
 151          $this->view->date_info->max_date = $argument->max_date;
 152          $this->view->date_info->min_date_date = date_format($this->view->date_info->min_date, DATE_FORMAT_DATE);
 153          $this->view->date_info->max_date_date = date_format($this->view->date_info->max_date, DATE_FORMAT_DATE);
 154          $this->view->date_info->forbid = isset($argument->forbid) ? $argument->forbid : FALSE;
 155          
 156          // Stop after the first date argument, if there is more than one.
 157          break;
 158        }
 159        $i++;
 160      }
 161      $this->view->date_info->display_types = $this->display_types();
 162      $keys = drupal_map_assoc(array_keys(calendar_display_types()));
 163      $this->view->date_info->calendar_type = $keys[$this->view->date_info->granularity];
 164      
 165      // bring the node type into the query so we can use it in the theme
 166      if ($this->view->base_table == 'node') {
 167        $this->view->query->add_field('node', 'type');
 168      }
 169        
 170      parent::query();
 171    }
 172    
 173    /**
 174     * Render the calendar navigation style.
 175     */
 176    function render() {
 177      return theme($this->theme_functions(), $this->view, $this->options, array());
 178    }
 179  }
 180  


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