Fixed some problems found in testing. Now appears to be working fully.
[cocanwiki.git] / scripts / edit_contact.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: edit_contact.ml,v 1.5 2006/03/27 18:09:46 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 Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_strings
30
31 let split_re = Pcre.regexp "[\\r\\n,;]+"
32
33 let run r (q : cgi) dbh hostid _ _ =
34   let id = Int32.of_string (q#param "id") in
35
36   let name = trim (q#param "name") in
37   let subject = trim (q#param "subject") in
38
39   let emails = try q#param "emails" with Not_found -> "" in
40
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
44
45   if name = "" then (
46     error ~back_button:true ~title:"Name field missing"
47       dbh hostid q "You must name your contact form.";
48     return ()
49   );
50
51   if subject = "" then (
52     error ~back_button:true ~title:"Subject line missing"
53       dbh hostid q
54       "You must give a subject line, which appears on contact emails.";
55     return ()
56   );
57
58   if emails = [] then (
59     error ~back_button:true ~title:"No email addresses"
60       dbh hostid q
61       ("There are no email addresses listed for this contact form.  You " ^
62        "must list at least one valid email address.");
63     return ()
64   );
65
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 ]);
70
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";
75
76   List.iter (
77     fun email ->
78       PGSQL(dbh) "insert into contact_emails (contactid, email)
79                   values ($id, $email)"
80   ) emails;
81
82   (* Finish off. *)
83   PGOCaml.commit dbh;
84
85   let buttons = [
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     dbh hostid q "The contact form was edited."
93
94 let () =
95   register_script ~restrict:[CanManageContacts] run