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.2 2004/09/23 11:56:47 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 : 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
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
43 error ~back_button:true ~title:"Name field missing"
44 q "You must name your contact form.";
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.";
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.");
61 (* Update the database. *)
62 let sth = dbh#prepare_cached "insert into contacts (hostid, name, subject)
64 sth#execute [`Int hostid; `String name; `String subject];
66 let contactid = sth#serial "contacts_id_seq" in
68 let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
70 List.iter (fun email ->
71 sth#execute [`Int contactid; `String email]) emails;
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
78 let buttons = [ { StdPages.label = " View contact form ";
79 StdPages.link = "/_bin/contact_show.cmo";
80 StdPages.method_ = None;
81 StdPages.params = [ "id", string_of_int contactid ] } ] in
82 ok ~title:"Contact form created" ~buttons q msg
85 register_script ~restrict:[CanManageContacts] run