PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/calendar.php

https://github.com/mindtonic/shabda
PHP | 117 lines | 72 code | 20 blank | 25 comment | 12 complexity | da2b83a9b748a3205d5dc2025e29e205 MD5 | raw file
  1. <?php
  2. # http://keithdevens.com/software/php_calendar
  3. class calendar
  4. {
  5. public $year;
  6. public $month;
  7. public $days = null;
  8. public $day_name_length = 3;
  9. public $month_href = null;
  10. public $first_day = 0;
  11. public $pn;
  12. private $timestamp;
  13. function __construct()
  14. {
  15. $registry = Registry::instance();
  16. if ($registry->request('d')) $this->timestamp = $registry->request('d');
  17. else $this->timestamp = time();
  18. $this->year = date('Y', $this->timestamp);
  19. $this->month = date('n', $this->timestamp);
  20. $this->pn = array('&laquo;'=> 'index.php?c=events&d='.strtotime('-1 month', $this->timestamp),
  21. '&raquo;'=> 'index.php?c=events&d='.strtotime('+1 month', $this->timestamp));
  22. }
  23. function set_days()
  24. {
  25. /*
  26. $this->days = array();
  27. for ($i = 1, $i < 20, $i++)
  28. $this->days[$i] = array($this->day_link($i),'linked-day');
  29. */
  30. }
  31. function day_link($day)
  32. {
  33. return 'index.php?c=events&d='.$this->calculate_day($day);
  34. }
  35. function calculate_day($day)
  36. {
  37. return mktime(0, 0, 0, $this->month, $day, $this->year);
  38. }
  39. function current_date()
  40. {
  41. return date('l, F jS, Y', $this->timestamp);
  42. }
  43. function render()
  44. {
  45. return $this->generate_calendar($this->year, $this->month, $this->days, $this->day_name_length, $this->month_href, $this->first_day, $this->pn);
  46. }
  47. # PHP Calendar (version 2.3), written by Keith Devens
  48. # http://keithdevens.com/software/php_calendar
  49. # see example at http://keithdevens.com/weblog
  50. # License: http://keithdevens.com/software/license
  51. function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array())
  52. {
  53. $first_of_month = gmmktime(0,0,0,$month,1,$year);
  54. #remember that mktime will automatically correct if invalid dates are entered
  55. # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
  56. # this provides a built in "rounding" feature to generate_calendar()
  57. $day_names = array(); #generate all the day names according to the current locale
  58. for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
  59. $day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name
  60. list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
  61. $weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
  62. $title = htmlentities(ucfirst($month_name)).'&nbsp;'.$year; #note that some locales don't capitalize month and day names
  63. #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
  64. @list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
  65. if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span>&nbsp;';
  66. if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
  67. $calendar = '<table class="calendar">'."\n".
  68. '<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";
  69. if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
  70. #if day_name_length is >3, the full name of the day will be printed
  71. foreach($day_names as $d)
  72. $calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
  73. $calendar .= "</tr>\n<tr>";
  74. }
  75. if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; #initial 'empty' days
  76. for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
  77. if($weekday == 7){
  78. $weekday = 0; #start a new week
  79. $calendar .= "</tr>\n<tr>";
  80. }
  81. /*
  82. if(isset($days[$day]) and is_array($days[$day])){
  83. @list($link, $classes, $content) = $days[$day];
  84. if(is_null($content)) $content = $day;
  85. $calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
  86. ($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
  87. }
  88. else $calendar .= "<td>$day</td>";
  89. */
  90. // Adapt so that all days are automaticaly linked
  91. $calendar .= '<td class="linked-day"><a href="'.htmlspecialchars($this->day_link($day)).'">'.$day.'</a></td>';
  92. }
  93. if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days
  94. return $calendar."</tr>\n</table>\n";
  95. }
  96. }
  97. ?>