PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/include/Calendar/Table/Helper.php

https://github.com/openpne/OpenPNE2
PHP | 280 lines | 130 code | 19 blank | 131 comment | 10 complexity | 373dbb039b9b3c2b3863f53fb319e6d9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4 |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license, |
  10. // | that is bundled with this package in the file LICENSE, and is |
  11. // | available at through the world-wide-web at |
  12. // | http://www.php.net/license/3_0.txt. |
  13. // | If you did not receive a copy of the PHP license and are unable to |
  14. // | obtain it through the world-wide-web, please send a note to |
  15. // | license@php.net so we can mail you a copy immediately. |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Harry Fuecks <hfuecks@phppatterns.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Helper.php,v 1.5 2005/10/22 09:51:53 quipo Exp $
  21. //
  22. /**
  23. * @package Calendar
  24. * @version $Id: Helper.php,v 1.5 2005/10/22 09:51:53 quipo Exp $
  25. */
  26. /**
  27. * Used by Calendar_Month_Weekdays, Calendar_Month_Weeks and Calendar_Week to
  28. * help with building the calendar in tabular form
  29. * @package Calendar
  30. * @access protected
  31. */
  32. class Calendar_Table_Helper
  33. {
  34. /**
  35. * Instance of the Calendar object being helped.
  36. * @var object
  37. * @access private
  38. */
  39. var $calendar;
  40. /**
  41. * Instance of the Calendar_Engine
  42. * @var object
  43. * @access private
  44. */
  45. var $cE;
  46. /**
  47. * First day of the week
  48. * @access private
  49. * @var string
  50. */
  51. var $firstDay;
  52. /**
  53. * The seven days of the week named
  54. * @access private
  55. * @var array
  56. */
  57. var $weekDays;
  58. /**
  59. * Days of the week ordered with $firstDay at the beginning
  60. * @access private
  61. * @var array
  62. */
  63. var $daysOfWeek = array();
  64. /**
  65. * Days of the month built from days of the week
  66. * @access private
  67. * @var array
  68. */
  69. var $daysOfMonth = array();
  70. /**
  71. * Number of weeks in month
  72. * @var int
  73. * @access private
  74. */
  75. var $numWeeks = null;
  76. /**
  77. * Number of emtpy days before real days begin in month
  78. * @var int
  79. * @access private
  80. */
  81. var $emptyBefore = 0;
  82. /**
  83. * Constructs Calendar_Table_Helper
  84. * @param object Calendar_Month_Weekdays, Calendar_Month_Weeks, Calendar_Week
  85. * @param int (optional) first day of the week e.g. 1 for Monday
  86. * @access protected
  87. */
  88. function Calendar_Table_Helper(& $calendar, $firstDay=null)
  89. {
  90. $this->calendar = & $calendar;
  91. $this->cE = & $calendar->getEngine();
  92. if (is_null($firstDay)) {
  93. $firstDay = $this->cE->getFirstDayOfWeek(
  94. $this->calendar->thisYear(),
  95. $this->calendar->thisMonth(),
  96. $this->calendar->thisDay()
  97. );
  98. }
  99. $this->firstDay = $firstDay;
  100. $this->setFirstDay();
  101. $this->setDaysOfMonth();
  102. }
  103. /**
  104. * Constructs $this->daysOfWeek based on $this->firstDay
  105. * @return void
  106. * @access private
  107. */
  108. function setFirstDay()
  109. {
  110. $weekDays = $this->cE->getWeekDays(
  111. $this->calendar->thisYear(),
  112. $this->calendar->thisMonth(),
  113. $this->calendar->thisDay()
  114. );
  115. $endDays = array();
  116. $tmpDays = array();
  117. $begin = false;
  118. foreach ($weekDays as $day) {
  119. if ($begin) {
  120. $endDays[] = $day;
  121. } else if ($day === $this->firstDay) {
  122. $begin = true;
  123. $endDays[] = $day;
  124. } else {
  125. $tmpDays[] = $day;
  126. }
  127. }
  128. $this->daysOfWeek = array_merge($endDays, $tmpDays);
  129. }
  130. /**
  131. * Constructs $this->daysOfMonth
  132. * @return void
  133. * @access private
  134. */
  135. function setDaysOfMonth()
  136. {
  137. $this->daysOfMonth = $this->daysOfWeek;
  138. $daysInMonth = $this->cE->getDaysInMonth(
  139. $this->calendar->thisYear(), $this->calendar->thisMonth());
  140. $firstDayInMonth = $this->cE->getFirstDayInMonth(
  141. $this->calendar->thisYear(), $this->calendar->thisMonth());
  142. $this->emptyBefore=0;
  143. foreach ($this->daysOfMonth as $dayOfWeek) {
  144. if ($firstDayInMonth == $dayOfWeek) {
  145. break;
  146. }
  147. $this->emptyBefore++;
  148. }
  149. $this->numWeeks = ceil(
  150. ($daysInMonth + $this->emptyBefore)
  151. /
  152. $this->cE->getDaysInWeek(
  153. $this->calendar->thisYear(),
  154. $this->calendar->thisMonth(),
  155. $this->calendar->thisDay()
  156. )
  157. );
  158. for ($i=1; $i < $this->numWeeks; $i++) {
  159. $this->daysOfMonth =
  160. array_merge($this->daysOfMonth, $this->daysOfWeek);
  161. }
  162. }
  163. /**
  164. * Returns the first day of the month
  165. * @see Calendar_Engine_Interface::getFirstDayOfWeek()
  166. * @return int
  167. * @access protected
  168. */
  169. function getFirstDay()
  170. {
  171. return $this->firstDay;
  172. }
  173. /**
  174. * Returns the order array of days in a week
  175. * @return int
  176. * @access protected
  177. */
  178. function getDaysOfWeek()
  179. {
  180. return $this->daysOfWeek;
  181. }
  182. /**
  183. * Returns the number of tabular weeks in a month
  184. * @return int
  185. * @access protected
  186. */
  187. function getNumWeeks()
  188. {
  189. return $this->numWeeks;
  190. }
  191. /**
  192. * Returns the number of real days + empty days
  193. * @return int
  194. * @access protected
  195. */
  196. function getNumTableDaysInMonth()
  197. {
  198. return count($this->daysOfMonth);
  199. }
  200. /**
  201. * Returns the number of empty days before the real days begin
  202. * @return int
  203. * @access protected
  204. */
  205. function getEmptyDaysBefore()
  206. {
  207. return $this->emptyBefore;
  208. }
  209. /**
  210. * Returns the index of the last real day in the month
  211. * @todo Potential performance optimization with static
  212. * @return int
  213. * @access protected
  214. */
  215. function getEmptyDaysAfter()
  216. {
  217. // Causes bug when displaying more than one month
  218. // static $index;
  219. // if (!isset($index)) {
  220. $index = $this->getEmptyDaysBefore() + $this->cE->getDaysInMonth(
  221. $this->calendar->thisYear(), $this->calendar->thisMonth());
  222. // }
  223. return $index;
  224. }
  225. /**
  226. * Returns the index of the last real day in the month, relative to the
  227. * beginning of the tabular week it is part of
  228. * @return int
  229. * @access protected
  230. */
  231. function getEmptyDaysAfterOffset()
  232. {
  233. $eAfter = $this->getEmptyDaysAfter();
  234. return $eAfter - (
  235. $this->cE->getDaysInWeek(
  236. $this->calendar->thisYear(),
  237. $this->calendar->thisMonth(),
  238. $this->calendar->thisDay()
  239. ) * ($this->numWeeks-1) );
  240. }
  241. /**
  242. * Returns the timestamp of the first day of the current week
  243. */
  244. function getWeekStart($y, $m, $d, $firstDay=1)
  245. {
  246. $dow = $this->cE->getDayOfWeek($y, $m, $d);
  247. if ($dow > $firstDay) {
  248. $d -= ($dow - $firstDay);
  249. }
  250. if ($dow < $firstDay) {
  251. $d -= (
  252. $this->cE->getDaysInWeek(
  253. $this->calendar->thisYear(),
  254. $this->calendar->thisMonth(),
  255. $this->calendar->thisDay()
  256. ) - $firstDay + $dow);
  257. }
  258. return $this->cE->dateToStamp($y, $m, $d);
  259. }
  260. }
  261. ?>