Projet

Général

Profil

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

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

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
/**
32
 * Implements hook_views_plugins().
33
 */
34
function date_views_views_plugins() {
35
  $path = drupal_get_path('module', 'date_views');
36
  $views_path = drupal_get_path('module', 'views');
37
  module_load_include('inc', 'date_views', 'theme/theme');
38

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

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

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

    
87
/**
88
 * Implements hook_views_data().
89
 */
90
function date_views_views_data() {
91
  $data = array();
92

    
93
  $tables = date_views_base_tables();
94

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

    
122
  return $data;
123
}
124

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

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

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

    
164
    case 'none':
165
      $date_handler->db_timezone = date_default_timezone();
166
      $date_handler->local_timezone = date_default_timezone();
167
      break;
168

    
169
    case 'utc':
170
      $date_handler->db_timezone = 'UTC';
171
      $date_handler->local_timezone = 'UTC';
172
      break;
173

    
174
    default:
175
      $date_handler->db_timezone = 'UTC';
176
      $date_handler->local_timezone = date_default_timezone();
177
      break;
178
  }
179
}
180

    
181
/**
182
 * Helper function to generate a query string.
183
 *
184
 * @param object $view
185
 *   A View object.
186
 *
187
 * @param array $extra_params
188
 *   An extra parameters.
189
 *
190
 * @return null/string
191
 *   Return a query or NULL.
192
 */
193
function date_views_querystring($view, $extra_params = array()) {
194
  $query_params = array_merge($_GET, $extra_params);
195
  // Allow NULL params to be removed from the query string.
196
  foreach ($extra_params as $key => $value) {
197
    if (!isset($value)) {
198
      unset($query_params[$key]);
199
    }
200
  }
201
  // Filter the special "q" and "view" variables out of the query string.
202
  $exclude = array('q');
203
  // If we don't explicitly add a value for "view", filter it out.
204
  if (empty($extra_params['view'])) {
205
    $exclude[] = 'view';
206
  }
207
  // Clear out old date pager settings if not explicitly added.
208
  if (!empty($view->date_info->pager_id) && empty($extra_params[$view->date_info->pager_id])) {
209
    $exclude[] = $view->date_info->pager_id;
210
  }
211

    
212
  $query = drupal_get_query_parameters($query_params, $exclude);
213
  // To prevent an empty query string from adding a "?" on to the end of a URL,
214
  // we return NULL.
215
  return !empty($query) ? $query : NULL;
216
}