About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / create_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: create_contact.ml,v 1.5 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 name = trim (q#param "name") in
35   let subject = trim (q#param "subject") in
36   let emails = try q#param "emails" with Not_found -> "" in
37
38   let emails = Pcre.split ~rex:split_re emails in
39   let emails = List.map trim emails in
40   let emails = List.filter ((<>) "") emails in
41
42   if name = "" then (
43     error ~back_button:true ~title:"Name field missing"
44       dbh hostid q "You must name your contact form.";
45     return ()
46   );
47
48   if subject = "" then (
49     error ~back_button:true ~title:"Subject line missing"
50       dbh hostid q
51       "You must give a subject line, which appears on contact emails.";
52     return ()
53   );
54
55   if emails = [] then (
56     error ~back_button:true ~title:"No email addresses"
57       dbh hostid q
58       ("There are no email addresses listed for this contact form.  You " ^
59        "must list at least one valid email address.");
60     return ()
61   );
62
63   (* Update the database. *)
64   let sth = dbh#prepare_cached "insert into contacts (hostid, name, subject)
65                                 values (?, ?, ?)" in
66   sth#execute [`Int hostid; `String name; `String subject];
67
68   let contactid = Int64.to_int (sth#serial "contacts_id_seq") in
69
70   let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
71                                 values (?, ?)" in
72   List.iter (fun email ->
73                sth#execute [`Int contactid; `String email]) emails;
74
75   (* Finish off. *)
76   dbh#commit ();
77
78   let msg = sprintf "Contact form created.  The contact id is %d.  On the next page you will be given some same <html> code which you should copy and paste onto a web page to create a simple form, which can then be modified for your requirements." contactid in
79
80   let buttons = [ { Template.StdPages.label = "   View contact form   ";
81                     Template.StdPages.link = "/_bin/contact_show.cmo";
82                     Template.StdPages.method_ = None;
83                     Template.StdPages.params =
84                       [ "id", string_of_int contactid ] } ] in
85   ok ~title:"Contact form created" ~buttons dbh hostid q msg
86
87 let () =
88   register_script ~restrict:[CanManageContacts] run