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: edit_contact.ml,v 1.6 2006/12/06 09:46:57 rich Exp $
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.
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.
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.
29 open Cocanwiki_strings
31 let split_re = Pcre.regexp "[\\r\\n,;]+"
33 let run r (q : cgi) dbh hostid _ _ =
34 let id = Int32.of_string (q#param "id") in
36 let name = trim (q#param "name") in
37 let subject = trim (q#param "subject") in
39 let emails = try q#param "emails" with Not_found -> "" in
41 let emails = Pcre.split ~rex:split_re emails in
42 let emails = List.map trim emails in
43 let emails = List.filter ((<>) "") emails in
46 error ~back_button:true ~title:"Name field missing"
47 r dbh hostid q "You must name your contact form.";
51 if subject = "" then (
52 error ~back_button:true ~title:"Subject line missing"
54 "You must give a subject line, which appears on contact emails.";
59 error ~back_button:true ~title:"No email addresses"
61 ("There are no email addresses listed for this contact form. You " ^
62 "must list at least one valid email address.");
66 (* Need to verify that the contact belongs to the host. *)
67 let rows = PGSQL(dbh) "select 1 from contacts
68 where hostid = $hostid and id = $id" in
69 assert (rows = [ Some 1l ]);
71 (* Update the database. *)
72 PGSQL(dbh) "update contacts set name = $name, subject = $subject
73 where hostid = $hostid and id = $id";
74 PGSQL(dbh) "delete from contact_emails where contactid = $id";
78 PGSQL(dbh) "insert into contact_emails (contactid, email)
86 ok_button "/_bin/contacts.cmo";
87 { Template.StdPages.label = " View contact form ";
88 Template.StdPages.link = "/_bin/contact_show.cmo";
89 Template.StdPages.method_ = None;
90 Template.StdPages.params = [ "id", Int32.to_string id ] } ] in
91 ok ~title:"Contact form edited" ~buttons
92 r dbh hostid q "The contact form was edited."
95 register_script ~restrict:[CanManageContacts] run