2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: edit_page_css.ml,v 1.4 2004/09/08 09:54:28 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 "set constraints pages_redirect_cn deferred" in
47 let sth = dbh#prepare_cached "update pages set url_deleted = url,
49 where hostid = ? and id = ?" in
50 sth#execute [`Int hostid; `Int oldpageid];
52 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
53 description, creation_date, logged_ip,
55 values (?, ?, ?, ?, ?, ?, ?, ?)" in
56 sth#execute [`Int hostid; `String page; title; description;
57 creation_date; logged_ip; redirect; css ];
59 let pageid = sth#serial "pages_id_seq" in
61 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
62 sectionname, content, divname)
63 select ? as pageid, ordering, sectionname,
67 sth#execute [`Int pageid; `Int oldpageid];
71 (* Email notification. *)
72 let subject = "CSS for page " ^ page ^ " has been modified" in
75 get_diff dbh hostid page ~version:pageid ~old_version:oldpageid () in
76 "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
79 email_notify ~subject ~body dbh hostid;
81 let buttons = [ ok_button ("/" ^ page);
82 { StdPages.label = "Edit stylesheet again";
83 StdPages.link = "/_bin/edit_page_css_form.cmo";
84 StdPages.method_ = None;
85 StdPages.params = [ "page", page ] } ] in
86 ok ~title:"Stylesheet changed" ~buttons
87 q ("The stylesheet was changed successfully. " ^
88 "Note: You must RELOAD the page to see changes to stylesheets.")
91 register_script ~restrict:[CanEdit] run