[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/date/includes/ -> date_api.views.inc (source)

   1  <?php
   2  //$Id: date_api.views.inc,v 1.1.2.12 2010/08/12 14:13:59 karens Exp $
   3  
   4  /**
   5   * @file
   6   * Defines date-related Views data and plugins:
   7   * 
   8   * Date argument:
   9   *   A generic date argument that has an option to select one or more 
  10   *   Views date fields to filter on, automatically adds them to the view, 
  11   *   and then filters the view by the value of the selected field(s). 
  12   *   The flexible argument will accept and evaluate most ISO date
  13   *   and period formats, like 2009-05-01, 2008-W25, P1W. 
  14   * 
  15   * Date filter:
  16   *   A generic date filter that has an option to select a 
  17   *   Views date field to filter on, with a choice of a widget to use
  18   *   for the filter form and an option to set the default value to 
  19   *   a set date or something like 'now +90 days'. If the operator is 
  20   *   set to 'between' or 'not between' you can set a default value for
  21   *   both the from and to dates. 
  22   * 
  23   * Current date argument default
  24   *   Adds a default option to set the argument to the current date 
  25   *   when the argument is empty.
  26   * 
  27   * Date navigation attachment
  28   *   Navigation that can be attached to any display to create back/next 
  29   *   links by date, requires the date argument and uses the current
  30   *   date argument default to set a starting point for the view.
  31   */
  32  /**
  33   * Implementation of hook_views_handlers() to register all of the basic handlers
  34   * views uses.
  35   */
  36  function date_api_views_handlers() {
  37    return array(
  38      'info' => array(
  39        'path' => drupal_get_path('module', 'date_api') .'/includes',
  40        ),
  41      'handlers' => array(
  42        'date_api_argument_handler' => array(
  43          'parent' => 'views_handler_argument_date',
  44        ),
  45        'date_api_filter_handler' => array(
  46          'parent' => 'views_handler_filter_numeric',
  47        ),
  48      ),
  49    );
  50  }
  51  
  52  /**
  53   * Implementation of hook_views_plugins
  54   */
  55  function date_api_views_plugins() {
  56    $path = drupal_get_path('module', 'date_api');
  57    $views_path = drupal_get_path('module', 'views');
  58    require_once "./$path/theme/theme.inc";
  59    return array(
  60      'module' => 'date_api', // This just tells our themes are elsewhere.
  61      'display' => array(
  62        // Parents are not really displays, just needed so the files can
  63        // be included.
  64        'parent' => array(
  65          'no ui' => TRUE,
  66          'handler' => 'views_plugin_display',
  67          'path' => "$views_path/plugins",
  68          'parent' => '',
  69        ),
  70        'attachment' => array(
  71          'no ui' => TRUE,
  72          'handler' => 'views_plugin_display_attachment',
  73          'path' => "$views_path/plugins",
  74          'parent' => 'parent',
  75        ),
  76        // Display plugin for date navigation.
  77        'date_nav' => array(
  78          'title' => t('Date browser'),
  79          'help' => t('Date back/next navigation to attach to other displays. Requires the Date argument.'),
  80          'handler' => 'date_plugin_display_attachment',
  81          'parent' => 'attachment',
  82          'path' => "$path/includes",
  83          'theme' => 'views_view',
  84          'use ajax' => TRUE,
  85          'admin' => t('Date browser'),
  86          'help topic' => 'date-browser',
  87        ),
  88      ),
  89      'style' => array(
  90        'parent' => array(
  91          // this isn't really a display but is necessary so the file can
  92          // be included.
  93          'no ui' => TRUE,
  94          'handler' => 'views_plugin_style',
  95          'path' => "$views_path/plugins",
  96          'theme file' => 'theme.inc',
  97          'theme path' => "$views_path/theme",
  98          'parent' => '',
  99        ),
 100        'date_nav' => array(
 101          'title' => t('Date browser style'),
 102          'help' => t('Creates back/next navigation.'),
 103          'handler' => 'date_navigation_plugin_style',
 104          'path' => "$path/includes",
 105          'parent' => 'parent',
 106          'theme' => 'date_navigation',
 107          'theme file' => 'theme.inc',
 108          'theme path' => "$path/theme",
 109          'uses row plugin' => FALSE,
 110          'uses fields' => FALSE,
 111          'uses options' => TRUE,
 112          'type' => 'date_nav', 
 113          'even empty' => TRUE,
 114        ),
 115      ),
 116    );
 117  }
 118  
 119  /**
 120   * Implementation of hook_views_data()
 121   */
 122  function date_api_views_data() {
 123    $data = array();
 124    
 125    $tables = module_invoke_all('date_api_tables');
 126    
 127    foreach ($tables as $base_table) {
 128      // The flexible date argument.
 129      $data[$base_table]['date_argument'] = array(
 130        'group' => t('Date'),
 131        'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
 132        'help' => t('Filter any Views !base_table date field by a date argument, using any common ISO date/period format (i.e. YYYY, YYYY-MM, YYYY-MM-DD, YYYY-W99, YYYY-MM-DD--P3M, P90D, etc).', array('!base_table' => $base_table)),
 133        'argument' => array(
 134          'handler' => 'date_api_argument_handler',
 135          'empty field name' => t('Undated'),
 136          'base' => $base_table,
 137        ),
 138      );  
 139      // The flexible date filter.
 140      $data[$base_table]['date_filter'] = array(
 141        'group' => t('Date'),
 142        'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
 143        'help' => t('Filter any Views !base_table date field.', array('!base_table' => $base_table)),
 144        'filter' => array(
 145          'handler' => 'date_api_filter_handler',
 146          'empty field name' => t('Undated'),
 147          'base' => $base_table,
 148        ),
 149      ); 
 150    }
 151    return $data;
 152  }
 153  
 154  /**
 155   *  Identify all potential date/timestamp fields and cache the data.
 156   */
 157  function date_api_fields($base = 'node', $reset = FALSE) {
 158    static $fields = array();
 159    $empty = array('name' => array(), 'alias' => array());
 160    require_once('./'. drupal_get_path('module', 'date_api') .'/includes/date_api_fields.inc');
 161    if (empty($fields[$base]) || $reset) {
 162      $cid = 'date_api_fields_'. $base;
 163      if (!$reset && $cached = cache_get($cid, 'cache_views')) {
 164        $fields[$base] = $cached->data;
 165      }
 166      else {
 167        $fields[$base] = _date_api_fields($base);
 168      }
 169    }
 170    // Make sure that empty values will be arrays in he expected format.
 171    return !empty($fields) && !empty($fields[$base]) ? $fields[$base] : $empty;
 172  }
 173  
 174  /**
 175   * Central function for setting up the right timezone values
 176   * in the SQL date handler.
 177   * 
 178   * The date handler will use this information to decide if the
 179   * database value needs a timezone conversion.
 180   * 
 181   * In Views, we will always be comparing to a local date value,
 182   * so the goal is to convert the database value to the right
 183   * value to compare to the local value.
 184   */
 185  function date_views_set_timezone(&$date_handler, &$view, $field) {
 186    $tz_handling  = $field['tz_handling'];
 187    switch ($tz_handling) {
 188      case 'date' :
 189        $date_handler->db_timezone = 'UTC';
 190        $date_handler->local_timezone_field = $field['timezone_field'];
 191        $date_handler->offset_field = $field['offset_field'];
 192        break;
 193      case 'none':
 194        $date_handler->db_timezone = date_default_timezone_name();
 195        $date_handler->local_timezone = date_default_timezone_name();
 196        break;
 197      case 'utc':
 198        $date_handler->db_timezone = 'UTC';
 199        $date_handler->local_timezone = 'UTC';
 200        break;
 201      default :
 202        $date_handler->db_timezone = 'UTC';
 203        $date_handler->local_timezone = date_default_timezone_name();
 204        break;
 205    }
 206  }
 207  
 208  function date_views_querystring($view, $extra_params = array()) {
 209    $query_params = array_merge($_GET, $extra_params);
 210    // Allow NULL params to be removed from the query string.
 211    foreach ($extra_params AS $key => $value) {
 212      if (!isset($value)) {
 213        unset($query_params[$key]);
 214      }
 215    }
 216    // Filter the special "q" and "view" variables out of the query string.
 217    $exclude = array('q');
 218    // If we don't explicitly add a value for "view", filter it out.
 219    if (empty($extra_params['view'])) {
 220      $exclude[] = 'view';
 221    }
 222    $query = drupal_query_string_encode($query_params, $exclude);
 223    // To prevent an empty query string from adding a "?" on to the end of a URL,
 224    // we return NULL.
 225    return !empty($query) ? $query : NULL;
 226  }
 227  
 228  /**
 229   * Identify the base url of the page,
 230   * needed when the calendar is embedded so we
 231   * don't set the url to the calendar url.
 232   */
 233  function date_views_page_url($view) {
 234    if ($view->build_type == 'page') {
 235      return date_views_real_url($view, $view->date_info->real_args);
 236    }
 237    else {
 238      $block_identifier = isset($view->date_info->block_identifier) ? $view->date_info->block_identifier : 'mini';
 239      return url($_GET['q'], date_views_querystring($view, array($block_identifier => NULL)), NULL, TRUE);
 240    }
 241  }
 242  
 243  /**
 244   * Figure out what the URL of the calendar view we're currently looking at is.
 245   */
 246  function date_views_real_url($view, $args) {
 247    if (empty($args)) {
 248      return $view->date_info->url;
 249    }
 250    // Add non-calendar arguments to the base url.
 251    $parts = explode('/', $view->date_info->url);
 252    $bump = 0;
 253    foreach ($parts as $delta => $part) {
 254      // If one of the args is buried in the url, add it here and adjust
 255      // the delta values we'll compare the calendar arg positions to.
 256      if (substr($part, 0, 1) == '$') {
 257        $parts[$delta] = array_shift($args);
 258        $bump++;
 259      }
 260    }
 261    foreach ($args as $delta => $arg) {
 262      if (!in_array($delta + $bump, calendar_arg_positions($view)) && !empty($arg)) {
 263        array_push($parts, $arg);
 264      }
 265    }
 266    return implode('/', $parts);
 267  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7