+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / edit_host_css_form.ml
index 7a673c6..c9e68a8 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: edit_host_css_form.ml,v 1.1 2004/09/22 11:41:03 rich Exp $
+ * $Id: edit_host_css_form.ml,v 1.5 2006/12/06 09:46:57 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
@@ -27,21 +27,41 @@ open Printf
 open Cocanwiki
 open Cocanwiki_template
 
-let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
-  let template = get_template dbh hostid "edit_host_css_form.html" in
+type stylesheet = Global | Ie6_fixes | Ie7_fixes
+let string_of_stylesheet = function
+  | Global -> "global"
+  | Ie6_fixes -> "ie6_fixes"
+  | Ie7_fixes -> "ie7_fixes"
+let stylesheet_of_string = function
+  | "global" -> Global
+  | "ie6_fixes" -> Ie6_fixes
+  | "ie7_fixes" -> Ie7_fixes
+  | str -> failwith ("stylesheet_of_string: unknown stylesheet: " ^ str)
 
-  let sth = dbh#prepare_cached "select css from hosts where id = ?" in
-  sth#execute [`Int hostid];
+let run r (q : cgi) dbh hostid _ _ =
+  let template = get_template r dbh hostid "edit_host_css_form.html" in
 
-  let css =
-    match sth#fetch1 () with
-      | [ `Null ] -> ""
-      | [ `String css ] -> css
-      | _ -> assert false in
+  (* Which stylesheet to edit? *)
+  let css = if q#param_exists "css" then q#param "css" else "global" in
+  let css = stylesheet_of_string css in
 
-  template#set "css" css;
+  let content =
+    match css with
+    | Global ->
+       PGSQL(dbh) "select css from hosts where id = $hostid"
+    | Ie6_fixes ->
+       PGSQL(dbh) "select ie6_fixes_css from hosts where id = $hostid"
+    | Ie7_fixes ->
+       PGSQL(dbh) "select ie7_fixes_css from hosts where id = $hostid" in
+  let content =
+    match content with
+    | [Some c] -> c
+    | _ -> "" in
+
+  template#set "css" (string_of_stylesheet css);
+  template#set "content" content;
 
   q#template template
 
 let () =
-  register_script run
+  register_script ~restrict:[CanEditGlobalCSS] run