Projet

Général

Profil

Paste
Télécharger (11,5 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2
/**
3
 * @file
4
 * Date pager.
5
 * Works with a Date argument, the argument filters the view and the pager provides back/next navigation.
6
 *
7
 * USER NOTES:
8
 *
9
 * To use this, add a pager to a view, and choose the option to 'Page by date'.
10
 * There are several settings:
11
 * - The pager id: Set an id to be used as the identifier in the url for pager values, defaults to 'date'.
12
 * - Pager position: Choose whether to display the date pager above, below, or both above and below the content.
13
 * - Link format: Choose whether the pager links will be in the simple 'calendar/2011-12' format or the
14
 *   more complex 'calendar/?date=2011-12' pager format. The second one is more likely to work correctly
15
 *   if the pager is used in blocks and panels.
16
 *
17
 * The pager works in combination with a Date argument and it will use the date fields and granularity
18
 * set in that argument to create its back/next links. If the view has no Date argument, the pager can
19
 * do nothing. The argument can either be a 'Date' argument that lets you select one or more date fields
20
 * in the argument, or the simple 'Content' argument for an individual date field. It must be an
21
 * argument that uses the date argument handler.
22
 *
23
 * DEVELOPER NOTES
24
 *
25
 * The pager could technically create a query of its own rather than depending on the date argument to
26
 * set the query, but it has only a limited set of tools to work with because it is a plugin, not a handler:
27
 * it has no knowledge about relationships, it cannot use the ensure_my_table() function,
28
 * plugins are not even invoked in pre_query(), so can't do anything there.
29
 *
30
 * My conclusion was that the date pager simply is not powerful enough to create its own queries for
31
 * date fields, which require very complex queries. Instead, we can combine this with a date argument and
32
 * let the argument create the query and let the pager just provide the back/next links. If there is no
33
 * date argument, the pager will do nothing.
34
 *
35
 * There are still other problems. The pager is not even initialized until after all the handlers
36
 * have created their queries, so it has no chance to alter values ahead of that. And the argument
37
 * has no knowledge of the pager, so it can't check for pager values before the query is created.
38
 *
39
 * The solution used here is to let the argument create the original query. The pager query
40
 * runs after that, so the pager checks to see if there is a pager value that needs to be used in the query.
41
 * The date argument has identified the placeholders it used in the query. So if a change is needed,
42
 * we can swap the pager value into the query created by the date argument and adjust the
43
 * $view->date_info values set by the argument accordingly so the theme will pick up the new information.
44
 */
45

    
46
/**
47
 * Example plugin to handle paging by month.
48
 */
49
class date_views_plugin_pager extends views_plugin_pager {
50

    
51
  /**
52
   * This kind of pager does not need to count the number of records.
53
   */
54
  function use_count_query() {
55
    return FALSE;
56
  }
57

    
58
  /**
59
   * Because we don't know how many pages there are, we never believe there are more records.
60
   */
61
  function has_more_records() {
62
    return FALSE;
63
  }
64

    
65
  /*
66
   * Tell Views what this pager's setting is.
67
   */
68
  function summary_title() {
69
    return t("Position: @position, format: @format.", array('@position' => $this->options['pager_position'], '@format' => $this->options['link_format']));
70
  }
71

    
72
  /**
73
   * Tell Views what options this plugin can store.
74
   */
75
  function option_definition() {
76
    $options = parent::option_definition();
77
    $options['date_id'] = array('default' => 'date');
78
    $options['pager_position'] = array('default' => 'top');
79
    $options['link_format'] = array('default' => 'pager');
80
    $options['date_argument'] = array('default' => 'Unknown');
81
    $options['granularity'] = array('default' => 'Unknown');
82
    return $options;
83
  }
84

    
85
  /*
86
   * Provide the form for setting options.
87
   */
88
  function options_form(&$form, &$form_state) {
89
    $form['markup']['#markup'] = t('This pager works together with a Date or Content date field contextual filter. If a Date filter has been added to the view, this pager will provide back/next paging to match the granularity of that filter (i.e. paging by year, month, week, or day). The filter must also be configured to use a DATE default value. If there is no Date contextual filter on this view, or if it has not been set to use a default date, the pager will not appear.');
90
    $form['date_id'] = array(
91
      '#title' => t('Date identifier'),
92
      '#type' => 'textfield',
93
      '#description' => t('The query identifier to use when fetching date data from in the URL. Note that if you have more than one display in the same view that uses the date pager (like a page and a block), the pager id must be different for each one or both will change when the pager value changes.'),
94
      '#default_value' => $this->options['date_id'],
95
      '#required' => TRUE,
96
    );
97
    $form['pager_position'] = array(
98
      '#title' => t('Pager position'),
99
      '#type' => 'select',
100
      '#options' => array('bottom' => t('Bottom'), 'top' => t('Top'), 'both' => t('Both')),
101
      '#description' => t('Where to place the date pager, on the top, bottom, or both top and bottom of the content.'),
102
      '#default_value' => $this->options['pager_position'],
103
      '#required' => TRUE,
104
    );
105
    $form['link_format'] = array(
106
      '#title' => t('Link format'),
107
      '#type' => 'select',
108
      '#options' => array('pager' => t('Pager'), 'clean' => t('Clean URL')),
109
      '#description' => t("The format for pager link urls. With the Pager format, the links look like 'calendar/?date=2020-05'. The Clean URL format links look like 'calendar/2020-05'. The Clean format links look nicer but the Pager format links are likely to work better if the calendar is used in blocks or panels."),
110
      '#default_value' => $this->options['link_format'],
111
      '#required' => TRUE,
112
    );
113
    $form['date_argument']['#type'] = 'hidden';
114
    $form['date_argument']['#value'] = $this->options['date_argument'];
115
    $form['granularity']['#type'] = 'hidden';
116
    $form['granularity']['#value'] = $this->options['granularity'];
117
  }
118

    
119
  /**
120
   * Transfer date information from the argument to the view so the pager theme can use it
121
   * and update the date argument value to whatever is set by the pager.
122
   */
123
  function query() {
124

    
125
    // By fetching our data from the exposed input, it is possible to
126
    // feed pager data through some method other than $_GET.
127
    $input = $this->view->get_exposed_input();
128
    $value = NULL;
129
    if (!empty($input) && !empty($input[$this->options['date_id']])) {
130
      $value = $input[$this->options['date_id']];
131
    }
132

    
133
    // Bring the argument information into the view so our theme can access it.
134
    $i = 0;
135
    foreach ($this->view->argument as $id => &$argument) {
136
      if (date_views_handler_is_date($argument, 'argument')) {
137

    
138
        // If the argument is empty, nothing to do. This could be from
139
        // an argument that does not set a default value.
140
        if (empty($argument->argument) || empty($argument->date_handler)) {
141
          continue;
142
        }
143

    
144
        // Storing this information in the pager so it's available for summary info.
145
        // The view argument information is not otherwise accessible to the pager.
146
        // Not working right yet, tho.
147
        $date_handler = $argument->date_handler;
148
        $this->options['date_argument'] = $id;
149
        $this->options['granularity'] = $argument->date_handler->granularity;
150

    
151
        // Reset values set by argument if pager requires it.
152
        if (!empty($value)) {
153
          $argument->argument = $value;
154
          $argument->date_range = $argument->date_handler->arg_range($value);
155
          $argument->min_date = $argument->date_range[0];
156
          $argument->max_date = $argument->date_range[1];
157
          // $argument->is_default works correctly for normal arguments, but does not
158
          // work correctly if we are swapping in a new value from the pager.
159
          $argument->is_default = FALSE;
160
        }
161

    
162
        // The pager value might move us into a forbidden range, so test it.
163
        if ($this->date_forbid($argument)) {
164
          $this->view->build_info['fail'] = TRUE;
165
          return;
166
        }
167

    
168
        if (empty($this->view->date_info)) $this->view->date_info = new stdClass();
169
        $this->view->date_info->granularity = $argument->date_handler->granularity;
170
        $format = $this->view->date_info->granularity == 'week' ? DATE_FORMAT_DATETIME : $argument->sql_format;
171
        $this->view->date_info->placeholders = isset($argument->placeholders) ? $argument->placeholders : $argument->date_handler->placeholders;
172
        $this->view->date_info->date_arg = $argument->argument;
173
        $this->view->date_info->date_arg_pos = $i;
174
        $this->view->date_info->year = date_format($argument->min_date, 'Y');
175
        $this->view->date_info->month = date_format($argument->min_date, 'n');;
176
        $this->view->date_info->day = date_format($argument->min_date, 'j');
177
        $this->view->date_info->week = date_week(date_format($argument->min_date, DATE_FORMAT_DATE));
178
        $this->view->date_info->date_range = $argument->date_range;
179
        $this->view->date_info->min_date = $argument->min_date;
180
        $this->view->date_info->max_date = $argument->max_date;
181
        $this->view->date_info->limit = $argument->limit;
182
        $this->view->date_info->url = $this->view->get_url();
183
        $this->view->date_info->pager_id = $this->options['date_id'];
184
        $this->view->date_info->date_pager_position = $this->options['pager_position'];
185
        $this->view->date_info->date_pager_format = $this->options['link_format'];
186
      }
187
      $i++;
188
    }
189

    
190
    // Is this a view that needs to be altered based on a pager value?
191
    // If there is pager input and the argument has set the placeholders,
192
    // swap the pager value in for the placeholder set by the argument.
193
    if (!empty($value) && !empty($this->view->date_info->placeholders)) {
194
      $placeholders = $this->view->date_info->placeholders;
195
      $count = count($placeholders);
196
      foreach ($this->view->query->where as $group => $data) {
197
        foreach ($data['conditions'] as $delta => $condition) {
198
          if (array_key_exists('value', $condition) && is_array($condition['value'])) {
199
            foreach ($condition['value'] as $placeholder => $placeholder_value) {
200
              if (array_key_exists($placeholder, $placeholders)) {
201
                // If we didn't get a match, this is a > $min < $max query that uses the view
202
                // min and max dates as placeholders.
203
                $date = ($count == 2) ? $this->view->date_info->min_date : $this->view->date_info->max_date;
204
                $next_placeholder = array_shift($placeholders);
205
                $this->view->query->where[$group]['conditions'][$delta]['value'][$placeholder] = $date->format($format);
206
                $count--;
207
              }
208
            }
209
          }
210
        }
211
      }
212
    }
213
  }
214

    
215
  /**
216
   * Add a callback to determine if we have moved outside the valid date range for this argument.
217
   */
218
  function date_forbid($argument) {
219
    // See if we're outside the allowed date range for our argument.
220
    $limit = date_range_years($argument->options['year_range']);
221
    if (date_format($argument->min_date, 'Y') < $limit[0] || date_format($argument->max_date, 'Y') > $limit[1]) {
222
      return TRUE;
223
    }
224
    return FALSE;
225
  }
226

    
227

    
228
  function render($input) {
229
    // This adds all of our template suggestions based upon the view name and display id.
230
    $pager_theme = views_theme_functions('date_views_pager', $this->view, $this->display);
231
    return theme($pager_theme, array('plugin' => $this, 'input' => $input));
232
  }
233
}