PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/classes/Calendar.inc.php

https://bitbucket.org/linards/dzeerveniite
PHP | 384 lines | 277 code | 56 blank | 51 comment | 76 complexity | ac6fd03a416a2dfe378afb4adb97496a MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /**
  3. * Calendar Generation Class
  4. *
  5. * This class provides a simple reuasable means to produce month calendars in valid html
  6. *
  7. * @version 2.8
  8. * @author Jim Mayes <jim.mayes@gmail.com>
  9. * @link http://style-vs-substance.com
  10. * @copyright Copyright (c) 2008, Jim Mayes
  11. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL v2.0
  12. */
  13. class Calendar{
  14. var $date;
  15. var $year;
  16. var $month;
  17. var $day;
  18. var $week_start_on = FALSE;
  19. var $week_start = 7;// sunday
  20. var $my_link_mode = 1; // normāli
  21. var $link_days = TRUE;
  22. var $link_to;
  23. var $formatted_link_to;
  24. var $mark_today = TRUE;
  25. var $today_date_class = 'today';
  26. var $mark_selected = TRUE;
  27. var $selected_date_class = 'selected';
  28. var $mark_passed = TRUE;
  29. var $passed_date_class = 'passed';
  30. var $highlighted_dates;
  31. var $titles;
  32. var $default_highlighted_class = 'highlighted';
  33. var $days = array("Sv.","P.","O.","T.","C.","P.","S.");
  34. var $days_long = array("svētdiena", "pirmdiena", "otrdiena", "trešdiena", "ceturtdiena", "piektdiena", "sestdiena");
  35. var $months = array("", "janvāris", "februāris", "marts", "aprīlis", "maijs", "jūnijs", "jūlijs", "augusts", "septembris", "oktobris", "novembris", "decembris");
  36. /* CONSTRUCTOR */
  37. function Calendar($date = NULL, $year = NULL, $month = NULL){
  38. $self = htmlspecialchars($_SERVER['PHP_SELF']);
  39. $this->link_to = $self;
  40. if( is_null($year) || is_null($month) ){
  41. if( !is_null($date) ){
  42. //-------- strtotime the submitted date to ensure correct format
  43. $this->date = date("Y-m-j", strtotime($date)); //var_dump($this->date);
  44. } else {
  45. //-------------------------- no date submitted, use today's date
  46. $this->date = date("Y-m-j");
  47. }
  48. $this->set_date_parts_from_date($this->date);
  49. } else {
  50. $this->year = $year;
  51. $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
  52. }
  53. }
  54. function set_date_parts_from_date($date){
  55. $this->year = date("Y", strtotime($date));
  56. $this->month = date("m", strtotime($date));
  57. $this->day = date("d", strtotime($date));
  58. //settype($this->month, "integer"); settype($this->day, "integer");
  59. }
  60. function day_of_week($date){
  61. $day_of_week = date("N", $date);
  62. if( !is_numeric($day_of_week) ){
  63. $day_of_week = date("w", $date);
  64. if( $day_of_week == 0 ){
  65. $day_of_week = 7;
  66. }
  67. }
  68. return $day_of_week;
  69. }
  70. function output_calendar($year = NULL, $month = NULL, $calendar_class = 'calendar'){
  71. if( $this->week_start_on !== FALSE ){
  72. echo "The property week_start_on is replaced due to a bug present in version before 2.6. of this class! Use the property week_start instead!";
  73. exit;
  74. }
  75. //--------------------- override class methods if values passed directly
  76. $year = ( is_null($year) )? $this->year : $year;
  77. $prev_year = $year - 1;
  78. $next_year = $year + 1;
  79. $month = ( is_null($month) )? $this->month : str_pad($month, 2, '0', STR_PAD_LEFT);
  80. //------------------------------------------- create first date of month
  81. $month_start_date = strtotime($year . "-" . $month . "-01");
  82. $prev_month = $month_start_date - 86400;
  83. //------------------------- first day of month falls on what day of week
  84. $first_day_falls_on = $this->day_of_week($month_start_date);
  85. //----------------------------------------- find number of days in month
  86. $days_in_month = date("t", $month_start_date);
  87. //-------------------------------------------- create last date of month
  88. $month_end_date = strtotime($year . "-" . $month . "-" . $days_in_month);
  89. $next_month = $month_end_date + 86400;
  90. //----------------------- calc offset to find number of cells to prepend
  91. $start_week_offset = $first_day_falls_on - $this->week_start;
  92. $prepend = ( $start_week_offset < 0 )? 7 - abs($start_week_offset) : $first_day_falls_on - $this->week_start;
  93. //-------------------------- last day of month falls on what day of week
  94. $last_day_falls_on = $this->day_of_week($month_end_date);
  95. //------------------------------------------------- start table, caption
  96. $output = "<table class=\"" . $calendar_class . "\">\n";
  97. $mon = strftime("%m", $month_start_date); settype($mon,"integer");
  98. $mon = $this->months[$mon];
  99. if(isset($_REQUEST["field"])) $add = "&field=".$_REQUEST["field"];
  100. else $add = "";
  101. if($this->my_link_mode == 1) $output .= "<caption><a target=\"_self\" href=\"$this->link_to?y=" . strftime("%Y", $prev_month) . "&amp;m=" . strftime("%m", $prev_month) . "$add\">&lt;</a> " . $mon . " <a target=\"_self\" href=\"$this->link_to?y=" . strftime("%Y", $next_month) . "&amp;m=" . strftime("%m", $next_month) . "$add\">&gt;</a> " . "<a target=\"_self\" href=\"$this->link_to?y=" . $prev_year . "&amp;m=" . $month . "$add\">&lt;</a> " . strftime("%Y", $month_start_date) . " <a target=\"_self\" href=\"$this->link_to?y=" . $next_year . "&amp;m=" . $month . "$add\">&gt;</a>" . " </caption>\n";
  102. else $output .= "<caption><a href=\"#\" onClick=\"change_cal(" . strftime("%Y", $prev_month) . "," . strftime("%m", $prev_month) . "); return false;\">&lt;</a> " . $mon . " <a href=\"#\" onClick=\"change_cal(" . strftime("%Y", $next_month) . "," . strftime("%m", $next_month) . "); return false;\">&gt;</a> " . "<a href=\"#\" onClick=\"change_cal(" . $prev_year . "," . $month . "); return false;\">&lt;</a> " . strftime("%Y", $month_start_date) . " <a href=\"#\" onClick=\"change_cal(" . $next_year . "," . $month . "); return false;\">&gt;</a>" . " </caption>\n";
  103. $col = '';
  104. $th = '';
  105. for( $i=1,$j=$this->week_start,$t=(3+$this->week_start)*86400; $i<=7; $i++,$j++,$t+=86400 ){
  106. $localized_day_name = gmstrftime('%w',$t);
  107. $col .= "<col class=\"" . strtolower($localized_day_name) ."\" />\n";
  108. $th .= "\t<th title=\"" . ucfirst($localized_day_name) ."\">" . ucfirst($this->days[$localized_day_name]) ."</th>\n";
  109. $j = ( $j == 7 )? 0 : $j;
  110. }
  111. //------------------------------------------------------- markup columns
  112. $output .= $col;
  113. //----------------------------------------------------------- table head
  114. $output .= "<thead>\n";
  115. $output .= "<tr>\n";
  116. $output .= $th;
  117. $output .= "</tr>\n";
  118. $output .= "</thead>\n";
  119. //---------------------------------------------------------- start tbody
  120. $output .= "<tbody>\n";
  121. $output .= "<tr>\n";
  122. //---------------------------------------------- initialize week counter
  123. $weeks = 1;
  124. //--------------------------------------------------- pad start of month
  125. //------------------------------------ adjust for week start on saturday
  126. $last_day_prev = date('d',$prev_month);
  127. for($i=1;$i<=$prepend;$i++){
  128. $d = $last_day_prev - $prepend + $i;
  129. $day_date = date('Y', $prev_month).'-'.date('m', $prev_month).'-'.$d;
  130. $y = strftime("%Y", strtotime($day_date)); $m = strftime("%m", strtotime($day_date)); $wd = strftime("%w", strtotime($day_date)); $d = strftime("%e", strtotime($day_date)); settype($m,"integer");
  131. if( is_array($this->highlighted_dates) ){
  132. if( in_array($day_date, $this->highlighted_dates) ){
  133. $class = " ".$this->default_highlighted_class;
  134. $title = "\n".$this->titles[$day_date];
  135. }
  136. else{ $class = ""; $title = ""; }
  137. }
  138. else{ $class = ""; $title = ""; }
  139. $output .= "\t<td class=\"pad$class\" id=\"" . strftime("%Y-%m-%d", strtotime($day_date)) . "\" title=\"" . $this->days_long[$wd] . ", $y. gada $d. " . $this->months[$m] . $title . "\">";
  140. unset($title, $class);
  141. switch( $this->link_days ){
  142. case 0 :
  143. $output .= $d;
  144. break;
  145. case 1 :
  146. if( empty($this->formatted_link_to) ){
  147. $output .= "<a href=\"" . $this->link_to . "?date=" . $day_date . "\">" . $d . "</a>";
  148. } else {
  149. $output .= "<a href=\"" . strftime($this->formatted_link_to, strtotime($day_date)) . "\">" . $d . "</a>";
  150. }
  151. break;
  152. case 2 :
  153. if( is_array($this->highlighted_dates) ){ //var_dump($day_date);
  154. if( in_array($day_date, $this->highlighted_dates) ){
  155. if( empty($this->formatted_link_to) ){
  156. $output .= "<a href=\"" . $this->link_to . "?date=" . $day_date . "\">";
  157. } else {
  158. $output .= "<a href=\"" . strftime($this->formatted_link_to, strtotime($day_date)) . "\">";
  159. }
  160. }
  161. }
  162. $output .= $d;
  163. if( is_array($this->highlighted_dates) ){
  164. if( in_array($day_date, $this->highlighted_dates) ){
  165. if( empty($this->formatted_link_to) ){
  166. $output .= "</a>";
  167. } else {
  168. $output .= "</a>";
  169. }
  170. }
  171. }
  172. break;
  173. }
  174. $output .= "</td>\n";
  175. }
  176. //--------------------------------------------------- loop days of month
  177. for($day=1,$cell=$prepend+1; $day<=$days_in_month; $day++,$cell++){
  178. $title = "";
  179. /*
  180. if this is first cell and not also the first day, end previous row
  181. */
  182. if( $cell == 1 && $day != 1 ){
  183. $output .= "<tr>\n";
  184. }
  185. //-------------- zero pad day and create date string for comparisons
  186. //$day = str_pad($day, 2, '0', STR_PAD_LEFT);
  187. $day_date = $year . "-" . $month . "-" . $day;
  188. //-------------------------- compare day and add classes for matches
  189. if( $this->mark_today == TRUE && $day_date == date("Y-m-d") ){
  190. $classes[] = $this->today_date_class;
  191. }
  192. if( $this->mark_selected == TRUE && $day_date == $this->date ){
  193. //echo $day_date;
  194. $classes[] = $this->selected_date_class;
  195. }
  196. if( $this->mark_passed == TRUE && $day_date < date("Y-m-d") ){
  197. $classes[] = $this->passed_date_class;
  198. }
  199. if( is_array($this->highlighted_dates) ){
  200. if( in_array($day_date, $this->highlighted_dates) ){
  201. $classes[] = $this->default_highlighted_class;
  202. $title = "\n".$this->titles[$day_date];
  203. }
  204. }
  205. //----------------- loop matching class conditions, format as string
  206. if( isset($classes) ){
  207. $day_class = ' class="';
  208. foreach( $classes AS $value ){
  209. $day_class .= $value . " ";
  210. }
  211. $day_class = substr($day_class, 0, -1) . '"';
  212. } else {
  213. $day_class = '';
  214. }
  215. //---------------------------------- start table cell, apply classes
  216. // detect windows os and substitute for unsupported day of month modifer
  217. $y = strftime("%Y", strtotime($day_date)); $m = strftime("%m", strtotime($day_date)); $wd = strftime("%w", strtotime($day_date)); $d = strftime("%e", strtotime($day_date)); settype($m,"integer");
  218. $title_format = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')? "%A, %B %#d, %Y": "%A, %B %e, %Y";
  219. //var_dump($day_date);
  220. //$output .= "\t<td" . $day_class . " title=\"" . ucwords(strftime($title_format, strtotime($day_date))) . "\">";
  221. $output .= "\t<td" . $day_class . " id=\"" . strftime("%Y-%m-%d", strtotime($day_date)) . "\" title=\"" . $this->days_long[$wd] . ", $y. gada $d. " . $this->months[$m] . $title . "\">";
  222. //----------------------------------------- unset to keep loop clean
  223. unset($day_class, $classes, $title);
  224. //-------------------------------------- conditional, start link tag
  225. switch( $this->link_days ){
  226. case 0 :
  227. $output .= $day;
  228. break;
  229. case 1 :
  230. if( empty($this->formatted_link_to) ){
  231. $output .= "<a href=\"" . $this->link_to . "?date=" . $day_date . "\">" . $day . "</a>";
  232. } else {
  233. $output .= "<a href=\"" . strftime($this->formatted_link_to, strtotime($day_date)) . "\">" . $day . "</a>";
  234. }
  235. break;
  236. case 2 :
  237. if( is_array($this->highlighted_dates) ){ //var_dump($day_date);
  238. if( in_array($day_date, $this->highlighted_dates) ){
  239. if( empty($this->formatted_link_to) ){
  240. $output .= "<a href=\"" . $this->link_to . "?date=" . $day_date . "\">";
  241. } else {
  242. $output .= "<a href=\"" . strftime($this->formatted_link_to, strtotime($day_date)) . "\">";
  243. }
  244. }
  245. }
  246. $output .= $day;
  247. if( is_array($this->highlighted_dates) ){
  248. if( in_array($day_date, $this->highlighted_dates) ){
  249. if( empty($this->formatted_link_to) ){
  250. $output .= "</a>";
  251. } else {
  252. $output .= "</a>";
  253. }
  254. }
  255. }
  256. break;
  257. }
  258. //------------------------------------------------- close table cell
  259. $output .= "</td>\n";
  260. //------- if this is the last cell, end the row and reset cell count
  261. if( $cell == 7 ){
  262. $output .= "</tr>\n";
  263. $cell = 0;
  264. }
  265. }
  266. //----------------------------------------------------- pad end of month
  267. if( $cell > 1 ){
  268. //$first_day_next = date('d',$prev_month);
  269. for($i=$cell;$i<=7;$i++){
  270. $d = 1 + $i - $cell;
  271. $day_date = date('Y', $next_month).'-'.date('m', $next_month).'-'.$d;
  272. $y = strftime("%Y", strtotime($day_date)); $m = strftime("%m", strtotime($day_date)); $wd = strftime("%w", strtotime($day_date)); $d = strftime("%e", strtotime($day_date)); settype($m,"integer");
  273. if( is_array($this->highlighted_dates) ){
  274. if( in_array($day_date, $this->highlighted_dates) ){
  275. $class = " ".$this->default_highlighted_class;
  276. $title = "\n".$this->titles[$day_date];
  277. }
  278. else{ $class = ""; $title = ""; }
  279. }
  280. else{ $class = ""; $title = ""; }
  281. $output .= "\t<td class=\"pad$class\" id=\"" . strftime("%Y-%m-%d", strtotime($day_date)) . "\" title=\"" . $this->days_long[$wd] . ", $y. gada $d. " . $this->months[$m] . $title. "\">";
  282. unset($title, $class);
  283. switch( $this->link_days ){
  284. case 0 :
  285. $output .= $d;
  286. break;
  287. case 1 :
  288. if( empty($this->formatted_link_to) ){
  289. $output .= "<a href=\"" . $this->link_to . "?date=" . $day_date . "\">" . $d . "</a>";
  290. } else {
  291. $output .= "<a href=\"" . strftime($this->formatted_link_to, strtotime($day_date)) . "\">" . $d . "</a>";
  292. }
  293. break;
  294. case 2 :
  295. if( is_array($this->highlighted_dates) ){ //var_dump($day_date);
  296. if( in_array($day_date, $this->highlighted_dates) ){
  297. if( empty($this->formatted_link_to) ){
  298. $output .= "<a href=\"" . $this->link_to . "?date=" . $day_date . "\">";
  299. } else {
  300. $output .= "<a href=\"" . strftime($this->formatted_link_to, strtotime($day_date)) . "\">";
  301. }
  302. }
  303. }
  304. $output .= $d;
  305. if( is_array($this->highlighted_dates) ){
  306. if( in_array($day_date, $this->highlighted_dates) ){
  307. if( empty($this->formatted_link_to) ){
  308. $output .= "</a>";
  309. } else {
  310. $output .= "</a>";
  311. }
  312. }
  313. }
  314. break;
  315. }
  316. $output .= "</td>\n";
  317. //$output .= "\t<td class=\"pad\">&nbsp;</td>\n";
  318. }
  319. $output .= "</tr>\n";
  320. }
  321. //--------------------------------------------- close last row and table
  322. $output .= "</tbody>\n";
  323. $output .= "</table>\n";
  324. //--------------------------------------------------------------- return
  325. return $output;
  326. }
  327. }
  328. ?>