(* COCANWIKI - a wiki written in Objective CAML.
* Written by Richard W.M. Jones <rich@merjis.com>.
* Copyright (C) 2004 Merjis Ltd.
- * $Id: edit.ml,v 1.11 2004/09/23 11:56:47 rich Exp $
+ * $Id: edit.ml,v 1.12 2004/09/24 15:53:57 rich Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
let template = get_template dbh hostid "edit.html" in
let template_conflict = get_template dbh hostid "edit_conflict.html" in
+ let template_email = get_template dbh hostid "edit_page_email.txt" in
(* Workaround bugs in IE, specifically lack of support for <button>
* elements.
*)
let sth =
dbh#prepare_cached
- "set constraints pages_redirect_cn, sitemenu_url_cn deferred" in
+ "set constraints pages_redirect_cn, sitemenu_url_cn,
+ page_emails_url_cn deferred" in
sth#execute [];
(* Mark the old page as deleted. NB. There is a small race
email_notify ~body ~subject dbh hostid;
+ (* General email notification of page edits. Send an email to
+ * anyone in the page_emails table who has a confirmed address
+ * and who hasn't received an email already today.
+ *)
+ let sth = dbh#prepare_cached "select email, opt_out from page_emails
+ where hostid = ? and url = ?
+ and pending is null
+ and last_sent < current_date" in
+ sth#execute [`Int hostid; `String url];
+
+ let addrs = sth#map (function [`String email; `String opt_out] ->
+ email, opt_out
+ | _ -> assert false) in
+
+ if addrs <> [] then (
+ (* Construct the email. *)
+ template_email#set "hostname" hostname;
+ template_email#set "page" url;
+
+ let subject =
+ "Site notice: " ^ hostname ^ "/" ^ url ^ " has been updated" in
+
+ (* Send each email individually (they all have different opt out
+ * links).
+ *)
+ List.iter (fun (to_addr, opt_out) ->
+ template_email#set "opt_out" opt_out;
+ let body = template_email#to_string in
+ Sendmail.send_mail ~subject ~to_addr:[to_addr] ~body ())
+ addrs
+ );
+
+ (* Update the database to record when these emails were sent. *)
+ let sth = dbh#prepare_cached "update page_emails
+ set last_sent = current_date
+ where hostid = ? and url = ?
+ and pending is null" in
+ sth#execute [`Int hostid; `String url];
+
+ dbh#commit ();
+
let buttons = [ ok_button ("/" ^ url) ] in
ok ~title:"Saved" ~buttons
q "The page was saved."