PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/common/ezdateoperatorcollection.php

http://github.com/ezsystems/ezpublish
PHP | 292 lines | 237 code | 15 blank | 40 comment | 51 complexity | c4ef0ff1555515f2a6991e02419f4be1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZDateOperatorCollection class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. class eZDateOperatorCollection
  11. {
  12. /**
  13. * Constructor
  14. *
  15. * @param string $monthName
  16. */
  17. public function __construct( $monthName = 'month_overview' )
  18. {
  19. $this->MonthOverviewName = $monthName;
  20. $this->Operators = array( $monthName );
  21. }
  22. /*!
  23. Returns the operators in this class.
  24. */
  25. function operatorList()
  26. {
  27. return $this->Operators;
  28. }
  29. /*!
  30. \return true to tell the template engine that the parameter list exists per operator type.
  31. */
  32. function namedParameterPerOperator()
  33. {
  34. return true;
  35. }
  36. /*!
  37. See eZTemplateOperator::namedParameterList()
  38. */
  39. function namedParameterList()
  40. {
  41. return array( 'month_overview' => array( 'field' => array( 'type' => 'string',
  42. 'required' => true,
  43. 'default' => false ),
  44. 'date' => array( 'type' => 'integer',
  45. 'required' => true,
  46. 'default' => false ),
  47. 'optional' => array( 'type' => 'array',
  48. 'required' => false,
  49. 'default' => false ) ) );
  50. }
  51. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement )
  52. {
  53. $locale = eZLocale::instance();
  54. if ( $operatorName == $this->MonthOverviewName )
  55. {
  56. $field = $namedParameters['field'];
  57. $date = $namedParameters['date'];
  58. if ( !$field )
  59. return $tpl->missingParameter( $operatorName, 'field' );
  60. if ( !$date )
  61. return $tpl->missingParameter( $operatorName, 'date' );
  62. $optional = $namedParameters['optional'];
  63. $dateInfo = getdate( $date );
  64. if ( is_array( $operatorValue ) )
  65. {
  66. $month = array();
  67. $month['year'] = $dateInfo['year'];
  68. $month['month'] = $locale->longMonthName( $dateInfo['mon'] );
  69. $weekDays = $locale->weekDays();
  70. $weekDaysMap = array();
  71. $i = 0;
  72. $dayNames = array( 0 => 'sun', 1 => 'mon', 2 => 'tue',
  73. 3 => 'wed', 4 => 'thu', 5 => 'fri', 6 => 'sat' );
  74. foreach ( $weekDays as $weekDay )
  75. {
  76. $weekDaysMap[$weekDay] = $i;
  77. $weekDayName = $locale->shortDayName( $weekDay );
  78. $weekDayIdentifier = $dayNames[$weekDay];
  79. $month['weekdays'][] = array( 'day' => $weekDayName,
  80. 'class' => $weekDayIdentifier );
  81. ++$i;
  82. }
  83. $days = array();
  84. $lastDay = getdate( mktime( 0, 0, 0, $dateInfo['mon']+1, 0, $dateInfo['year'] ) );
  85. $lastDay = $lastDay['mday'];
  86. for ( $day = 1; $day <= $lastDay; ++$day )
  87. {
  88. $days[$day] = false;
  89. }
  90. foreach ( $operatorValue as $item )
  91. {
  92. $value = null;
  93. if ( is_object( $item ) and
  94. method_exists( $item, 'hasAttribute' ) and
  95. method_exists( $item, 'attribute' ) )
  96. {
  97. if ( $item->hasAttribute( $field ) )
  98. $value = $item->attribute( $field );
  99. }
  100. else if ( is_array( $item ) )
  101. {
  102. if ( array_key_exists( $field, $item ) )
  103. $value = $item[$field];
  104. }
  105. if ( $value !== null )
  106. {
  107. $info = getdate( $value );
  108. if ( $info['year'] == $dateInfo['year'] and
  109. $info['mon'] == $dateInfo['mon'] )
  110. {
  111. $days[$info['mday']] = true;
  112. }
  113. }
  114. }
  115. $currentDay = false;
  116. if ( isset( $optional['current'] ) and $optional['current'] !== false )
  117. {
  118. $info = getdate( $optional['current'] );
  119. $currentDay = $info['yday'];
  120. }
  121. $today = time();
  122. $todayInfo = getdate( $today );
  123. $todayClass = false;
  124. if ( isset( $optional['today_class'] ) )
  125. $todayClass = $optional['today_class'];
  126. $dayClass = false;
  127. if ( isset( $optional['day_class'] ) )
  128. $dayClass = $optional['day_class'];
  129. $link = false;
  130. if ( isset( $optional['link'] ) )
  131. $link = $optional['link'];
  132. $yearLinkParameter = false;
  133. $monthLinkParameter = false;
  134. $dayLinkParameter = false;
  135. if ( isset( $optional['year_link'] ) )
  136. $yearLinkParameter = $optional['year_link'];
  137. if ( isset( $optional['month_link'] ) )
  138. $monthLinkParameter = $optional['month_link'];
  139. if ( isset( $optional['day_link'] ) )
  140. $dayLinkParameter = $optional['day_link'];
  141. $weeks = array();
  142. $lastWeek = 0;
  143. for ( $day = 1; $day <= $lastDay; ++$day )
  144. {
  145. $timestamp = mktime( 0, 0, 0, $dateInfo['mon'], $day, $dateInfo['year'] );
  146. $info = getdate( $timestamp );
  147. $weekDay = $weekDaysMap[$info['wday']];
  148. /*
  149. * Attention: date('W') returns the week number according to
  150. * ISO, which states that the week with the first Thursday
  151. * in the new year is week 1.
  152. */
  153. $week = date( 'W', $timestamp );
  154. if ( $weekDay == 0 && $weekDaysMap[0] == 0 )
  155. {
  156. ++$week;
  157. }
  158. /*
  159. * This checks for a year switch within a week. Routine
  160. * takes care that first days in January might still belong
  161. * to the last week of the old year (according to ISO week
  162. * number), thus be part of week 52 or 53.
  163. */
  164. if ($week >= 52 || $week == 1)
  165. {
  166. // See if it's a new year by comparing the year of the previous week with the
  167. // current one.
  168. $timestampPrevWeek = mktime( 0, 0, 0, $dateInfo['mon'], $day-7, $dateInfo['year'] );
  169. $isNewYear = date('Y', $timestampPrevWeek) < date('Y', $timestamp);
  170. if ($isNewYear && $week != 1)
  171. {
  172. // A new year with the first week having last year's final week number (52 or 53),
  173. // because the week's Thursday lies in the old year.
  174. $week = $lastWeek;
  175. }
  176. else
  177. {
  178. // The last week of December having the week number 1, because
  179. // the week's Thursday lies in the new year.
  180. $week = $lastWeek;
  181. }
  182. if ($weekDay == 0)
  183. {
  184. ++$week;
  185. }
  186. }
  187. $lastWeek = $week;
  188. if ( !isset( $weeks[$week] ) )
  189. {
  190. for ( $i = 0; $i < 7; ++$i )
  191. {
  192. $weeks[$week][] = false;
  193. }
  194. }
  195. $dayData = array( 'day' => $day,
  196. 'link' => false,
  197. 'class' => $dayClass,
  198. 'highlight' => false );
  199. if ( $currentDay == $info['yday'] )
  200. {
  201. if ( isset( $optional['current_class'] ) )
  202. $dayData['class'] = $optional['current_class'];
  203. $dayData['highlight'] = true;
  204. }
  205. if ( $dateInfo['year'] == $todayInfo['year'] and
  206. $dateInfo['mon'] == $todayInfo['mon'] and
  207. $day == $todayInfo['mday'] )
  208. {
  209. if ( $dayData['class'] )
  210. $dayData['class'] .= '-' . $todayClass;
  211. else
  212. $dayData['class'] = $todayClass;
  213. }
  214. if ( $days[$day] )
  215. {
  216. $dayLink = $link;
  217. if ( $dayLink )
  218. {
  219. $dayLink .= '/(year)/' . $info['year'];
  220. $dayLink .= '/(month)/' . $info['mon'];
  221. $dayLink .= '/(day)/' . $info['mday'];
  222. }
  223. $dayData['link'] = $dayLink;
  224. }
  225. $weeks[$week][$weekDay] = $dayData;
  226. }
  227. $next = false;
  228. if ( isset( $optional['next'] ) )
  229. $next = $optional['next'];
  230. if ( $next )
  231. {
  232. $nextTimestamp = mktime( 0, 0, 0, $dateInfo['mon'] + 1, 1, $dateInfo['year'] );
  233. $nextInfo = getdate( $nextTimestamp );
  234. $month['next'] = array( 'month' => $locale->longMonthName( $nextInfo['mon'] ),
  235. 'year' => $nextInfo['year'] );
  236. $nextLink = $next['link'];
  237. $nextLink .= '/(year)/' . $nextInfo['year'];
  238. $nextLink .= '/(month)/' . $nextInfo['mon'];
  239. $month['next']['link'] = $nextLink;
  240. }
  241. else
  242. $month['next'] = false;
  243. $month['current'] = array( 'month' => $locale->longMonthName( $info['mon'] ),
  244. 'year' => $info['year'] );
  245. $currentLink = $next['link'];
  246. $currentLink .= '/(year)/' . $info['year'];
  247. $currentLink .= '/(month)/' . $info['mon'];
  248. $month['current']['link'] = $currentLink;
  249. $previous = false;
  250. if ( isset( $optional['previous'] ) )
  251. {
  252. $previous = $optional['previous'];
  253. }
  254. if ( $previous )
  255. {
  256. $previousTimestamp = mktime( 0, 0, 0, $dateInfo['mon'] - 1, 1, $dateInfo['year'] );
  257. $previousInfo = getdate( $previousTimestamp );
  258. $month['previous'] = array( 'month' => $locale->longMonthName( $previousInfo['mon'] ),
  259. 'year' => $previousInfo['year'] );
  260. $previousLink = $previous['link'];
  261. $previousLink .= '/(year)/' . $previousInfo['year'];
  262. $previousLink .= '/(month)/' . $previousInfo['mon'];
  263. $month['previous']['link'] = $previousLink;
  264. }
  265. else
  266. {
  267. $month['previous'] = false;
  268. }
  269. $month['weeks'] = $weeks;
  270. $operatorValue = $month;
  271. }
  272. }
  273. }
  274. /// \privatesection
  275. public $Operators;
  276. }
  277. ?>