PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/common/ezdateoperatorcollection.php

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