About half way through switching cocanwiki to using the new PG interface.
[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.4 2005/11/24 14:54:11 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 : Dbi.connection) hostid _ _ =
34   let id = int_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 sth = dbh#prepare_cached "select 1 from contacts
68                                  where hostid = ? and id = ?" in
69   sth#execute [`Int hostid; `Int id];
70
71   assert (sth#fetch1int () = 1);
72
73   (* Update the database. *)
74   let sth = dbh#prepare_cached "update contacts set name = ?, subject = ?
75                                  where hostid = ? and id = ?" in
76   sth#execute [`String name; `String subject; `Int hostid; `Int id];
77
78   let sth =
79     dbh#prepare_cached "delete from contact_emails where contactid = ?" in
80   sth#execute [`Int id];
81
82   let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
83                                 values (?, ?)" in
84   List.iter (fun email ->
85                sth#execute [`Int id; `String email]) emails;
86
87   (* Finish off. *)
88   dbh#commit ();
89
90   let buttons = [
91     ok_button "/_bin/contacts.cmo";
92     { Template.StdPages.label = "   View contact form   ";
93       Template.StdPages.link = "/_bin/contact_show.cmo";
94       Template.StdPages.method_ = None;
95       Template.StdPages.params = [ "id", string_of_int id ] } ] in
96   ok ~title:"Contact form edited" ~buttons
97     dbh hostid q "The contact form was edited."
98
99 let () =
100   register_script ~restrict:[CanManageContacts] run