Révision b720ea3e
Ajouté par Assos Assos il y a plus de 8 ans
drupal7/sites/all/modules/date/date_views/includes/date_views_argument_handler_simple.inc | ||
---|---|---|
7 | 7 |
/** |
8 | 8 |
* Date API argument handler. |
9 | 9 |
*/ |
10 |
// @codingStandardsIgnoreStart |
|
10 | 11 |
class date_views_argument_handler_simple extends views_handler_argument_date { |
11 | 12 |
|
12 | 13 |
/** |
13 |
* Get granularity and use it to create the formula and a format |
|
14 |
* for the results. |
|
14 |
* Get granularity. |
|
15 |
* |
|
16 |
* Use it to create the formula and a format for the results. |
|
15 | 17 |
*/ |
16 | 18 |
function init(&$view, &$options) { |
17 | 19 |
parent::init($view, $options); |
... | ... | |
29 | 31 |
$this->date_handler->local_timezone = date_get_timezone($field['settings']['tz_handling']); |
30 | 32 |
} |
31 | 33 |
$this->date_handler->granularity = $this->options['granularity']; |
32 |
// This value needs to be initialized so it exists even if the query doesn't run. |
|
34 |
// This value needs to be initialized so |
|
35 |
// it exists even if the query doesn't run. |
|
33 | 36 |
$this->date_handler->placeholders = array(); |
34 | 37 |
|
35 | 38 |
$this->format = $this->date_handler->views_formats($this->date_handler->granularity, 'display'); |
36 | 39 |
$this->sql_format = $this->date_handler->views_formats($this->date_handler->granularity, 'sql'); |
37 |
// $this->arg_format is the format the parent date handler will use to create a default argument. |
|
40 |
// $this->arg_format is the format the parent date |
|
41 |
// handler will use to create a default argument. |
|
38 | 42 |
$this->arg_format = $this->format(); |
39 | 43 |
|
40 | 44 |
// Identify the base table for this field. |
... | ... | |
43 | 47 |
|
44 | 48 |
} |
45 | 49 |
|
50 |
/** |
|
51 |
* {@inheritdoc} |
|
52 |
*/ |
|
46 | 53 |
function format() { |
47 | 54 |
if (!empty($this->options['granularity'])) { |
48 | 55 |
return $this->date_handler->views_formats($this->options['granularity']); |
... | ... | |
53 | 60 |
} |
54 | 61 |
|
55 | 62 |
/** |
56 |
* Set the empty argument value to the current date, |
|
57 |
* formatted appropriately for this argument. |
|
63 |
* Set the empty argument value to the current date. |
|
64 |
* |
|
65 |
* Formatted appropriately for this argument. |
|
58 | 66 |
*/ |
59 | 67 |
function get_default_argument($raw = FALSE) { |
60 | 68 |
$is_default = FALSE; |
... | ... | |
85 | 93 |
$options = parent::option_definition(); |
86 | 94 |
$options['year_range'] = array('default' => '-3:+3'); |
87 | 95 |
$options['granularity'] = array('default' => 'month'); |
96 |
$options['granularity_reset'] = array('default' => FALSE); |
|
88 | 97 |
$options['default_argument_type']['default'] = 'date'; |
89 | 98 |
$options['add_delta'] = array('default' => ''); |
90 | 99 |
$options['use_fromto'] = array('default' => ''); |
... | ... | |
116 | 125 |
'#attributes' => array('class' => array('dependent-options')), |
117 | 126 |
'#states' => array( |
118 | 127 |
'visible' => array( |
119 |
':input[name="options[default_action]"]' => array('value' => 'summary') |
|
128 |
':input[name="options[default_action]"]' => array( |
|
129 |
'value' => 'summary', |
|
130 |
), |
|
120 | 131 |
), |
121 | 132 |
), |
122 | 133 |
); |
... | ... | |
129 | 140 |
'#attributes' => array('class' => array('dependent-options')), |
130 | 141 |
'#states' => array( |
131 | 142 |
'visible' => array( |
132 |
':input[name="options[title_format]"]' => array('value' => 'custom') |
|
143 |
':input[name="options[title_format]"]' => array( |
|
144 |
'value' => 'custom', |
|
145 |
), |
|
133 | 146 |
), |
134 | 147 |
), |
135 | 148 |
); |
136 | 149 |
|
150 |
// Get default granularity options |
|
137 | 151 |
$options = $this->date_handler->date_parts(); |
138 |
unset($options['second'], $options['minute']); |
|
139 |
$options += array('week' => t('Week', array(), array('context' => 'datetime'))); |
|
152 |
// Add the 'week' option. |
|
153 |
$options += array( |
|
154 |
'week' => t('Week', array(), array( |
|
155 |
'context' => 'datetime', |
|
156 |
)), |
|
157 |
); |
|
158 |
|
|
140 | 159 |
$form['granularity'] = array( |
141 | 160 |
'#title' => t('Granularity'), |
142 | 161 |
'#type' => 'radios', |
143 | 162 |
'#options' => $options, |
144 | 163 |
'#default_value' => $this->options['granularity'], |
145 |
'#multiple' => TRUE, |
|
146 | 164 |
'#description' => t("Select the type of date value to be used in defaults, summaries, and navigation. For example, a granularity of 'month' will set the default date to the current month, summarize by month in summary views, and link to the next and previous month when using date navigation."), |
147 | 165 |
); |
148 | 166 |
|
167 |
$form['granularity_reset'] = array( |
|
168 |
'#title' => t('Use granularity from argument value'), |
|
169 |
'#type' => 'checkbox', |
|
170 |
'#default_value' => $this->options['granularity_reset'], |
|
171 |
'#description' => t("If the granularity of argument value is different from selected, use it from argument value."), |
|
172 |
); |
|
173 |
|
|
149 | 174 |
$form['year_range'] = array( |
150 | 175 |
'#title' => t('Date year range'), |
151 | 176 |
'#type' => 'textfield', |
... | ... | |
172 | 197 |
'#default_value' => $this->options['add_delta'], |
173 | 198 |
'#options' => array('' => t('No'), 'yes' => t('Yes')), |
174 | 199 |
'#description' => t('Add an identifier to the view to show which multiple value date fields meet the filter criteria. Note: This option may introduce duplicate values into the view. Required when using multiple value fields in a Calendar or any time you want the node view of multiple value dates to display only the values that match the view filters.'), |
175 |
// Only let mere mortals tweak this setting for multi-value fields |
|
200 |
// Only let mere mortals tweak this setting for multi-value fields.
|
|
176 | 201 |
'#access' => $access, |
177 | 202 |
); |
178 |
|
|
179 | 203 |
} |
180 | 204 |
|
205 |
/** |
|
206 |
* {@inheritdoc} |
|
207 |
*/ |
|
181 | 208 |
function options_validate(&$form, &$form_state) { |
182 | 209 |
// It is very important to call the parent function here: |
183 | 210 |
parent::options_validate($form, $form_state); |
184 |
if (!preg_match('/^(?:\-[0-9]{1,4}|[0-9]{4}):(?:[\+|\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) {
|
|
211 |
if (!preg_match('/^(?:\-[0-9]{1,4}|[0-9]{4}):(?:[\+\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) { |
|
185 | 212 |
form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9')); |
186 | 213 |
} |
187 | 214 |
} |
... | ... | |
209 | 236 |
$format = !empty($this->options['title_format_custom']) && !empty($this->options['title_format_custom']) ? $this->options['title_format_custom'] : $this->date_handler->views_formats($this->options['granularity'], 'display'); |
210 | 237 |
$range = $this->date_handler->arg_range($this->argument); |
211 | 238 |
return date_format_date($range[0], 'custom', $format); |
212 |
} |
|
239 |
}
|
|
213 | 240 |
|
214 | 241 |
/** |
215 |
* Provide the argument to use to link from the summary to the next level; |
|
216 |
* this will be called once per row of a summary, and used as part of |
|
242 |
* Provide the argument to use to link from the summary to the next level. |
|
243 |
* |
|
244 |
* This will be called once per row of a summary, and used as part of |
|
217 | 245 |
* $view->get_url(). |
218 | 246 |
* |
219 |
* @param $data |
|
247 |
* @param object $data
|
|
220 | 248 |
* The query results for the row. |
221 | 249 |
*/ |
222 | 250 |
function summary_argument($data) { |
... | ... | |
234 | 262 |
*/ |
235 | 263 |
function summary_query() { |
236 | 264 |
|
237 |
// @TODO The summary values are computed by the database. Unless the database has |
|
238 |
// built-in timezone handling it will use a fixed offset, which will not be |
|
239 |
// right for all dates. The only way I can see to make this work right is to |
|
240 |
// store the offset for each date in the database so it can be added to the base |
|
265 |
// @TODO The summary values are computed by the database. |
|
266 |
// Unless the database has built-in timezone handling it will use |
|
267 |
// a fixed offset, which will not be right for all dates. |
|
268 |
// The only way I can see to make this work right is to store the offset |
|
269 |
// for each date in the database so it can be added to the base |
|
241 | 270 |
// date value before the database formats the result. Because this is a huge |
242 | 271 |
// architectural change, it won't go in until we start a new branch. |
243 | 272 |
$this->formula = $this->date_handler->sql_format($this->sql_format, $this->date_handler->sql_field("***table***.$this->real_field")); |
... | ... | |
245 | 274 |
// Now that our table is secure, get our formula. |
246 | 275 |
$formula = $this->get_formula(); |
247 | 276 |
|
248 |
// Add the field, give it an alias that does NOT match the actual field name or grouping won't work right. |
|
277 |
// Add the field, give it an alias that does NOT match the actual |
|
278 |
// field name or grouping won't work right. |
|
249 | 279 |
$this->base_alias = $this->name_alias = $this->query->add_field(NULL, $formula, $this->field . '_summary'); |
250 | 280 |
$this->query->set_count_field(NULL, $formula, $this->field); |
251 | 281 |
|
... | ... | |
254 | 284 |
|
255 | 285 |
/** |
256 | 286 |
* Inject a test for valid date range before the regular query. |
287 |
* |
|
257 | 288 |
* Override the parent query to be able to control the $group. |
258 | 289 |
*/ |
259 | 290 |
function query($group_by = FALSE) { |
260 | 291 |
|
261 |
// @TODO Not doing anything with $group_by yet, need to figure out what has to be done. |
|
292 |
// @TODO Not doing anything with $group_by yet, |
|
293 |
// need to figure out what has to be done. |
|
262 | 294 |
|
263 | 295 |
if ($this->date_forbid()) { |
264 | 296 |
return; |
265 | 297 |
} |
266 | 298 |
|
267 | 299 |
// See if we need to reset granularity based on an argument value. |
268 |
// Make sure we don't try to reset to some bogus value if someone has typed in an unexpected argument.
|
|
269 |
$granularity = $this->date_handler->arg_granularity($this->argument);
|
|
270 |
if (!empty($granularity)) {
|
|
300 |
// Make sure we don't try to reset to some bogus value if someone has |
|
301 |
// typed in an unexpected argument.
|
|
302 |
if ($this->options['granularity_reset'] && $granularity = $this->date_handler->arg_granularity($this->argument)) {
|
|
271 | 303 |
$this->date_handler->granularity = $granularity; |
272 | 304 |
$this->format = $this->date_handler->views_formats($this->date_handler->granularity, 'display'); |
273 | 305 |
$this->sql_format = $this->date_handler->views_formats($this->date_handler->granularity, 'sql'); |
... | ... | |
276 | 308 |
$this->ensure_my_table(); |
277 | 309 |
$group = !empty($this->options['date_group']) ? $this->options['date_group'] : 0; |
278 | 310 |
|
279 |
// If requested, add the delta field to the view so we can later find the value that matched our query. |
|
311 |
// If requested, add the delta field to the view so |
|
312 |
// we can later find the value that matched our query. |
|
280 | 313 |
if (!empty($this->options['add_delta']) && (substr($this->real_field, -6) == '_value' || substr($this->real_field, -7) == '_value2')) { |
281 | 314 |
$this->query->add_field($this->table_alias, 'delta'); |
282 | 315 |
$real_field_name = str_replace(array('_value', '_value2'), '', $this->real_field); |
... | ... | |
291 | 324 |
$view_max_placeholder = $this->placeholder(); |
292 | 325 |
$this->date_handler->placeholders = array($view_min_placeholder => $view_min, $view_max_placeholder => $view_max); |
293 | 326 |
|
294 |
// Are we comparing this field only or the Start/End date range to the view criteria? |
|
327 |
// Are we comparing this field only or the Start/End date range |
|
328 |
// to the view criteria? |
|
295 | 329 |
if (!empty($this->options['use_fromto'])) { |
296 | 330 |
|
297 | 331 |
// The simple case, match the field to the view range. |
... | ... | |
302 | 336 |
} |
303 | 337 |
else { |
304 | 338 |
|
305 |
// Look for the intersection of the range of the date field with the range of the view. |
|
306 |
// Get the Start/End values for this field. Retrieve using the original table name. |
|
307 |
// Swap the current table name (adjusted for relationships) into the query. |
|
308 |
// @TODO We may be able to use Views substitutions here, investigate that later. |
|
339 |
// Look for the intersection of the range |
|
340 |
// of the date field with the range of the view. |
|
341 |
// Get the Start/End values for this field. |
|
342 |
// Retrieve using the original table name. |
|
343 |
// Swap the current table name (adjusted for relationships) |
|
344 |
// into the query. |
|
345 |
// @TODO We may be able to use Views substitutions here, |
|
346 |
// investigate that later. |
|
309 | 347 |
$fields = date_views_fields($this->base_table); |
310 | 348 |
$fields = $fields['name']; |
311 | 349 |
$fromto = $fields[$this->original_table . '.' . $this->real_field]['fromto']; |
... | ... | |
321 | 359 |
} |
322 | 360 |
|
323 | 361 |
/** |
324 |
* Add a callback to determine if we have moved outside the valid date range for this argument. |
|
362 |
* Add a callback. |
|
363 |
* |
|
364 |
* To determine if we have moved outside |
|
365 |
* the valid date range for this argument. |
|
325 | 366 |
*/ |
326 | 367 |
function date_forbid() { |
327 | 368 |
if (empty($this->argument)) { |
... | ... | |
343 | 384 |
} |
344 | 385 |
|
345 | 386 |
} |
387 |
// @codingStandardsIgnoreEnd |
Formats disponibles : Unified diff
Weekly update of contrib modules