theme.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @file
  4. * Theme files for Date API.
  5. */
  6. /**
  7. * Returns HTML for a date timezone element.
  8. */
  9. function theme_date_timezone($variables) {
  10. $element = $variables['element'];
  11. $attributes = $element['#attributes'];
  12. $wrapper_attributes = array();
  13. // Add an wrapper to mimic the way a single value field works, for ease in
  14. // using #states.
  15. if (isset($element['#children'])) {
  16. $element['#children'] = '<div id="' . $element['#id'] . '" ' . drupal_attributes($wrapper_attributes) . '>' . $element['#children'] . '</div>';
  17. }
  18. return '<div ' . drupal_attributes($attributes) . '>' . theme('form_element', $element) . '</div>';
  19. }
  20. /**
  21. * Returns HTML for a date select element.
  22. */
  23. function theme_date_select($variables) {
  24. $element = $variables['element'];
  25. $attributes = !empty($element['#wrapper_attributes']) ? $element['#wrapper_attributes'] : array('class' => array());
  26. $attributes['class'][] = 'container-inline-date';
  27. $wrapper_attributes = array('class' => array('date-padding'));
  28. $wrapper_attributes['class'][] = 'clearfix';
  29. // Add an wrapper to mimic the way a single value field works, for ease in
  30. // using #states.
  31. if (isset($element['#children'])) {
  32. $element['#children'] = '<div id="' . $element['#id'] . '" ' . drupal_attributes($wrapper_attributes) . '>' . $element['#children'] . '</div>';
  33. }
  34. return '<div ' . drupal_attributes($attributes) . '>' . theme('form_element', $element) . '</div>';
  35. }
  36. /**
  37. * Returns HTML for a date text element.
  38. */
  39. function theme_date_text($variables) {
  40. $element = $variables['element'];
  41. $attributes = !empty($element['#wrapper_attributes']) ? $element['#wrapper_attributes'] : array('class' => array());
  42. $attributes['class'][] = 'container-inline-date';
  43. // If there is no description, the floating date elements need some extra
  44. // padding below them.
  45. $wrapper_attributes = array('class' => array('date-padding'));
  46. if (empty($element['date']['#description'])) {
  47. $wrapper_attributes['class'][] = 'clearfix';
  48. }
  49. // Add an wrapper to mimic the way a single value field works, for ease in
  50. // using #states.
  51. if (isset($element['#children'])) {
  52. $element['#children'] = '<div id="' . $element['#id'] . '" ' . drupal_attributes($wrapper_attributes) . '>' . $element['#children'] . '</div>';
  53. }
  54. return '<div ' . drupal_attributes($attributes) . '>' . theme('form_element', $element) . '</div>';
  55. }
  56. /**
  57. * Returns HTML for a date select input form element.
  58. */
  59. function theme_date_select_element($variables) {
  60. $element = $variables['element'];
  61. $parents = $element['#parents'];
  62. $part = array_pop($parents);
  63. return '<div class="date-' . $part . '">' . theme('select', $element) . '</div>';
  64. }
  65. /**
  66. * Returns HTML for a date textfield input form element.
  67. */
  68. function theme_date_textfield_element($variables) {
  69. $element = $variables['element'];
  70. $parents = $element['#parents'];
  71. $part = array_pop($parents);
  72. return '<div class="date-' . $part . '">' . theme('textfield', $element) . '</div>';
  73. }
  74. /**
  75. * Returns HTML for a 'hour' date part prefix.
  76. */
  77. function theme_date_part_hour_prefix($variables) {
  78. $element = $variables['element'];
  79. if ($element['#date_label_position'] != 'above') {
  80. return '<span class="form-item date-spacer">&nbsp;-&nbsp;</span>';
  81. }
  82. }
  83. /**
  84. * Returns HTML for a 'minutes and seconds' date part prefix.
  85. */
  86. function theme_date_part_minsec_prefix($variables) {
  87. $element = $variables['element'];
  88. if ($element['#date_label_position'] != 'above') {
  89. return '<span class="form-item date-spacer">:</span>';
  90. }
  91. }
  92. /**
  93. * Returns HTML for a date_select 'year' label.
  94. */
  95. function theme_date_part_label_year($variables) {
  96. return t('Year', array(), array('context' => 'datetime'));
  97. }
  98. /**
  99. * Returns HTML for a date_select 'month' label.
  100. */
  101. function theme_date_part_label_month($variables) {
  102. return t('Month', array(), array('context' => 'datetime'));
  103. }
  104. /**
  105. * Returns HTML for a date_select 'day' label.
  106. */
  107. function theme_date_part_label_day($variables) {
  108. return t('Day', array(), array('context' => 'datetime'));
  109. }
  110. /**
  111. * Returns HTML for a date_select 'hour' label.
  112. */
  113. function theme_date_part_label_hour($variables) {
  114. return t('Hour', array(), array('context' => 'datetime'));
  115. }
  116. /**
  117. * Returns HTML for a date_select 'minute' label.
  118. */
  119. function theme_date_part_label_minute($variables) {
  120. return t('Minute', array(), array('context' => 'datetime'));
  121. }
  122. /**
  123. * Returns HTML for a date_select 'second' label.
  124. */
  125. function theme_date_part_label_second($variables) {
  126. return t('Second', array(), array('context' => 'datetime'));
  127. }
  128. /**
  129. * Returns HTML for a date_select 'ampm' label.
  130. */
  131. function theme_date_part_label_ampm($variables) {
  132. return '&nbsp;';
  133. }
  134. /**
  135. * Returns HTML for a date_select 'timezone' label.
  136. */
  137. function theme_date_part_label_timezone($variables) {
  138. return t('Timezone');
  139. }
  140. /**
  141. * Returns HTML for a date_select 'date' label.
  142. */
  143. function theme_date_part_label_date($variables) {
  144. return t('Date', array(), array('context' => 'datetime'));
  145. }
  146. /**
  147. * Returns HTML for a date_select 'time' label.
  148. */
  149. function theme_date_part_label_time($variables) {
  150. return t('Time', array(), array('context' => 'datetime'));
  151. }
  152. /**
  153. * Returns HTML for a date block that looks like a mini calendar day.
  154. *
  155. * Pass in a date object already set to the right timezone, format as a calendar
  156. * page date. The calendar styling is created in CSS.
  157. */
  158. function theme_date_calendar_day($variables) {
  159. $output = '';
  160. $date = $variables['date'];
  161. if (!empty($date)) {
  162. $output .= '<div class="date-calendar-day">';
  163. $output .= '<span class="month">' . date_format_date($date, 'custom', 'M') . '</span>';
  164. $output .= '<span class="day">' . date_format_date($date, 'custom', 'j') . '</span>';
  165. $output .= '<span class="year">' . date_format_date($date, 'custom', 'Y') . '</span>';
  166. $output .= '</div>';
  167. }
  168. return $output;
  169. }
  170. /**
  171. * Returns HTML for a date in the format 'time ago'.
  172. */
  173. function theme_date_time_ago($variables) {
  174. $start_date = $variables['start_date'];
  175. $end_date = $variables['end_date'];
  176. $use_end_date = isset($variables['use_end_date']) ? $variables['use_end_date'] : false;
  177. $interval = !empty($variables['interval']) ? $variables['interval'] : 2;
  178. $display = isset($variables['interval_display']) ? $variables['interval_display'] : 'time ago';
  179. // If no date is sent, then return nothing.
  180. if (empty($start_date) || empty($end_date)) {
  181. return;
  182. }
  183. // We use the end date only when the option is checked.
  184. if ($use_end_date){
  185. $date = date_format($end_date, DATE_FORMAT_UNIX);
  186. }
  187. else {
  188. $date = date_format($start_date, DATE_FORMAT_UNIX);
  189. }
  190. // Time to compare dates to.
  191. $now = date_format(date_now(), DATE_FORMAT_UNIX);
  192. // Will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence).
  193. $time_diff = $now - $date;
  194. // Uses the same options used by Views format_interval.
  195. switch ($display) {
  196. case 'raw time ago':
  197. return format_interval($time_diff, $interval);
  198. case 'time ago':
  199. return t('%time ago', array('%time' => format_interval($time_diff, $interval)));
  200. case 'raw time hence':
  201. return format_interval(-$time_diff, $interval);
  202. case 'time hence':
  203. return t('%time hence', array('%time' => format_interval(-$time_diff, $interval)));
  204. case 'raw time span':
  205. return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), $interval);
  206. case 'inverse time span':
  207. return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), $interval);
  208. case 'time span':
  209. return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), $interval)));
  210. }
  211. }