PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/flatpress/fp-plugins/calendar/plugin.calendar.php

https://bitbucket.org/alexandrul/flatpress
PHP | 103 lines | 57 code | 26 blank | 20 comment | 14 complexity | b98cb175c15f89537280053ed2363ad2 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MIT
  1. <?php
  2. /*
  3. Plugin Name: Calendar
  4. Version: 1.1
  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(date_strformat('%A',$t)); #%A means full textual day name
  23. list($month, $year, $month_name, $weekday) = explode(',',date_strformat('%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="'.($pl).'">'.$p.'</a>' : $p).'</span>&nbsp;';
  29. if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.($nl).'">'.$n.'</a>' : $n).'</span>';
  30. $calendar = '<table class="calendar">'."\n".
  31. '<caption class="calendar-month">'.$p.($month_href ? '<a href="'.($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="'.($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. global $fp_params;
  57. $y = isset($fp_params['y'])? $fp_params['y'] : date('y');
  58. $m = isset($fp_params['m'])? $fp_params['m'] : date('m');
  59. global $fpdb;
  60. $q =& new FPDB_Query(array('fullparse'=>false,'y'=>$y,'m'=>$m, 'count' => -1), null);
  61. $days = array();
  62. while ($q->hasmore($queryId)) {
  63. list($id, $entry) = $q->getEntry($queryId);
  64. $date = date_from_id($id);
  65. $d = (int)$date['d'];
  66. $days[$d] = array(get_day_link($y, $m, str_pad($d, 2, '0', STR_PAD_LEFT)), 'linked-day');
  67. $count++;
  68. }
  69. // load plugin strings
  70. // they're located under plugin.PLUGINNAME/lang/LANGID/
  71. $lang = lang_load('plugin:calendar');
  72. $widget = array();
  73. $widget['subject'] = $lang['plugin']['calendar']['subject'];
  74. $widget['content'] = '<ul id="widget_calendar"><li>'. generate_calendar($y,$m, $days).'</li></ul>';
  75. return $widget;
  76. }
  77. register_widget('calendar', 'Calendar', 'plugin_calendar_widget');