PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/mod_k2_tools/includes/calendarClass.php

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