PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/system/expressionengine/libraries/channel_entries_parser/components/Date.php

https://bitbucket.org/mbaily/tremain
PHP | 241 lines | 193 code | 12 blank | 36 comment | 3 complexity | a1a95408dde0b07dafea95e06d1cbef9 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2013, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Channel Parser Component (Dates)
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class EE_Channel_date_parser implements EE_Channel_parser_component {
  24. /**
  25. * Check if dates are enabled.
  26. *
  27. * @param array A list of "disabled" features
  28. * @return Boolean Is disabled?
  29. */
  30. public function disabled(array $disabled, EE_Channel_preparser $pre)
  31. {
  32. return FALSE;
  33. }
  34. // ------------------------------------------------------------------------
  35. /**
  36. * Find all date variables in the template. The return value
  37. * will be passed to process for the main parsing loop.
  38. *
  39. * @param String The tagdata to be parsed
  40. * @param Object The preparser object.
  41. * @return Array Found date tags.
  42. */
  43. public function pre_process($tagdata, EE_Channel_preparser $pre)
  44. {
  45. $prefix = $pre->prefix();
  46. $entry_date = array();
  47. $gmt_date = array();
  48. $gmt_entry_date = array();
  49. $edit_date = array();
  50. $gmt_edit_date = array();
  51. $expiration_date = array();
  52. $week_date = array();
  53. $date_vars = array('entry_date', 'gmt_date', 'gmt_entry_date', 'edit_date', 'gmt_edit_date', 'expiration_date', 'recent_comment_date', 'week_date');
  54. ee()->load->helper('date');
  55. foreach ($date_vars as $val)
  56. {
  57. if ( ! $pre->has_tag($val))
  58. {
  59. continue;
  60. }
  61. $full_val = $prefix.$val;
  62. if (preg_match_all("/".LD.$full_val."\s+format=([\"'])([^\\1]*?)\\1".RD."/s", $tagdata, $matches))
  63. {
  64. for ($j = 0; $j < count($matches[0]); $j++)
  65. {
  66. $matches[0][$j] = str_replace(array(LD,RD), '', $matches[0][$j]);
  67. switch ($val)
  68. {
  69. case 'entry_date':
  70. $entry_date[$matches[0][$j]] = $matches[2][$j];
  71. break;
  72. case 'gmt_date':
  73. $gmt_date[$matches[0][$j]] = $matches[2][$j];
  74. break;
  75. case 'gmt_entry_date':
  76. $gmt_entry_date[$matches[0][$j]] = $matches[2][$j];
  77. break;
  78. case 'edit_date':
  79. $edit_date[$matches[0][$j]] = $matches[2][$j];
  80. break;
  81. case 'gmt_edit_date':
  82. $gmt_edit_date[$matches[0][$j]] = $matches[2][$j];
  83. break;
  84. case 'expiration_date':
  85. $expiration_date[$matches[0][$j]] = $matches[2][$j];
  86. break;
  87. case 'recent_comment_date':
  88. $recent_comment_date[$matches[0][$j]] = $matches[2][$j];
  89. break;
  90. case 'week_date':
  91. $week_date[$matches[0][$j]] = $matches[2][$j];
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. return call_user_func_array('compact', $date_vars);
  98. }
  99. // ------------------------------------------------------------------------
  100. /**
  101. * Replace all of the default date fields.
  102. *
  103. * @param String The tagdata to be parsed
  104. * @param Object The channel parser object
  105. * @param Mixed The results from the preparse method
  106. *
  107. * @return String The processed tagdata
  108. */
  109. public function replace($tagdata, EE_Channel_data_parser $obj, $date_vars)
  110. {
  111. $tag = $obj->tag();
  112. $tag_options = $obj->tag_options();
  113. $data = $obj->row();
  114. $prefix = $obj->prefix();
  115. // @todo
  116. $key = $tag;
  117. $val = $tag_options;
  118. extract($date_vars);
  119. // parse entry date
  120. if (isset($entry_date[$key]))
  121. {
  122. $val = str_replace($entry_date[$key], ee()->localize->format_date($entry_date[$key], $data['entry_date']), $val);
  123. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  124. }
  125. // Recent Comment Date
  126. elseif (isset($recent_comment_date[$key]))
  127. {
  128. if ($data['recent_comment_date'] != 0)
  129. {
  130. $val = str_replace($recent_comment_date[$key], ee()->localize->format_date($recent_comment_date[$key], $data['recent_comment_date']), $val);
  131. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  132. }
  133. else
  134. {
  135. $tagdata = str_replace(LD.$key.RD, '', $tagdata);
  136. }
  137. }
  138. // GMT date - entry date in GMT
  139. elseif (isset($gmt_entry_date[$key]))
  140. {
  141. $val = str_replace($gmt_entry_date[$key], ee()->localize->format_date($gmt_entry_date[$key], $data['entry_date'], FALSE), $val);
  142. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  143. }
  144. elseif (isset($gmt_date[$key]))
  145. {
  146. $val = str_replace($gmt_date[$key], ee()->localize->format_date($gmt_date[$key], $data['entry_date'], FALSE), $val);
  147. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  148. }
  149. // parse "last edit" date
  150. elseif (isset($edit_date[$key]))
  151. {
  152. $val = str_replace($edit_date[$key], ee()->localize->format_date($edit_date[$key], mysql_to_unix($data['edit_date'])), $val);
  153. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  154. }
  155. // "last edit" date as GMT
  156. elseif (isset($gmt_edit_date[$key]))
  157. {
  158. $val = str_replace($gmt_edit_date[$key], ee()->localize->format_date($gmt_edit_date[$key], mysql_to_unix($data['edit_date']), FALSE), $val);
  159. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  160. }
  161. // parse expiration date
  162. elseif (isset($expiration_date[$key]))
  163. {
  164. if ($data['expiration_date'] != 0)
  165. {
  166. $val = str_replace($expiration_date[$key], ee()->localize->format_date($expiration_date[$key], $data['expiration_date']), $val);
  167. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  168. }
  169. else
  170. {
  171. $tagdata = str_replace(LD.$key.RD, "", $tagdata);
  172. }
  173. }
  174. // "week_date"
  175. elseif (isset($week_date[$key]))
  176. {
  177. // Subtract the number of days the entry is "into" the week to get zero (Sunday)
  178. // If the entry date is for Sunday, and Monday is being used as the week's start day,
  179. // then we must back things up by six days
  180. $offset = 0;
  181. if (strtolower(ee()->TMPL->fetch_param('start_day')) == 'monday')
  182. {
  183. $day_of_week = ee()->localize->format_date('%w', $data['entry_date']);
  184. if ($day_of_week == '0')
  185. {
  186. $offset = -518400; // back six days
  187. }
  188. else
  189. {
  190. $offset = 86400; // plus one day
  191. }
  192. }
  193. $week_start_date = $data['entry_date'] - (ee()->localize->format_date('%w', $data['entry_date'], TRUE) * 60 * 60 * 24) + $offset;
  194. $val = str_replace($week_date[$key], ee()->localize->format_date($week_date[$key], $week_start_date), $val);
  195. $tagdata = str_replace(LD.$key.RD, $val, $tagdata);
  196. }
  197. return $tagdata;
  198. }
  199. }