Projet

Général

Profil

Paste
Télécharger (6,76 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_views / includes / date_views.views.inc @ db9ffd17

1
<?php
2
/**
3
 * @file
4
 * Defines date-related Views data and plugins:
5
 *
6
 * Date argument:
7
 *   A generic date argument that has an option to select one or more
8
 *   Views date fields to filter on, automatically adds them to the view,
9
 *   and then filters the view by the value of the selected field(s).
10
 *   The flexible argument will accept and evaluate most ISO date
11
 *   and period formats, like 2009-05-01, 2008-W25, P1W.
12
 *
13
 * Date filter:
14
 *   A generic date filter that has an option to select a
15
 *   Views date field to filter on, with a choice of a widget to use
16
 *   for the filter form and an option to set the default value to
17
 *   a set date or something like 'now +90 days' . If the operator is
18
 *   set to 'between' or 'not between' you can set a default value for
19
 *   both the start and end dates.
20
 *
21
 * Current date argument default
22
 *   Adds a default option to set the argument to the current date
23
 *   when the argument is empty.
24
 *
25
 * Date navigation attachment
26
 *   Navigation that can be attached to any display to create back/next
27
 *   links by date, requires the date argument and uses the current
28
 *   date argument default to set a starting point for the view.
29
 */
30
/**
31
 * Implements hook_views_plugins
32
 */
33
function date_views_views_plugins() {
34
  $path = drupal_get_path('module', 'date_views');
35
  $views_path = drupal_get_path('module', 'views');
36
  module_load_include('inc', 'date_views', 'theme/theme');
37

    
38
  return array(
39
    'module' => 'date_views', // This just tells our themes are elsewhere.
40
    'display' => array(
41
      // Display plugin for date navigation.
42
      'date_nav' => array(
43
        'title' => t('Date browser'),
44
        'help' => t('Date back/next navigation to attach to other displays. Requires the Date argument.'),
45
        'handler' => 'date_plugin_display_attachment',
46
        'parent' => 'attachment',
47
        'path' => "$path/includes",
48
        'theme' => 'views_view',
49
        'use ajax' => TRUE,
50
        'admin' => t('Date browser'),
51
        'help topic' => 'date-browser',
52
      ),
53
    ),
54

    
55
    'pager' => array(
56
      'date_views_pager' => array(
57
        'title' => t('Page by date'),
58
        'help' => t('Page using the value of a date field.'),
59
        'handler' => 'date_views_plugin_pager',
60
        'path' => "$path/includes",
61
        'help topic' => 'date-views-pager',
62
        'uses options' => TRUE,
63
      ),
64
    ),
65

    
66
    'style' => array(
67
      'date_nav' => array(
68
        'title' => t('Date browser style'),
69
        'help' => t('Creates back/next navigation.'),
70
        'handler' => 'date_navigation_plugin_style',
71
        'path' => "$path/includes",
72
        'theme' => 'date_navigation',
73
        'theme file' => 'theme.inc',
74
        'theme path' => "$path/theme",
75
        'uses row plugin' => FALSE,
76
        'uses fields' => FALSE,
77
        'uses options' => TRUE,
78
        'type' => 'date_nav',
79
        'even empty' => TRUE,
80
      ),
81
    ),
82
  );
83
}
84

    
85
/**
86
 * Implements hook_views_data()
87
 */
88
function date_views_views_data() {
89
  $data = array();
90

    
91
  $tables = date_views_base_tables();
92

    
93
  foreach ($tables as $base_table => $entity) {
94
    // The flexible date argument.
95
    $data[$base_table]['date_argument'] = array(
96
      'group' => t('Date'),
97
      'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
98
      '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)),
99
      'argument' => array(
100
        'handler' => 'date_views_argument_handler',
101
        'empty field name' => t('Undated'),
102
        'is date' => TRUE,
103
        //'skip base' => $base_table,
104
      ),
105
    );
106
    // The flexible date filter.
107
    $data[$base_table]['date_filter'] = array(
108
      'group' => t('Date'),
109
      'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
110
      'help' => t('Filter any Views !base_table date field.', array('!base_table' => $base_table)),
111
      'filter' => array(
112
        'handler' => 'date_views_filter_handler',
113
        'empty field name' => t('Undated'),
114
        'is date' => TRUE,
115
        //'skip base' => $base_table,
116
      ),
117
    );
118
  }
119

    
120
  return $data;
121
}
122

    
123
/**
124
 * Implements hook_views_data_alter().
125
 */
126
function date_views_views_data_alter(&$data) {
127

    
128
  // Mark all the core date handlers as date fields.
129
  // This will identify all handlers that directly use the _date handlers,
130
  // will not pick up any that extend those handlers.
131
  foreach ($data as $base_table => &$table) {
132
    foreach ($table as $id => &$field) {
133
      foreach (array('field', 'sort', 'filter', 'argument') as $type) {
134
        if (isset($field[$type]) && isset($field[$type]['handler']) && ($field[$type]['handler'] == 'views_handler_' . $type . '_date')) {
135
          $field[$type]['is date'] = TRUE;
136
        }
137
      }
138
    }
139
  }
140
}
141

    
142
/**
143
 * Central function for setting up the right timezone values
144
 * in the SQL date handler.
145
 *
146
 * The date handler will use this information to decide if the
147
 * database value needs a timezone conversion.
148
 *
149
 * In Views, we will always be comparing to a local date value,
150
 * so the goal is to convert the database value to the right
151
 * value to compare to the local value.
152
 */
153
function date_views_set_timezone(&$date_handler, &$view, $field) {
154
  switch ($field['tz_handling']) {
155
    case 'date' :
156
      $date_handler->db_timezone = 'UTC';
157
      $date_handler->local_timezone_field = $field['timezone_field'];
158
      $date_handler->offset_field = $field['offset_field'];
159
      break;
160
    case 'none':
161
      $date_handler->db_timezone = date_default_timezone();
162
      $date_handler->local_timezone = date_default_timezone();
163
      break;
164
    case 'utc':
165
      $date_handler->db_timezone = 'UTC';
166
      $date_handler->local_timezone = 'UTC';
167
      break;
168
    default :
169
      $date_handler->db_timezone = 'UTC';
170
      $date_handler->local_timezone = date_default_timezone();
171
      break;
172
  }
173
}
174

    
175
function date_views_querystring($view, $extra_params = array()) {
176
  $query_params = array_merge($_GET, $extra_params);
177
  // Allow NULL params to be removed from the query string.
178
  foreach ($extra_params AS $key => $value) {
179
    if (!isset($value)) {
180
      unset($query_params[$key]);
181
    }
182
  }
183
  // Filter the special "q" and "view" variables out of the query string.
184
  $exclude = array('q');
185
  // If we don't explicitly add a value for "view", filter it out.
186
  if (empty($extra_params['view'])) {
187
    $exclude[] = 'view';
188
  }
189
  // Clear out old date pager settings if not explicitly added.
190
  if (!empty($view->date_info->pager_id) && empty($extra_params[$view->date_info->pager_id])) {
191
    $exclude[] = $view->date_info->pager_id;
192
  }
193

    
194
  $query = drupal_get_query_parameters($query_params, $exclude);
195
  // To prevent an empty query string from adding a "?" on to the end of a URL,
196
  // we return NULL.
197
  return !empty($query) ? $query : NULL;
198
}