date_context_date_condition.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Context date condition plugin.
  5. */
  6. /**
  7. * Expose term views/term forms by vocabulary as a context condition.
  8. */
  9. // @codingStandardsIgnoreStart
  10. class date_context_date_condition extends context_condition_node {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function condition_values() {
  15. $values = array();
  16. $fields = field_info_fields();
  17. foreach ($fields as $field_name => $field) {
  18. if (in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
  19. $values[$field_name] = $field_name;
  20. }
  21. }
  22. return $values;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function options_form($context) {
  28. $defaults = $this->fetch_from_context($context, 'options');
  29. $options = array(
  30. '<' => t('Is less than'),
  31. '<=' => t('Is less than or equal to'),
  32. '>=' => t('Is greater than or equal to'),
  33. '>' => t('Is greater than'),
  34. '=' => t('Is equal to'),
  35. '!=' => t('Is not equal to'),
  36. 'empty' => t('Is empty'),
  37. 'not empty' => t('Is not Empty'),
  38. );
  39. $dependency_options = array('<', '<=', '>', '>=', '=', '!=');
  40. $form['operation'] = array(
  41. '#title' => t('Operation'),
  42. '#type' => 'select',
  43. '#options' => $options,
  44. '#description' => t('The comparison to perform to determine if the date field meets the condition. For multiple value date fields, all values will be checked to see if any meet the condition.'),
  45. '#default_value' => isset($defaults['operation']) ? $defaults['operation'] : '',
  46. '#required' => TRUE,
  47. );
  48. $form['value'] = array(
  49. '#title' => t('Value'),
  50. '#type' => 'textfield',
  51. '#description' => t("The value the field should contain to meet the condition. This can either be an absolute date in ISO format (YYYY-MM-DDTHH:MM:SS) or a relative string like '12AM today'. Examples: 2011-12-31T00:00:00, 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')),
  52. '#default_value' => isset($defaults['value']) ? $defaults['value'] : '',
  53. '#process' => array('ctools_dependent_process'),
  54. '#dependency' => array('edit-conditions-plugins-date-context-date-condition-options-operation' => $dependency_options),
  55. );
  56. return $form;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function execute($entity, $op) {
  62. if (in_array($op, array('view', 'form'))) {
  63. foreach ($this->get_contexts() as $context) {
  64. $options = $this->fetch_from_context($context, 'options');
  65. $fields = $this->fetch_from_context($context, 'values');
  66. foreach ($fields as $field_name => $label) {
  67. // If this field does not exist on this entity, just move along.
  68. if (empty($entity->{$field_name})) {
  69. continue;
  70. }
  71. $items = field_get_items('node', $entity, $field_name);
  72. // If there are no values, nothing to do unless we were looking for 'empty' or '!='.
  73. if (empty($items)) {
  74. if ($options['operation'] == '!=' || $options['operation'] == 'empty') {
  75. $this->condition_met($context, $field_name);
  76. }
  77. }
  78. // We don't have to construct the date values if we're just testing for 'not empty'.
  79. elseif ($options['operation'] == 'not empty') {
  80. $this->condition_met($context, $field_name);
  81. }
  82. // All other operations need to retrieve the date values for comparision.
  83. else {
  84. $field = field_info_field($field_name);
  85. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  86. foreach ($items as $delta => $item) {
  87. $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
  88. $date = new DateObject($item['value'], $timezone_db);
  89. date_timezone_set($date, timezone_open($timezone));
  90. $date1 = $date->format(DATE_FORMAT_DATETIME);
  91. if (empty($item['value2'])) {
  92. $item['value2'] = $item['value'];
  93. }
  94. $date = new DateObject($item['value2'], $timezone_db);
  95. date_timezone_set($date, timezone_open($timezone));
  96. $date2 = $date->format(DATE_FORMAT_DATETIME);
  97. str_replace('now', 'today', $options['value']);
  98. $date = date_create($options['value'], date_default_timezone_object());
  99. $compdate = $date->format(DATE_FORMAT_DATETIME);
  100. switch ($options['operation']) {
  101. case '=':
  102. if ($date2 >= $compdate && $date1 <= $compdate) {
  103. $this->condition_met($context, $field_name);
  104. }
  105. break;
  106. case '>':
  107. if ($date1 > $compdate) {
  108. $this->condition_met($context, $field_name);
  109. }
  110. break;
  111. case '>=':
  112. if ($date1 >= $compdate) {
  113. $this->condition_met($context, $field_name);
  114. }
  115. break;
  116. case '<':
  117. if ($date2 < $compdate) {
  118. $this->condition_met($context, $field_name);
  119. }
  120. break;
  121. case '<=':
  122. if ($date2 <= $compdate) {
  123. $this->condition_met($context, $field_name);
  124. }
  125. break;
  126. case '!=':
  127. if ($date1 < $compdate || $date2 > $compdate) {
  128. $this->condition_met($context, $field_name);
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // @codingStandardsIgnoreEnd