changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / module / calendar.php
1 <?php
2 /**
3 * this plugin is in european date format only at the moment.
4 *
5 * returns a calender for the current / every page,
6 * example:
7 *
8 *       <!php
9 *               #-- plugin, CSS
10 *               include("plugins/calendar.php");
11 *               include("fragments/calendar.css");
12 *
13 *               #-- show page
14 *               echo ewiki_page();
15 *
16 *               #-- show calendar
17 *               if (calendar_exists()) {
18 *                       echo calendar();
19 *               }
20 *       !>
21 *
22 * this plugin was
23 * contributed_by("Carsten Senf <ewiki@csenf.de>");
24 * and en_ localization was added by Andy Fundinger Andy@burgiss.com
25 *
26 */ 
27
28 $ewiki_t["en"]["MONTHS"] = "January February March April May June July August September October November December";
29 $ewiki_t["de"]["MONTHS"] = "Januar Februar März April Mai Juni Juli August September Oktober November Dezember";
30 $ewiki_t["en"]["DAYABBRVS"] = "Mo Tu We Th Fr Sa Su";
31 $ewiki_t["de"]["DAYABBRVS"] = "Mo Di Mi Do Fr Sa So";
32 $ewiki_t["en"]["CALENDARFOR"] = "Calendar for";         
33 $ewiki_t["de"]["CALENDARFOR"] = "Kalender für";
34
35 define("EWIKI_ACTION_CALENDAR", "calendar");
36 define("EWIKI_PAGE_CALENDAR", "PageCalendar");
37 define("EWIKI_PAGE_YEAR_CALENDAR", "PageYearCalendar");
38 define("EWIKI_CALENDAR_WIDTH", 4);
39 define("EWIKI_NAME_SEP", "_");
40 define("CALENDAR_NAME_SEP", EWIKI_NAME_SEP);
41 define("CALENDAR_PAGE_TITLE_REGEX","/^(.*?)".preg_quote(CALENDAR_NAME_SEP) ."\d{8}$/");
42
43 $ewiki_plugins["page"][EWIKI_PAGE_CALENDAR] = "ewiki_page_calendar";
44 $ewiki_plugins["page"][EWIKI_PAGE_YEAR_CALENDAR] = "ewiki_page_year_calendar";
45 $ewiki_plugins["action"][EWIKI_ACTION_CALENDAR] = "ewiki_page_calendar";
46 $ewiki_config["action_links"]["view"][EWIKI_ACTION_CALENDAR] = EWIKI_PAGE_CALENDAR;
47
48 function calendar() {
49         ($id = $GLOBALS["ewiki_id"]) or ($id = EWIKI_PAGE_CALENDAR);
50         return(ewiki_page_calendar($id, array("id"=>$id)));
51 }
52
53
54 function calendar_exists($always=false) {
55         if ($always) { return(true); }
56
57         $result=($id = $GLOBALS["ewiki_id"])
58         && ($Qresult = ewiki_db::SEARCH("id", $id.CALENDAR_NAME_SEP))
59         && ($Qresult->count());
60
61         if (!$result || ($id==EWIKI_PAGE_CALENDAR) || ($id==EWIKI_PAGE_YEAR_CALENDAR) || (!empty($_REQUEST["year"]))) {
62                 return(false);
63         }
64         while ($row = $Qresult->get(0, 0x1037)) {
65                 if (ewiki_isCalendarId($row["id"])) {
66                         return(true);
67                 }
68         }
69         return(false);
70 }
71
72
73 function ewiki_page_calendar($id, $data=0) {
74
75         if ($_REQUEST["year"]) {
76                 return(ewiki_page_year_calendar($id, $data));
77         }
78         else {
79                 return(renderCalendar($id, TRUE));
80         }
81 }
82
83
84 function ewiki_page_year_calendar($id, $data=0) {
85
86         ($year = $_REQUEST['year']) or ($year = date("Y"));
87         ($pgname = $_REQUEST['pgname']) or ($pgname = $id);
88
89         $prev = $year-1;
90         $next = $year+1;
91                 
92         $html = '<h2>'.ewiki_t("CALENDARFOR").' <a href="'.ewiki_script("",$pgname).'">'.$pgname."</a> - ".$year.'</h2><center><table cellpadding=\"10\">'."\n";
93
94         for($i=1; $i<12; $i+=EWIKI_CALENDAR_WIDTH) {
95                 $html .= "<tr>\n";
96                 for($month=$i; $month<$i+EWIKI_CALENDAR_WIDTH; $month++) {
97                         $html .= "<td valign=\"top\">\n" . RenderCalendar($pgname, FALSE, $year, $month) . "\n</td>\n";
98                 }
99                 $html .= "</tr>\n";
100         }
101         
102         $html .= "<tr>
103                 <td align=\"left\" valign=\"bottom\">".
104                 '<a href="'.ewiki_script(EWIKI_ACTION_CALENDAR,$pgname,'year='.$prev).'">'.
105                 "&lt; $prev</a>
106                 </td>
107                 <td align=\"center\" colspan=\"".(EWIKI_CALENDAR_WIDTH-2)."\"></td>".
108                 "<td align=\"right\" valign=\"bottom\">".
109                 '<a href="'.ewiki_script(EWIKI_ACTION_CALENDAR,$pgname,'year='.$next).'">'.
110                 "$next &gt;</a>
111                 </td>
112                 ";
113         $html .= "</table></center>";
114         return $html;
115 }
116
117
118
119
120 function renderCalendar($pgname, $more, $year = NULL, $month = NULL) {  
121
122         if (preg_match(CALENDAR_PAGE_TITLE_REGEX, $pgname, $match)) {
123                 $pgname = $match[1];
124         }
125
126             $month_names = explode(" ", ewiki_t("MONTHS"));
127         $day_names = explode(" ", ewiki_t("DAYABBRVS"));
128         
129         if (!isset($year)) {
130                 $year = date("Y");
131         }
132         if (!isset($month)) {
133                 $month = date("n");
134         }
135         
136         $shift = 0;
137         $today_ts = mktime(0,0,0,$month,date("d"),$year); // non relative date
138         $firstday_month_ts = mktime(0,0,0,$month,1,$year); // first day of the month
139         $lastday_month_ts = mktime(0,0,0,$month+1,0,$year);    // last day of the month
140         
141         $numYear = date("Y",$firstday_month_ts);
142         $numMonth = date("m",$firstday_month_ts);
143         $textMonth = $month_names[(date("m", $firstday_month_ts))-1];
144         $daysInMonth = date("t",$firstday_month_ts);
145         
146         $dayMonth_start = date("w",$firstday_month_ts);
147         if ($dayMonth_start==0) { $dayMonth_start=7;} 
148         
149         
150         $dayMonth_end = date("w",$lastday_month_ts);
151         if ($dayMonth_end==0) { $dayMonth_end=7; }
152         
153         $ret = "";
154         
155         #-- start table, print month name as title
156         $ret .=  '<table class="calendar caltable" cellpadding="2" cellspacing="1">'."\n";
157         $ret .=  '<tr><th class="head calhead" colspan="7">';
158         $ret .= ($more ? '<a href="'.ewiki_script(EWIKI_ACTION_CALENDAR,$pgname,'year='.$year).'">' : "")
159                 . $textMonth."&nbsp;&nbsp;".$numYear
160                 . ($more ? "</a>" : "");
161         $ret .= "</th></tr>\n";
162
163         #-- output days
164         $ret .=  "<tr>\n";
165         for ($n=0; $n<7; $n++) {
166                 $ret .=  '<th class="days daynames caldays">' . $day_names[$n] . '</th>';
167         }       
168         $ret .= "</tr>\n";
169         
170         #-- not-existent days
171         $ret .=  "<tr>\n";
172         
173         for ($k=1; $k<$dayMonth_start; $k++) {
174                 $ret .=  "<td>&nbsp;</td>\n";
175         }
176
177         #-- pre-scan for calendar day pages
178         $f = array();
179         for ($i=1; $i<=$daysInMonth; $i++) {
180                 $f[] = $pgname.CALENDAR_NAME_SEP.$numYear.$numMonth.(strlen($i)<2 ? "0$i" : "$i");
181         }
182         $f = ewiki_db::FIND($f);
183         
184         for ($i=1; $i<=$daysInMonth; $i++) {
185                 $day_i_ts=mktime(0,0,0,date("n",$firstday_month_ts),$i,date("Y",$firstday_month_ts));
186                 $day_i = date("w",$day_i_ts);
187                 
188                 if ($day_i==0) { $day_i=7;}
189
190                 #-- calendar day page name
191                 $page = $pgname.CALENDAR_NAME_SEP.$numYear.$numMonth. (strlen($i)<2 ? "0$i" : "$i");
192
193                 #-- retrieve and check rights if running in protected mode
194                 if (EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING && $f[$page] && !ewiki_auth($page, $uu, "view", $ring=false, $force=0)) {
195                    unset($f[$page]);
196                 }   
197
198                 #-- link to calendar day page
199                 $link_i = '<a href="'.ewiki_script("",$page).'"'
200                         . ($f[$page]
201                                 ? ' class="found calpg"><b>' . $i . '</b>'
202                                 : ' class="hide calhide">' . $i )
203                         . "</a>";
204         
205                 #-- day table cell
206                 $day_class = (($month==date("n") && $today_ts==$day_i_ts)
207                         ? "today caltoday" : "day calday");
208                 $ret .= '<td class="' . $day_class . '">' . $link_i . "</td>";
209                 
210                 #-- close week-row                        
211                 if ($day_i==7 && $i<$daysInMonth) {
212                                 $ret .=  "</tr><tr>\n"; 
213                 } 
214                 else if ($day_i==7 && $i==$daysInMonth) {
215                                 $ret .=  "</tr>\n";
216                 }
217                 #-- add empty day cells to the end
218                 else if ($i==$daysInMonth) {
219                                 for ($h=$dayMonth_end; $h<7; $h++) { 
220                                 $ret .=  "<td>&nbsp;</td>\n";
221                                  }
222                         $ret .=  "</tr>\n";
223                 }
224         }
225         $ret .=  "</table>\n";
226
227         return($ret);
228 }
229
230
231 function ewiki_isCalendarId($id) {
232         return(preg_match(CALENDAR_PAGE_TITLE_REGEX, $id));
233 }
234
235
236 ?>