date_context_date_condition.inc

  1. nittany7 modules/contrib/date/date_context/plugins/date_context_date_condition.inc
  2. cis7 modules/contrib/date/date_context/plugins/date_context_date_condition.inc
  3. mooc7 modules/contrib/date/date_context/plugins/date_context_date_condition.inc

Classes

Namesort descending Description
date_context_date_condition Expose term views/term forms by vocabulary as a context condition.

File

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