2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: edit_page_css.ml,v 1.5 2004/09/08 13:09:05 rich Exp $
15 open Cocanwiki_emailnotify
16 open Cocanwiki_strings
18 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
19 let page = q#param "page" in
20 let css = q#param "css" in
22 let css = if string_is_whitespace css then `Null else `String css in
24 (* Get the IP address of the user, if available. *)
26 try `String (Connection.remote_ip (Request.connection r))
27 with Not_found -> `Null in
29 (* Changing the CSS creates a new version of the page. This enables
30 * us to revert changes to the CSS easily.
32 let sth = dbh#prepare_cached "select id, title, description, creation_date,
35 where hostid = ? and url = ?" in
36 sth#execute [`Int hostid; `String page];
38 let oldpageid, title, description, creation_date, redirect =
39 match sth#fetch1 () with
40 [ `Int id; title; description; creation_date; redirect ] ->
41 id, title, description, creation_date, redirect
42 | _ -> assert false in
44 let sth = dbh#prepare_cached
45 "set constraints pages_redirect_cn, sitemenu_url_cn deferred" in
48 let sth = dbh#prepare_cached "update pages set url_deleted = url,
50 where hostid = ? and id = ?" in
51 sth#execute [`Int hostid; `Int oldpageid];
53 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
54 description, creation_date, logged_ip,
56 values (?, ?, ?, ?, ?, ?, ?, ?)" in
57 sth#execute [`Int hostid; `String page; title; description;
58 creation_date; logged_ip; redirect; css ];
60 let pageid = sth#serial "pages_id_seq" in
62 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
63 sectionname, content, divname)
64 select ? as pageid, ordering, sectionname,
68 sth#execute [`Int pageid; `Int oldpageid];
72 (* Email notification. *)
73 let subject = "CSS for page " ^ page ^ " has been modified" in
76 get_diff dbh hostid page ~version:pageid ~old_version:oldpageid () in
77 "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
80 email_notify ~subject ~body dbh hostid;
82 let buttons = [ ok_button ("/" ^ page);
83 { StdPages.label = "Edit stylesheet again";
84 StdPages.link = "/_bin/edit_page_css_form.cmo";
85 StdPages.method_ = None;
86 StdPages.params = [ "page", page ] } ] in
87 ok ~title:"Stylesheet changed" ~buttons
88 q ("The stylesheet was changed successfully. " ^
89 "Note: You must RELOAD the page to see changes to stylesheets.")
92 register_script ~restrict:[CanEdit] run