date_views_filter_handler_simple.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /**
  3. * @file
  4. * A standard Views filter for a single date field, using Date API form selectors and sql handling.
  5. */
  6. class date_views_filter_handler_simple extends views_handler_filter_date {
  7. var $date_handler = NULL;
  8. var $offset = NULL;
  9. function init(&$view, &$options) {
  10. parent::init($view, $options);
  11. module_load_include('inc', 'date_api', 'date_api_sql');
  12. $this->date_handler = new date_sql_handler(DATE_UNIX);
  13. if (!empty($this->definition['field_name'])) {
  14. $field = field_info_field($this->definition['field_name']);
  15. if (!empty($field) && !empty($field['type'])) {
  16. $this->date_handler->date_type = $field['type'];
  17. }
  18. $this->date_handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
  19. $this->date_handler->local_timezone = date_get_timezone($field['settings']['tz_handling']);
  20. }
  21. $this->form_submitted = FALSE;
  22. $this->date_handler->granularity = isset($options['granularity']) ? $options['granularity'] : 'day';
  23. $this->format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
  24. // Identify the base table for this field.
  25. // It will be used to call for the right query field options.
  26. $this->base_table = $this->table;
  27. }
  28. // Set default values for the date filter.
  29. function option_definition() {
  30. $options = parent::option_definition();
  31. $options['granularity'] = array('default' => 'day');
  32. $options['form_type'] = array('default' => 'date_select');
  33. $options['default_date'] = array('default' => '');
  34. $options['default_to_date'] = array('default' => '');
  35. $options['year_range'] = array('default' => '-3:+3');
  36. $options['add_delta'] = array('default' => '');
  37. return $options;
  38. }
  39. function operators() {
  40. $operators = parent::operators();
  41. $operators['contains'] = array(
  42. 'title' => t('Contains'),
  43. 'method' => 'op_contains',
  44. 'short' => t('contains'),
  45. 'values' => 1,
  46. );
  47. return $operators;
  48. }
  49. /**
  50. * Helper function to find a default value.
  51. */
  52. function date_default_value($prefix, $options = NULL) {
  53. $default_date = '';
  54. if (empty($options)) {
  55. $options = $this->options;
  56. }
  57. // If this is a remembered value, use the value from the SESSION.
  58. if (!empty($this->options['expose']['remember'])) {
  59. $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
  60. if (!empty($_SESSION['views'][$this->view->name][$display_id][$this->options['expose']['identifier']][$prefix])) {
  61. return $_SESSION['views'][$this->view->name][$display_id][$this->options['expose']['identifier']][$prefix];
  62. }
  63. }
  64. // This is a date that needs to be constructed from options like 'now' .
  65. $default_option = $prefix == 'max' ? $options['default_to_date'] : $options['default_date'];
  66. if (!empty($default_option)) {
  67. str_replace('now', 'today', $default_option);
  68. $date = date_create($default_option, date_default_timezone_object());
  69. $default_date = !empty($date) ? $date->format($this->format) : '';
  70. // The format for our filter is in ISO format, but the widget will need it in datetime format.
  71. $default_date = str_replace('T', ' ', $default_date);
  72. }
  73. // This a fixed date.
  74. else {
  75. $default_date = $options['value'][$prefix];
  76. }
  77. return $default_date;
  78. }
  79. /**
  80. * Helper function to see if we need to swap in the default value.
  81. *
  82. * Views exposed filters treat everything as submitted, so if it's an empty value we have to
  83. * see if anything actually was submitted. If nothing has really been submitted, we need
  84. * to swap in our default value logic.
  85. */
  86. function get_filter_value($prefix, $input) {
  87. // All our date widgets provide datetime values but we use ISO in our SQL
  88. // for consistency between the way filters and arguments work (arguments
  89. // cannot contain spaces).
  90. if (empty($input)) {
  91. if (empty($this->options['exposed'])) {
  92. return str_replace(' ', 'T', $this->date_default_value($prefix));
  93. }
  94. elseif (isset($this->options['expose']['identifier']) && !isset($_GET[$this->options['expose']['identifier']])) {
  95. return str_replace(' ', 'T', $this->date_default_value($prefix));
  96. }
  97. }
  98. return str_replace(' ', 'T', $input);
  99. }
  100. function accept_exposed_input($input) {
  101. if (!empty($this->options['exposed'])) {
  102. $element_input = $input[$this->options['expose']['identifier']];
  103. $element_input['value'] = $this->get_filter_value('value', !empty($element_input['value']) ? $element_input['value'] : '');
  104. $element_input['min'] = $this->get_filter_value('min', !empty($element_input['min']) ? $element_input['min'] : '');
  105. $element_input['max'] = $this->get_filter_value('max', !empty($element_input['max']) ? $element_input['max'] : '');
  106. if (is_array($element_input) && isset($element_input['default_date'])) {
  107. unset($element_input['default_date']);
  108. }
  109. if (is_array($element_input) && isset($element_input['default_to_date'])) {
  110. unset($element_input['default_to_date']);
  111. }
  112. $input[$this->options['expose']['identifier']] = $element_input;
  113. }
  114. return parent::accept_exposed_input($input);
  115. }
  116. function op_between($field) {
  117. // Add the delta field to the view so we can later find the value that matched our query.
  118. list($table_name, $field_name) = explode('.', $field);
  119. if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) {
  120. $this->query->add_field($table_name, 'delta');
  121. $real_field_name = str_replace(array('_value', '_value2'), '', $this->real_field);
  122. $this->query->add_field($table_name, 'entity_id', 'date_id_' . $real_field_name);
  123. $this->query->add_field($table_name, 'delta', 'date_delta_' . $real_field_name);
  124. }
  125. $min_value = $this->get_filter_value('min', $this->value['min']);
  126. $min_comp_date = new DateObject($min_value, date_default_timezone(), $this->format);
  127. $max_value = $this->get_filter_value('max', $this->value['max']);
  128. $max_comp_date = new DateObject($max_value, date_default_timezone(), $this->format);
  129. $field_min = $this->date_handler->sql_field($field, NULL, $min_comp_date);
  130. $field_min = $this->date_handler->sql_format($this->format, $field_min);
  131. $field_max = $this->date_handler->sql_field($field, NULL, $max_comp_date);
  132. $field_max = $this->date_handler->sql_format($this->format, $field_max);
  133. $placeholder_min = $this->placeholder();
  134. $placeholder_max = $this->placeholder();
  135. $group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
  136. if ($this->operator == 'between') {
  137. $this->query->add_where_expression($group, "$field_min >= $placeholder_min AND $field_max <= $placeholder_max", array($placeholder_min => $min_value, $placeholder_max => $max_value));
  138. }
  139. else {
  140. $this->query->add_where_expression($group, "$field_min < $placeholder_min OR $field_max > $placeholder_max", array($placeholder_min => $min_value, $placeholder_max => $max_value));
  141. }
  142. }
  143. function op_simple($field) {
  144. // Add the delta field to the view so we can later find the value that matched our query.
  145. list($table_name, $field_name) = explode('.', $field);
  146. if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) {
  147. $this->query->add_field($table_name, 'delta');
  148. $real_field_name = str_replace(array('_value', '_value2'), '', $this->real_field);
  149. $this->query->add_field($table_name, 'entity_id', 'date_id_' . $real_field_name);
  150. $this->query->add_field($table_name, 'delta', 'date_delta_' . $real_field_name);
  151. }
  152. $value = $this->get_filter_value('value', $this->value['value']);
  153. $comp_date = new DateObject($value, date_default_timezone(), $this->format);
  154. $field = $this->date_handler->sql_field($field, NULL, $comp_date);
  155. $field = $this->date_handler->sql_format($this->format, $field);
  156. $placeholder = $this->placeholder();
  157. $group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
  158. $this->query->add_where_expression($group, "$field $this->operator $placeholder", array($placeholder => $value));
  159. }
  160. function op_contains($field) {
  161. // Add the delta field to the view so we can later find the value that matched our query.
  162. list($table_name, $field_name) = explode('.', $field);
  163. if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) {
  164. $this->query->add_field($table_name, 'delta');
  165. }
  166. $value = $this->get_filter_value('value', $this->value['value']);
  167. $comp_date = new DateObject($value, date_default_timezone(), $this->format);
  168. $fields = date_views_fields($this->base_table);
  169. $fields = $fields['name'];
  170. $fromto = $fields[$field]['fromto'];
  171. $field_min = $this->date_handler->sql_field($fromto[0], NULL, $comp_date);
  172. $field_min = $this->date_handler->sql_format($this->format, $field_min);
  173. $field_max = $this->date_handler->sql_field($fromto[1], NULL, $comp_date);
  174. $field_max = $this->date_handler->sql_format($this->format, $field_max);
  175. $placeholder_min = $this->placeholder();
  176. $placeholder_max = $this->placeholder();
  177. $group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
  178. $this->query->add_where_expression($group, "$field_max >= $placeholder_min AND $field_min <= $placeholder_max", array($placeholder_min => $value, $placeholder_max => $value));
  179. }
  180. /**
  181. * Set the granularity of the date parts to use in the filter.
  182. */
  183. function has_extra_options() { return TRUE; }
  184. /**
  185. * Date selection options.
  186. */
  187. function widget_options() {
  188. $options = array(
  189. 'date_select' => t('Select'),
  190. 'date_text' => t('Text'),
  191. 'date_popup' => t('Popup'),
  192. );
  193. if (!module_exists('date_popup')) {
  194. unset($options['date_popup']);
  195. }
  196. return $options;
  197. }
  198. function year_range() {
  199. $year_range = explode(':', $this->options['year_range']);
  200. if (substr($this->options['year_range'], 0, 1) == '-' || $year_range[0] < 0) {
  201. $this_year = date_format(date_now(), 'Y');
  202. $year_range[0] = $this_year + $year_range[0];
  203. $year_range[1] = $this_year + $year_range[1];
  204. }
  205. return $year_range;
  206. }
  207. function extra_options_form(&$form, &$form_state) {
  208. parent::extra_options_form($form, $form_state);
  209. $form['form_type'] = array(
  210. '#type' => 'radios',
  211. '#title' => t('Date selection form element'),
  212. '#default_value' => $this->options['form_type'],
  213. '#options' => $this->widget_options(),
  214. );
  215. $form['granularity'] = $this->date_handler->granularity_form($this->options['granularity']);
  216. $form['granularity']['#title'] = t('Filter granularity');
  217. $form['year_range'] = array(
  218. '#type' => 'date_year_range',
  219. '#default_value' => $this->options['year_range'],
  220. );
  221. if (!empty($this->definition['field_name'])) {
  222. $field = field_info_field($this->definition['field_name']);
  223. }
  224. $form['add_delta'] = array(
  225. '#type' => 'radios',
  226. '#title' => t('Add multiple value identifier'),
  227. '#default_value' => $this->options['add_delta'],
  228. '#options' => array('' => t('No'), 'yes' => t('Yes')),
  229. '#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.'),
  230. // Only let mere mortals tweak this setting for multi-value fields
  231. '#access' => !empty($field) ? $field['cardinality'] != 1 : 0,
  232. );
  233. }
  234. function extra_options_validate($form, &$form_state) {
  235. if (!preg_match('/^(?:\-[0-9]{1,4}|[0-9]{4}):(?:[\+|\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) {
  236. form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9'));
  237. }
  238. }
  239. /**
  240. * Add the selectors to the value form using the date handler.
  241. */
  242. function value_form(&$form, &$form_state) {
  243. // We use different values than the parent form, so we must
  244. // construct our own form element.
  245. $form['value'] = array();
  246. $form['value']['#tree'] = TRUE;
  247. // Below section copied from views_handler_filter_numeric.inc.
  248. $which = 'all';
  249. $source = '';
  250. if (!empty($form['operator'])) {
  251. $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
  252. }
  253. $identifier = $this->options['expose']['identifier'];
  254. if (!empty($form_state['exposed'])) {
  255. if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
  256. // exposed and locked.
  257. $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
  258. }
  259. else {
  260. $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
  261. }
  262. }
  263. if ($which == 'all' || $which == 'value') {
  264. $form['value'] += $this->date_parts_form($form_state, 'value', $source, $which, $this->operator_values(1), $identifier, 'default_date');
  265. }
  266. if ($which == 'all' || $which == 'minmax') {
  267. $form['value'] += $this->date_parts_form($form_state, 'min', $source, $which, $this->operator_values(2), $identifier, 'default_date');
  268. $form['value'] += $this->date_parts_form($form_state, 'max', $source, $which, $this->operator_values(2), $identifier, 'default_to_date');
  269. }
  270. // Add some extra validation for the select widget to be sure that
  271. // the user inputs all parts of the date.
  272. if ($this->options['form_type'] == 'date_select') {
  273. $form['value']['#element_validate'] = array('date_views_select_validate');
  274. }
  275. }
  276. /**
  277. * A form element to select date part values.
  278. *
  279. * @param string $prefix
  280. * A prefix for the date values, 'value', 'min', or 'max' .
  281. * @param string $source
  282. * The operator for this element.
  283. * @param string $which
  284. * Which element to provide, 'all', 'value', or 'minmax' .
  285. * @param array $operator_values
  286. * An array of the allowed operators for this element.
  287. * @param array $identifier
  288. * Identifier of the exposed element.
  289. * @param array $relative_id
  290. * Form element id to use for the relative date field.
  291. *
  292. * @return
  293. * The form date part element for this instance.
  294. */
  295. function date_parts_form($form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id) {
  296. module_load_include('inc', 'date_api', 'date_api_elements');
  297. switch ($prefix) {
  298. case 'min':
  299. $label = t('Start date');
  300. $relative_label = t('Relative start date');
  301. break;
  302. case 'max':
  303. $label = t('End date');
  304. $relative_label = t('Relative end date');
  305. break;
  306. default:
  307. $label = '';
  308. $relative_label = t('Relative date');
  309. break;
  310. }
  311. $type = $this->options['form_type'];
  312. if ($type == 'date_popup' && !module_exists('date_popup')) {
  313. $type = 'date_text';
  314. }
  315. $format = $this->date_handler->views_formats($this->options['granularity'], 'display');
  316. $granularity = array_keys($this->date_handler->date_parts($this->options['granularity']));
  317. $relative_value = ($prefix == 'max' ? $this->options['default_to_date'] : $this->options['default_date']);
  318. if (!empty($form_state['exposed'])) {
  319. // UI when the date selector is exposed.
  320. $default_date = $this->date_default_value($prefix);
  321. $id = 'edit-' . str_replace('_', '-', $this->field) . '-' . $prefix;
  322. $form[$prefix] = array(
  323. '#title' => check_plain($label),
  324. '#type' => $type,
  325. '#size' => 20,
  326. '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
  327. '#date_format' => date_limit_format($format, $granularity),
  328. '#date_label_position' => 'within',
  329. '#date_year_range' => $this->options['year_range'],
  330. '#process' => array($type . '_element_process'),
  331. '#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '-inside-wrapper">',
  332. '#suffix' => '</div></div>',
  333. );
  334. if ($which == 'all') {
  335. $form[$prefix]['#pre_render'][] = 'ctools_dependent_pre_render';
  336. $form[$prefix]['#dependency'] = array($source => $operator_values);
  337. }
  338. if (!isset($form_state['input'][$identifier][$prefix])) {
  339. $form_state['input'][$identifier][$prefix] = $this->value[$prefix];
  340. }
  341. }
  342. else {
  343. // UI when the date selector is on the views configuration screen.
  344. $default_date = '';
  345. $id = 'edit-options-value-' . $prefix;
  346. $form[$prefix . '_group'] = array(
  347. '#type' => 'fieldset',
  348. '#attributes' => array('class' => array('date-views-filter-fieldset')),
  349. );
  350. $form[$prefix . '_group'][$prefix . '_choose_input_type'] = array(
  351. '#title' => check_plain($label),
  352. '#type' => 'select',
  353. '#options' => array('date' => t('Select a date'), 'relative' => ('Enter a relative date')),
  354. '#attributes' => array('class' => array($prefix . '-choose-input-type')),
  355. '#default_value' => !empty($relative_value) ? 'relative' : 'date',
  356. );
  357. $form[$prefix . '_group'][$prefix] = array(
  358. '#title' => t('Select a date'),
  359. '#type' => $type,
  360. '#size' => 20,
  361. '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
  362. '#date_format' => date_limit_format($format, $granularity),
  363. '#date_label_position' => 'within',
  364. '#date_year_range' => $this->options['year_range'],
  365. '#process' => array($type . '_element_process'),
  366. '#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '-inside-wrapper">',
  367. '#suffix' => '</div></div>',
  368. '#states' => array(
  369. 'visible' => array(
  370. ":input.{$prefix}-choose-input-type" => array('value' => 'date'),
  371. ),
  372. ),
  373. );
  374. $form[$prefix . '_group'][$relative_id] = array(
  375. '#type' => 'textfield',
  376. '#title' => check_plain($relative_label),
  377. '#default_value' => $relative_value,
  378. '#description' => t("Relative dates are computed when the view is displayed. Examples: now, now +1 day, 12AM today, Monday next week. <a href=\"@relative_format\">More examples of relative date formats in the PHP documentation</a>.", array('@relative_format' => 'http://www.php.net/manual/en/datetime.formats.relative.php')),
  379. '#states' => array(
  380. 'visible' => array(
  381. ":input.{$prefix}-choose-input-type" => array('value' => 'relative'),
  382. ),
  383. ),
  384. );
  385. if ($which == 'all') {
  386. $form[$prefix . '_group']['#pre_render'][] = 'ctools_dependent_pre_render';
  387. $form[$prefix . '_group']['#dependency'] = array($source => $operator_values);
  388. }
  389. }
  390. return $form;
  391. }
  392. /**
  393. * Value validation.
  394. *
  395. * TODO add in more validation.
  396. *
  397. * We are setting an extra option using a value form
  398. * because it makes more sense to set it there.
  399. * That's not the normal method, so we have to manually
  400. * transfer the selected value back to the option.
  401. */
  402. function value_validate($form, &$form_state) {
  403. $options = &$form_state['values']['options'];
  404. if ($options['operator'] == 'between' || $options['operator'] == 'not between') {
  405. if ($options['value']['min_group']['min_choose_input_type'] == 'relative') {
  406. if (empty($options['value']['min_group']['default_date'])) {
  407. form_set_error('options][value][min_group][default_date', t('Relative start date not specified.'));
  408. }
  409. else {
  410. $this->options['default_date'] = $options['value']['min_group']['default_date'];
  411. // NULL out the value field, user wanted the relative value to take hold.
  412. $options['value']['min_group']['min'] = NULL;
  413. }
  414. }
  415. // If an absolute date was used, be sure to wipe the relative date.
  416. else {
  417. $this->options['default_date'] = '';
  418. }
  419. if ($options['value']['max_group']['max_choose_input_type'] == 'relative') {
  420. if (empty($options['value']['max_group']['default_to_date'])) {
  421. form_set_error('options][value][max_group][default_to_date', t('Relative end date not specified.'));
  422. }
  423. else {
  424. $this->options['default_to_date'] = $options['value']['max_group']['default_to_date'];
  425. // NULL out the value field, user wanted the relative value to take hold.
  426. $options['value']['max_group']['max'] = NULL;
  427. }
  428. }
  429. // If an absolute date was used, be sure to wipe the relative date.
  430. else {
  431. $this->options['default_to_date'] = '';
  432. }
  433. }
  434. elseif (in_array($options['operator'], array('<', '<=', '=', '!=', '>=', '>'))) {
  435. if ($options['value']['value_group']['value_choose_input_type'] == 'relative') {
  436. if (empty($options['value']['value_group']['default_date'])) {
  437. form_set_error('options][value][value_group][default_date', t('Relative date not specified.'));
  438. }
  439. else {
  440. $this->options['default_date'] = $options['value']['value_group']['default_date'];
  441. // NULL out the value field, user wanted the relative value to take hold.
  442. $options['value']['value_group']['value'] = NULL;
  443. }
  444. }
  445. // If an absolute date was used, be sure to wipe the relative date.
  446. else {
  447. $this->options['default_date'] = '';
  448. }
  449. }
  450. // Flatten the form structure for views, so the values can be saved.
  451. foreach (array('value', 'min', 'max') as $key) {
  452. $options['value'][$key] = $options['value'][$key . '_group'][$key];
  453. }
  454. }
  455. /**
  456. * Validate that the time values convert to something usable.
  457. */
  458. function validate_valid_time(&$form, $operator, $value) {
  459. // Override the core date filter validation.
  460. // Our date widgets do their own validation.
  461. }
  462. // Update the summary values to provide
  463. // meaningful information for each option.
  464. function admin_summary() {
  465. $parts = $this->date_handler->date_parts();
  466. $widget_options = $this->widget_options();
  467. // If the filter is exposed, display the granularity.
  468. if ($this->options['exposed']) {
  469. return t('<strong>Exposed</strong> @widget @format', array('@format' => $parts[$this->date_handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
  470. }
  471. // If not exposed, display the value.
  472. $output = '';
  473. if (in_array($this->operator, $this->operator_values(2))) {
  474. $min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
  475. $max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
  476. $output .= t('@min and @max', array('@min' => $min, '@max' => $max));
  477. }
  478. else {
  479. $output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
  480. }
  481. return $output;
  482. }
  483. }