Serial columns now 64 bits.
[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.4 2005/11/17 10:14:42 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       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       q "You must give a subject line, which appears on contact emails.";
51     return ()
52   );
53
54   if emails = [] then (
55     error ~back_button:true ~title:"No email addresses"
56       q ("There are no email addresses listed for this contact form.  You " ^
57          "must list at least one valid email address.");
58     return ()
59   );
60
61   (* Update the database. *)
62   let sth = dbh#prepare_cached "insert into contacts (hostid, name, subject)
63                                 values (?, ?, ?)" in
64   sth#execute [`Int hostid; `String name; `String subject];
65
66   let contactid = Int64.to_int (sth#serial "contacts_id_seq") in
67
68   let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
69                                 values (?, ?)" in
70   List.iter (fun email ->
71                sth#execute [`Int contactid; `String email]) emails;
72
73   (* Finish off. *)
74   dbh#commit ();
75
76   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
77
78   let buttons = [ { Template.StdPages.label = "   View contact form   ";
79                     Template.StdPages.link = "/_bin/contact_show.cmo";
80                     Template.StdPages.method_ = None;
81                     Template.StdPages.params =
82                       [ "id", string_of_int contactid ] } ] in
83   ok ~title:"Contact form created" ~buttons q msg
84
85 let () =
86   register_script ~restrict:[CanManageContacts] run