From b19eb2c96aef3bc2c0914376984dfb45fdab4f18 Mon Sep 17 00:00:00 2001 From: rich Date: Tue, 21 Sep 2004 18:24:15 +0000 Subject: [PATCH] Edit contact forms. --- MANIFEST | 3 ++ scripts/.depend | 4 ++ scripts/Makefile | 4 +- scripts/edit_contact.ml | 98 ++++++++++++++++++++++++++++++++++++++++ scripts/edit_contact_form.ml | 61 +++++++++++++++++++++++++ templates/edit_contact_form.html | 64 ++++++++++++++++++++++++++ 6 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 scripts/edit_contact.ml create mode 100644 scripts/edit_contact_form.ml create mode 100644 templates/edit_contact_form.html diff --git a/MANIFEST b/MANIFEST index 26b2b46..1d0591c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -80,6 +80,8 @@ scripts/delete_user.ml scripts/delete_user_form.ml scripts/diff.ml scripts/edit.ml +scripts/edit_contact.ml +scripts/edit_contact_form.ml scripts/edit_page_css.ml scripts/edit_page_css_form.ml scripts/edit_sitemenu.ml @@ -142,6 +144,7 @@ templates/delete_user_form.html templates/diff.html templates/edit.html templates/edit_conflict.html +templates/edit_contact_form.html templates/edit_page_css_form.html templates/edit_sitemenu.html templates/edit_user_form.html diff --git a/scripts/.depend b/scripts/.depend index ca05094..d32b9d8 100644 --- a/scripts/.depend +++ b/scripts/.depend @@ -60,6 +60,10 @@ edit.cmo: cocanwiki.cmo cocanwiki_diff.cmo cocanwiki_emailnotify.cmo \ cocanwiki_ok.cmo cocanwiki_strings.cmo cocanwiki_template.cmi edit.cmx: cocanwiki.cmx cocanwiki_diff.cmx cocanwiki_emailnotify.cmx \ cocanwiki_ok.cmx cocanwiki_strings.cmx cocanwiki_template.cmx +edit_contact.cmo: cocanwiki.cmo cocanwiki_ok.cmo cocanwiki_template.cmi +edit_contact.cmx: cocanwiki.cmx cocanwiki_ok.cmx cocanwiki_template.cmx +edit_contact_form.cmo: cocanwiki.cmo cocanwiki_template.cmi +edit_contact_form.cmx: cocanwiki.cmx cocanwiki_template.cmx edit_page_css.cmo: cocanwiki.cmo cocanwiki_diff.cmo cocanwiki_emailnotify.cmo \ cocanwiki_ok.cmo cocanwiki_strings.cmo edit_page_css.cmx: cocanwiki.cmx cocanwiki_diff.cmx cocanwiki_emailnotify.cmx \ diff --git a/scripts/Makefile b/scripts/Makefile index 0a0be74..fdeaedf 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -1,5 +1,5 @@ # Makefile for COCANWIKI. -# $Id: Makefile,v 1.17 2004/09/21 17:07:58 rich Exp $ +# $Id: Makefile,v 1.18 2004/09/21 18:24:15 rich Exp $ include ../Makefile.config @@ -41,6 +41,8 @@ OBJS := 00-TEMPLATE.cmo \ delete_user_form.cmo \ diff.cmo \ edit.cmo \ + edit_contact.cmo \ + edit_contact_form.cmo \ edit_page_css.cmo \ edit_page_css_form.cmo \ edit_sitemenu.cmo \ diff --git a/scripts/edit_contact.ml b/scripts/edit_contact.ml new file mode 100644 index 0000000..88077e6 --- /dev/null +++ b/scripts/edit_contact.ml @@ -0,0 +1,98 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_contact.ml,v 1.1 2004/09/21 18:24:15 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 id = int_of_string (q#param "id") in + + 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 + ); + + (* Need to verify that the contact belongs to the host. *) + let sth = dbh#prepare_cached "select 1 from contacts + where hostid = ? and id = ?" in + sth#execute [`Int hostid; `Int id]; + + assert (sth#fetch1int () = 1); + + (* Update the database. *) + let sth = dbh#prepare_cached "update contacts set name = ?, subject = ? + where hostid = ? and id = ?" in + sth#execute [`String name; `String subject; `Int hostid; `Int id]; + + let sth = + dbh#prepare_cached "delete from contact_emails where contactid = ?" in + sth#execute [`Int id]; + + let sth = dbh#prepare_cached "insert into contact_emails (contactid, email) + values (?, ?)" in + List.iter (fun email -> + sth#execute [`Int id; `String email]) emails; + + (* Finish off. *) + dbh#commit (); + + let buttons = [ + ok_button "/_bin/contacts.cmo"; + { StdPages.label = " View contact form "; + StdPages.link = "/_bin/contact_show.cmo"; + StdPages.method_ = None; + StdPages.params = [ "id", string_of_int id ] } ] in + ok ~title:"Contact form edited" ~buttons + q "The contact form was edited." + +let () = + register_script ~restrict:[CanManageContacts] run diff --git a/scripts/edit_contact_form.ml b/scripts/edit_contact_form.ml new file mode 100644 index 0000000..bc888eb --- /dev/null +++ b/scripts/edit_contact_form.ml @@ -0,0 +1,61 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_contact_form.ml,v 1.1 2004/09/21 18:24:15 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_template + +let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ = + let template = get_template dbh hostid "edit_contact_form.html" in + + let id = int_of_string (q#param "id") in + template#set "id" (string_of_int id); + + (* Get details from the database. *) + let sth = dbh#prepare_cached "select name, subject from contacts + where hostid = ? and id = ?" in + sth#execute [`Int hostid; `Int id]; + + let name, subject = + match sth#fetch1 () with + [ `String name; `String subject ] -> name, subject + | _ -> assert false in + + template#set "name" name; + template#set "subject" subject; + + let sth = dbh#prepare_cached "select email from contact_emails + where contactid = ? order by 1" in + sth#execute [`Int id]; + + let table = sth#map (function [`String email] -> + [ "email", Template.VarString email ] + | _ -> assert false) in + template#table "emails" table; + + q#template template + +let () = + register_script ~restrict:[CanManageContacts] run diff --git a/templates/edit_contact_form.html b/templates/edit_contact_form.html new file mode 100644 index 0000000..4be865d --- /dev/null +++ b/templates/edit_contact_form.html @@ -0,0 +1,64 @@ + + + +Edit contact: ::name_html:: + + + + + +

Edit contact: ::name_html::

+ +
+ + + + + + + + + + + + + + + + + + + + + +
Form name:
Subject line for emails:
Email addresses: +
+
+ + + + + + + + + \ No newline at end of file -- 1.8.3.1