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: edit_contact.ml,v 1.1 2004/09/21 18:24:15 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 id = int_of_string (q#param "id") in
36 let name = trim (q#param "name") in
37 let subject = trim (q#param "subject") in
39 let emails = try q#param "emails" with Not_found -> "" in
41 let emails = Pcre.split ~rex:split_re emails in
42 let emails = List.map trim emails in
43 let emails = List.filter ((<>) "") emails in
46 error ~back_button:true ~title:"Name field missing"
47 q "You must name your contact form.";
51 if subject = "" then (
52 error ~back_button:true ~title:"Subject line missing"
53 q "You must give a subject line, which appears on contact emails.";
58 error ~back_button:true ~title:"No email addresses"
59 q ("There are no email addresses listed for this contact form. You " ^
60 "must list at least one valid email address.");
64 (* Need to verify that the contact belongs to the host. *)
65 let sth = dbh#prepare_cached "select 1 from contacts
66 where hostid = ? and id = ?" in
67 sth#execute [`Int hostid; `Int id];
69 assert (sth#fetch1int () = 1);
71 (* Update the database. *)
72 let sth = dbh#prepare_cached "update contacts set name = ?, subject = ?
73 where hostid = ? and id = ?" in
74 sth#execute [`String name; `String subject; `Int hostid; `Int id];
77 dbh#prepare_cached "delete from contact_emails where contactid = ?" in
78 sth#execute [`Int id];
80 let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
82 List.iter (fun email ->
83 sth#execute [`Int id; `String email]) emails;
89 ok_button "/_bin/contacts.cmo";
90 { StdPages.label = " View contact form ";
91 StdPages.link = "/_bin/contact_show.cmo";
92 StdPages.method_ = None;
93 StdPages.params = [ "id", string_of_int id ] } ] in
94 ok ~title:"Contact form edited" ~buttons
95 q "The contact form was edited."
98 register_script ~restrict:[CanManageContacts] run