PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Molinos/DateTime/Calendar.php

https://code.google.com/p/molinos-cms/
PHP | 394 lines | 177 code | 65 blank | 152 comment | 28 complexity | f344bd9f93461164d1165bbd193600ec MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * PHP Calendar Class Version 1.4 (5th March 2001)
  4. *
  5. * Copyright David Wilkinson 2000 - 2001. All Rights reserved.
  6. *
  7. * This software may be used, modified and distributed freely
  8. * providing this copyright notice remains intact at the head
  9. * of the file.
  10. *
  11. * This software is freeware. The author accepts no liability for
  12. * any loss or damages whatsoever incurred directly or indirectly
  13. * from the use of this script. The author of this software makes
  14. * no claims as to its fitness for any purpose whatsoever. If you
  15. * wish to use this software you should first satisfy yourself that
  16. * it meets your requirements.
  17. *
  18. * Modified by a.ivanov@molinos.ru
  19. *
  20. * @package Molinos_CMS
  21. * @subpackage base
  22. * @author davidw@cascade.org.uk
  23. * @link http://www.cascade.org.uk/software/php/calendar/
  24. */
  25. class Molinos_DateTime_Calendar
  26. {
  27. /**
  28. * The start day of the week. This is the day that appears in the first column
  29. * of the calendar. Sunday = 0.
  30. */
  31. private $startDay = 1;
  32. /**
  33. * The start month of the year. This is the month that appears in the first slot
  34. * of the calendar in the year view. January = 1.
  35. */
  36. private $startMonth = 1;
  37. /**
  38. * The labels to display for the days of the week. The first entry in this array
  39. * represents Sunday.
  40. */
  41. private $dayNames = array("S", "M", "T", "W", "T", "F", "S");
  42. /**
  43. * The labels to display for the months of the year. The first entry in this array
  44. * represents January.
  45. */
  46. private $monthNames = array("January", "February", "March", "April", "May", "June",
  47. "July", "August", "September", "October", "November", "December");
  48. /**
  49. * The number of days in each month. You're unlikely to want to change this...
  50. * The first entry in this array represents January.
  51. */
  52. private $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  53. function __construct() { }
  54. /**
  55. * Get the array of strings used to label the days of the week. This array contains seven
  56. * elements, one for each day of the week. The first entry in this array represents Sunday.
  57. */
  58. function getDayNames() {
  59. return $this->dayNames;
  60. }
  61. /*
  62. Set the array of strings used to label the days of the week. This array must contain seven
  63. elements, one for each day of the week. The first entry in this array represents Sunday.
  64. */
  65. function setDayNames($names) {
  66. $this->dayNames = $names;
  67. }
  68. /*
  69. Get the array of strings used to label the months of the year. This array contains twelve
  70. elements, one for each month of the year. The first entry in this array represents January.
  71. */
  72. function getMonthNames() {
  73. return $this->monthNames;
  74. }
  75. /*
  76. Set the array of strings used to label the months of the year. This array must contain twelve
  77. elements, one for each month of the year. The first entry in this array represents January.
  78. */
  79. function setMonthNames($names) {
  80. $this->monthNames = $names;
  81. }
  82. /*
  83. Gets the start day of the week. This is the day that appears in the first column
  84. of the calendar. Sunday = 0.
  85. */
  86. function getStartDay() {
  87. return $this->startDay;
  88. }
  89. /*
  90. Sets the start day of the week. This is the day that appears in the first column
  91. of the calendar. Sunday = 0.
  92. */
  93. function setStartDay($day) {
  94. $this->startDay = $day;
  95. }
  96. /*
  97. Gets the start month of the year. This is the month that appears first in the year
  98. view. January = 1.
  99. */
  100. function getStartMonth() {
  101. return $this->startMonth;
  102. }
  103. /*
  104. Sets the start month of the year. This is the month that appears first in the year
  105. view. January = 1.
  106. */
  107. function setStartMonth($month) {
  108. $this->startMonth = $month;
  109. }
  110. /*
  111. Return the URL to link to in order to display a calendar for a given month/year.
  112. You must override this method if you want to activate the "forward" and "back"
  113. feature of the calendar.
  114. Note: If you return an empty string from this function, no navigation link will
  115. be displayed. This is the default behaviour.
  116. If the calendar is being displayed in "year" view, $month will be set to zero.
  117. */
  118. function getCalendarLink($month, $year) {
  119. return "";
  120. }
  121. /*
  122. Return the URL to link to for a given date.
  123. You must override this method if you want to activate the date linking
  124. feature of the calendar.
  125. Note: If you return an empty string from this function, no navigation link will
  126. be displayed. This is the default behaviour.
  127. */
  128. function getDateLink($day, $month, $year) {
  129. return "";
  130. }
  131. /*
  132. Return the HTML for the current month
  133. */
  134. function getCurrentMonthView() {
  135. $d = getdate(time());
  136. return $this->getMonthView($d["mon"], $d["year"]);
  137. }
  138. /*
  139. Return the HTML for the current year
  140. */
  141. function getCurrentYearView() {
  142. $d = getdate(time());
  143. return $this->getYearView($d["year"]);
  144. }
  145. /*
  146. Return the HTML for a specified month
  147. */
  148. function getMonthView($month, $year) {
  149. return $this->getMonthHTML($month, $year);
  150. }
  151. /*
  152. Return the HTML for a specified year
  153. */
  154. function getYearView($year) {
  155. return $this->getYearHTML($year);
  156. }
  157. /********************************************************************************
  158. The rest are private methods. No user-servicable parts inside.
  159. You shouldn't need to call any of these functions directly.
  160. *********************************************************************************/
  161. /*
  162. Calculate the number of days in a month, taking into account leap years.
  163. */
  164. function getDaysInMonth($month, $year) {
  165. return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  166. // WTF is below???
  167. if ($month < 1 || $month > 12)
  168. {
  169. return 0;
  170. }
  171. $d = $this->daysInMonth[$month - 1];
  172. if ($month == 2)
  173. {
  174. // Check for leap year
  175. // Forget the 4000 rule, I doubt I'll be around then...
  176. if ($year%4 == 0)
  177. {
  178. if ($year%100 == 0)
  179. {
  180. if ($year%400 == 0)
  181. {
  182. $d = 29;
  183. }
  184. }
  185. else
  186. {
  187. $d = 29;
  188. }
  189. }
  190. }
  191. return $d;
  192. }
  193. /*
  194. Generate the HTML for a given month
  195. */
  196. function getMonthHTML($m, $y, $showYear = 1) {
  197. // $s = "";
  198. $s = array();
  199. $a = $this->adjustDate($m, $y);
  200. $month = $a[0];
  201. $year = $a[1];
  202. $daysInMonth = $this->getDaysInMonth($month, $year);
  203. $date = getdate(mktime(12, 0, 0, $month, 1, $year));
  204. $first = $date["wday"];
  205. $monthName = $this->monthNames[$month - 1];
  206. $prev = $this->adjustDate($month - 1, $year);
  207. $next = $this->adjustDate($month + 1, $year);
  208. if ($showYear == 1)
  209. {
  210. $prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
  211. $nextMonth = $this->getCalendarLink($next[0], $next[1]);
  212. }
  213. else
  214. {
  215. $prevMonth = "";
  216. $nextMonth = "";
  217. }
  218. $header = $monthName . (($showYear > 0) ? " " . $year : "");
  219. // $s .= "<table class=\"calendar\">\n";
  220. // $s .= "<tr>\n";
  221. // $s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? "&nbsp;" : "<a href=\"$prevMonth\">&lt;&lt;</a>") . "</td>\n";
  222. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n";
  223. // $s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? "&nbsp;" : "<a href=\"$nextMonth\">&gt;&gt;</a>") . "</td>\n";
  224. // $s .= "</tr>\n";
  225. // $s .= "<tr>\n";
  226. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
  227. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
  228. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
  229. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
  230. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
  231. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
  232. // $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
  233. // $s .= "</tr>\n";
  234. // We need to work out what date to start at so that the first appears in the correct column
  235. $d = $this->startDay + 1 - $first;
  236. while ($d > 1) {
  237. $d -= 7;
  238. }
  239. // Make sure we know when today is, so that we can use a different CSS style
  240. $today = getdate(time());
  241. while ($d <= $daysInMonth)
  242. {
  243. $tmp = array();
  244. // $s .= "<tr>\n";
  245. for ($i = 0; $i < 7; $i++)
  246. {
  247. $class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
  248. // $s .= "<td class=\"$class\" align=\"right\" valign=\"top\">";
  249. if ($d > 0 && $d <= $daysInMonth) {
  250. $link = $this->getDateLink($d, $month, $year);
  251. // $s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
  252. $tmp[] = (($link == "") ? $d : "<a href=\"$link\">$d</a>");
  253. } else {
  254. $tmp[] = null;
  255. }
  256. // $s .= "</td>\n";
  257. $d++;
  258. }
  259. // $s .= "</tr>\n";
  260. array_push($s, $tmp);
  261. }
  262. // $s .= "</table>\n";
  263. return $s;
  264. }
  265. /*
  266. Generate the HTML for a given year
  267. */
  268. function getYearHTML($year)
  269. {
  270. $s = "";
  271. $prev = $this->getCalendarLink(0, $year - 1);
  272. $next = $this->getCalendarLink(0, $year + 1);
  273. $s .= "<table class=\"calendar\" border=\"0\">\n";
  274. $s .= "<tr>";
  275. $s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? "&nbsp;" : "<a href=\"$prev\">&lt;&lt;</a>") . "</td>\n";
  276. $s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
  277. $s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? "&nbsp;" : "<a href=\"$next\">&gt;&gt;</a>") . "</td>\n";
  278. $s .= "</tr>\n";
  279. $s .= "<tr>";
  280. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
  281. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
  282. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
  283. $s .= "</tr>\n";
  284. $s .= "<tr>\n";
  285. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
  286. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
  287. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
  288. $s .= "</tr>\n";
  289. $s .= "<tr>\n";
  290. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
  291. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
  292. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
  293. $s .= "</tr>\n";
  294. $s .= "<tr>\n";
  295. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
  296. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
  297. $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
  298. $s .= "</tr>\n";
  299. $s .= "</table>\n";
  300. return $s;
  301. }
  302. /*
  303. Adjust dates to allow months > 12 and < 0. Just adjust the years appropriately.
  304. e.g. Month 14 of the year 2001 is actually month 2 of year 2002.
  305. */
  306. function adjustDate($month, $year)
  307. {
  308. $a = array();
  309. $a[0] = $month;
  310. $a[1] = $year;
  311. while ($a[0] > 12)
  312. {
  313. $a[0] -= 12;
  314. $a[1]++;
  315. }
  316. while ($a[0] <= 0)
  317. {
  318. $a[0] += 12;
  319. $a[1]--;
  320. }
  321. return $a;
  322. }
  323. }