+(* COCANWIKI - a wiki written in Objective CAML.
+ * Written by Richard W.M. Jones <rich@merjis.com>.
+ * Copyright (C) 2004 Merjis Ltd.
+ * $Id: create_contact.ml,v 1.1 2004/09/21 15:55:48 rich Exp $
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *)
+
+open Apache
+open Registry
+open Cgi
+open Printf
+
+open Cocanwiki
+open Cocanwiki_ok
+open Cocanwiki_strings
+
+let split_re = Pcre.regexp "[\\r\\n,;]+"
+
+let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
+ let name = trim (q#param "name") in
+ let subject = trim (q#param "subject") in
+ let emails = try q#param "emails" with Not_found -> "" in
+
+ let emails = Pcre.split ~rex:split_re emails in
+ let emails = List.map trim emails in
+ let emails = List.filter ((<>) "") emails in
+
+ if name = "" then (
+ error ~back_button:true ~title:"Name field missing"
+ q "You must name your contact form.";
+ raise CgiExit
+ );
+
+ if subject = "" then (
+ error ~back_button:true ~title:"Subject line missing"
+ q "You must give a subject line, which appears on contact emails.";
+ raise CgiExit
+ );
+
+ if emails = [] then (
+ error ~back_button:true ~title:"No email addresses"
+ q ("There are no email addresses listed for this contact form. You " ^
+ "must list at least one valid email address.");
+ raise CgiExit
+ );
+
+ (* Update the database. *)
+ let sth = dbh#prepare_cached "insert into contacts (hostid, name, subject)
+ values (?, ?, ?)" in
+ sth#execute [`Int hostid; `String name; `String subject];
+
+ let contactid = sth#serial "contacts_id_seq" in
+
+ let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
+ values (?, ?)" in
+ List.iter (fun email ->
+ sth#execute [`Int contactid; `String email]) emails;
+
+ (* Finish off. *)
+ dbh#commit ();
+
+ 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
+
+ let buttons = [ { StdPages.label = " View contact form ";
+ StdPages.link = "/_bin/contact_show.cmo";
+ StdPages.method_ = None;
+ StdPages.params = [ "id", string_of_int contactid ] } ] in
+ ok ~title:"Contact form created" ~buttons q msg
+
+let () =
+ register_script ~restrict:[CanManageContacts] run