(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: create_contact.ml,v 1.6 2006/03/27 18:09:46 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 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" dbh hostid q "You must name your contact form."; return () ); if subject = "" then ( error ~back_button:true ~title:"Subject line missing" dbh hostid q "You must give a subject line, which appears on contact emails."; return () ); if emails = [] then ( error ~back_button:true ~title:"No email addresses" dbh hostid q ("There are no email addresses listed for this contact form. You " ^ "must list at least one valid email address."); return () ); (* Update the database. *) PGSQL(dbh) "insert into contacts (hostid, name, subject) values ($hostid, $name, $subject)"; let contactid = PGOCaml.serial4 dbh "contacts_id_seq" in List.iter ( fun email -> PGSQL(dbh) "insert into contact_emails (contactid, email) values ($contactid, $email)" ) emails; (* Finish off. *) PGOCaml.commit dbh; let msg = sprintf "Contact form created. The contact id is %ld. On the next page you will be given some same 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 = [ { Template.StdPages.label = " View contact form "; Template.StdPages.link = "/_bin/contact_show.cmo"; Template.StdPages.method_ = None; Template.StdPages.params = [ "id", Int32.to_string contactid ] } ] in ok ~title:"Contact form created" ~buttons dbh hostid q msg let () = register_script ~restrict:[CanManageContacts] run