Added styling to some pages which previously were "outside" the site
[cocanwiki.git] / scripts / lib / cocanwiki_ext_calendar.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: cocanwiki_ext_calendar.ml,v 1.5 2006/07/27 16:46:55 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open ExtList
28
29 open Cocanwiki
30 open Cocanwiki_extensions
31 open Cocanwiki_template
32 open Cocanwiki_strings
33 open Cocanwiki_date
34
35 let day_template = _get_template "calendar_day.html"
36 let month_template = _get_template "calendar_month.html"
37 let year_template = _get_template "calendar_year.html"
38 let year_1m_template = _get_template "calendar_year_1m.html"
39
40 (* Check a date is valid. *)
41 let check_date (y, m, d) =
42   try
43     let t = Date.make y m d in
44     Date.year t = y
45         && Date.int_of_month (Date.month t) = m
46         && Date.day_of_month t = d
47   with
48       Date.Out_of_bounds | Date.Undefined -> false
49
50 let rec range a b =
51   if a <= b then
52     a :: range (a+1) b
53   else
54     []
55
56 let extension r dbh hostid url =
57   (* Validate a date in the form "yyyy[/mm[/dd]]".  Returns a (yyyy, mm, dd)
58    * tuple with missing fields set to 0.  If the string doesn't parse or the
59    * date isn't valid, then raises Not_found.
60    *)
61   let valid_date str =
62     if String.length str = 4 &&
63       isdigit str.[0] && isdigit str.[1] &&
64       isdigit str.[2] && isdigit str.[3] then (
65         let yyyy = int_of_string (String.sub str 0 4) in
66         (yyyy, 0, 0)
67       )
68     else if String.length str = 7 &&
69       isdigit str.[0] && isdigit str.[1] &&
70       isdigit str.[2] && isdigit str.[3] &&
71       str.[4] = '/' &&
72       isdigit str.[5] && isdigit str.[6] then (
73         let yyyy = int_of_string (String.sub str 0 4) in
74         let mm = int_of_string (String.sub str 5 2) in
75         if mm >= 1 && mm <= 12 then (yyyy, mm, 0) else raise Not_found
76       )
77     else if String.length str = 10 &&
78       isdigit str.[0] && isdigit str.[1] &&
79       isdigit str.[2] && isdigit str.[3] &&
80       str.[4] = '/' &&
81       isdigit str.[5] && isdigit str.[6] &&
82       str.[7] = '/' &&
83       isdigit str.[8] && isdigit str.[9] then (
84         let yyyy = int_of_string (String.sub str 0 4) in
85         let mm = int_of_string (String.sub str 5 2) in
86         let dd = int_of_string (String.sub str 8 2) in
87         let date = (yyyy, mm, dd) in
88         if check_date date then date else raise Not_found
89       )
90     else
91       raise Not_found
92   in
93
94   (* From the links table, find all links to this page, or sub-calendar pages.
95    * This query overselects.  We then filter the real pages in OCaml.
96    *)
97   let rows =
98     let patt = url ^ "%" in
99     PGSQL(dbh)
100       "select li.from_url, p.title, li.to_url
101          from links li, pages p
102         where li.hostid = $hostid and li.to_url like $patt
103           and li.hostid = p.hostid and li.from_url = p.url" in
104
105   let pages =
106     let results =
107       List.map (fun (from_url, title, to_url) ->
108                   from_url, title, to_url) rows in
109     List.filter_map
110       (fun (from_url, title, to_url) ->
111          try let date = valid_date to_url in Some (date, (title, from_url))
112          with Not_found -> None) results in
113   let pages = List.sort pages in
114
115   (* Validate the date in the URL itself. *)
116   let date =
117     try Some (valid_date url)
118     with Not_found -> None in
119
120   match date with
121     | None ->                           (* Not a valid date. *)
122         "<p>" ^ url ^ " is not an actual date.</p>"
123     | Some (yyyy, 0, 0) ->              (* Year view. *)
124         let template = year_template in
125
126         template#set "yyyy" (string_of_int yyyy);
127         template#set "prev_yyyy" (string_of_int (yyyy - 1));
128         template#set "next_yyyy" (string_of_int (yyyy + 1));
129
130         (* Return true if there are any events on a particular day. *)
131         let has_events date = List.exists (fun (d, _) -> date = d) pages in
132
133         let int_of_day_of_week = function
134           | Date.Sun -> 0 | Date.Mon -> 1 | Date.Tue -> 2
135           | Date.Wed -> 3 | Date.Thu -> 4 | Date.Fri -> 5
136           | Date.Sat -> 6
137         in
138
139         (* Generate each month template separately ...
140          * Wow, finally found a place I can use a for loop.
141          *)
142         for mm = 1 to 12 do
143           let str =
144             let template = year_1m_template in
145             template#set "yyyy" (string_of_int yyyy);
146             template#set "mm" (sprintf "%02d" mm);
147             template#set "month_name"
148               (!Printer.month_name (Date.month_of_int mm));
149             let dow = Date.day_of_week (Date.make yyyy mm 1) in
150             let max_dd = Date.days_in_month (Date.make yyyy mm 1) in
151             let dd = ref (1 - int_of_day_of_week dow) in
152             let rows = ref [] in
153             for r = 0 to 5 do           (* up to 5 rows ... *)
154               let cols = ref [] in
155               for c = 0 to 6 do         (* 7 columns, Sunday - Saturday *)
156                 let is_day = !dd >= 1 && !dd <= max_dd in
157                 let clasz =
158                   if is_day then (
159                     let is_weekend =
160                       match Date.day_of_week (Date.make yyyy mm !dd) with
161                           Date.Sat | Date.Sun -> true | _ -> false in
162                     let date = yyyy, mm, !dd in
163                     let events = has_events date in
164                     (if is_weekend then "cal_year_1m_weekend " else "") ^
165                     (if events then "cal_year_1m_events" else "")
166                   ) else
167                     "cal_year_1m_empty" in
168                 let col =
169                   [ "is_day", Template.VarConditional is_day;
170                     "dd", Template.VarString (sprintf "%02d" !dd);
171                     "class", Template.VarString clasz ] in
172                 cols := col :: !cols;
173                 incr dd
174               done;
175               rows := [ "cols", Template.VarTable (List.rev !cols) ] :: !rows;
176               cols := []
177             done;
178
179             template#table "rows" (List.rev !rows);
180
181             template#to_string in
182           template#set ("month" ^ string_of_int mm) str
183         done;
184
185         (* Annual events. *)
186         let events =
187           List.filter (function ((_, 0, 0), _) -> true | _ -> false) pages in
188
189         let table =
190           List.map (fun (_, (title, page)) ->
191                       [ "title", Template.VarString title;
192                         "page", Template.VarString page ]) events in
193         template#table "events" table;
194
195         template#to_string
196
197     | Some (yyyy, mm, 0) ->             (* Month view. *)
198         let template = month_template in
199
200         template#set "month_name" (!Printer.month_name (Date.month_of_int mm));
201         template#set "yyyy" (string_of_int yyyy);
202         template#set "mm" (sprintf "%02d" mm);
203
204         let prev_yyyy, prev_mm =
205           if mm = 1 then yyyy - 1, 12
206           else yyyy, mm - 1 in
207         let next_yyyy, next_mm =
208           if mm = 12 then yyyy + 1, 1
209           else yyyy, mm + 1 in
210         template#set "prev_yyyy" (string_of_int prev_yyyy);
211         template#set "prev_mm" (sprintf "%02d" prev_mm);
212         template#set "next_yyyy" (string_of_int next_yyyy);
213         template#set "next_mm" (sprintf "%02d" next_mm);
214
215         (* Get all monthly events and all daily events. *)
216         let monthly_events, daily_events =
217           List.partition (function ((_, _, 0), _) -> true | _ -> false)
218             pages in
219
220         (* Table of monthly events. *)
221         let table =
222           List.map (fun (_, (title, page)) ->
223                       [ "title", Template.VarString title;
224                         "page", Template.VarString page ]) monthly_events in
225         template#table "monthly_events" table;
226
227         (* How many days in this month? *)
228         let max_dd = Date.days_in_month (Date.make yyyy mm 1) in
229         let days = range 1 max_dd in
230
231         let table =
232           List.map (fun dd ->
233                       let events =
234                         List.filter (fun ((_, _, d), _) -> d = dd)
235                           daily_events in
236                       let table =
237                         List.map (fun (_, (title, page)) ->
238                                     [ "title", Template.VarString title;
239                                       "page", Template.VarString page ])
240                           events in
241                       let is_weekend =
242                         match Date.day_of_week (Date.make yyyy mm dd) with
243                             Date.Sat | Date.Sun -> true | _ -> false in
244                       [ "dd", Template.VarString (sprintf "%02d" dd);
245                         "is_weekend", Template.VarConditional is_weekend;
246                         "events", Template.VarTable table ])
247             days in
248         template#table "days" table;
249
250         template#to_string
251
252     | Some (yyyy, mm, dd) ->            (* Single day view. *)
253         let template = day_template in
254
255         (* XXX This will change once we start doing date and time events.
256          * For now it is very simple indeed.
257          *)
258         template#set "yyyy" (string_of_int yyyy);
259         template#set "mm" (sprintf "%02d" mm);
260         template#set "dd" (sprintf "%02d" dd);
261
262         template#set "month_name" (!Printer.month_name (Date.month_of_int mm));
263
264         let t = Date.make yyyy mm dd in
265         let dow = Date.day_of_week t in
266         template#set "short_weekday" (Printer.short_name_of_day dow);
267
268         let oneday = Date.Period.day 1 in
269         let prev_t = Date.rem t oneday in
270         let next_t = Date.add t oneday in
271         let prev_yyyy, prev_mm, prev_dd =
272           Date.year prev_t, Date.int_of_month (Date.month prev_t),
273           Date.day_of_month prev_t in
274         let next_yyyy, next_mm, next_dd =
275           Date.year next_t, Date.int_of_month (Date.month next_t),
276           Date.day_of_month next_t in
277
278         template#set "prev_yyyy" (string_of_int prev_yyyy);
279         template#set "prev_mm" (sprintf "%02d" prev_mm);
280         template#set "prev_dd" (sprintf "%02d" prev_dd);
281         template#set "next_yyyy" (string_of_int next_yyyy);
282         template#set "next_mm" (sprintf "%02d" next_mm);
283         template#set "next_dd" (sprintf "%02d" next_dd);
284
285         let table = List.map (fun (_, (title, page)) ->
286                                 [ "title", Template.VarString title;
287                                   "page", Template.VarString page ]) pages in
288         template#table "events" table;
289
290         template#to_string
291
292 (* Register the extension. *)
293 let () =
294   extensions := ("calendar", extension) :: !extensions