PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/session-fix/flatpress/fp-plugins/calendar/plugin.calendar.php

https://bitbucket.org/alexandrul/flatpress
PHP | 101 lines | 56 code | 25 blank | 20 comment | 14 complexity | 578bbb399151c9d0893dc4f1065db024 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MIT
  1. <?php
  2. /*
  3. Plugin Name: Calendar
  4. Version: 1.0
  5. Plugin URI: http://flatpress.sf.net
  6. Type: Block
  7. Description: Adds a Calendar block level element
  8. Author: NoWhereMan
  9. Author URI: http://flatpress.sf.net
  10. */
  11. # PHP Calendar (version 2.3), written by Keith Devens
  12. # http://keithdevens.com/software/php_calendar
  13. # see example at http://keithdevens.com/weblog
  14. # License: http://keithdevens.com/software/license
  15. function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()){
  16. $first_of_month = gmmktime(0,0,0,$month,1,$year);
  17. #remember that mktime will automatically correct if invalid dates are entered
  18. # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
  19. # this provides a built in "rounding" feature to generate_calendar()
  20. $day_names = array(); #generate all the day names according to the current locale
  21. for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
  22. $day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name
  23. list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
  24. $weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
  25. $title = htmlentities(ucfirst($month_name)).'&nbsp;'.$year; #note that some locales don't capitalize month and day names
  26. #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
  27. @list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
  28. if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span>&nbsp;';
  29. if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
  30. $calendar = '<table class="calendar">'."\n".
  31. '<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";
  32. if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
  33. #if day_name_length is >3, the full name of the day will be printed
  34. foreach($day_names as $d)
  35. $calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
  36. $calendar .= "</tr>\n<tr>";
  37. }
  38. if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; #initial 'empty' days
  39. for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
  40. if($weekday == 7){
  41. $weekday = 0; #start a new week
  42. $calendar .= "</tr>\n<tr>";
  43. }
  44. if(isset($days[$day]) and is_array($days[$day])){
  45. @list($link, $classes, $content) = $days[$day];
  46. if(is_null($content)) $content = $day;
  47. $calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
  48. ($link ? '<a class="calendar-day" href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
  49. }
  50. else $calendar .= "<td>$day</td>";
  51. }
  52. if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days
  53. return $calendar."</tr>\n</table>\n";
  54. }
  55. function plugin_calendar_widget() {
  56. $y = isset($_GET['y'])? $_GET['y'] : date('y');
  57. $m = isset($_GET['m'])? $_GET['m'] : date('m');
  58. global $fpdb;
  59. $q =& new FPDB_Query(array('fullparse'=>false,'y'=>$y,'m'=>$m, 'count' => -1), null);
  60. $days = array();
  61. while ($q->hasmore($queryId)) {
  62. list($id, $entry) = $q->getEntry($queryId);
  63. $date = date_from_id($id);
  64. $d = (int)$date['d'];
  65. $days[$d] = array(get_day_link($y, $m, $d), 'linked-day');
  66. $count++;
  67. }
  68. // load plugin strings
  69. // they're located under plugin.PLUGINNAME/lang/LANGID/
  70. $lang = lang_load('plugin:calendar');
  71. $entry['subject'] = $lang['plugin']['calendar']['subject'];
  72. $entry['content'] = '<ul id="widget_calendar"><li>'. generate_calendar($y,$m, $days).'</li></ul>';
  73. return $entry;
  74. }
  75. register_widget('calendar', 'Calendar', 'plugin_calendar_widget');
  76. ?>