Removed dependency on imported merjislib.
[cocanwiki.git] / scripts / delete_file.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: delete_file.ml,v 1.3 2004/09/07 14:58:34 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_ok
14 open Cocanwiki_emailnotify
15
16 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
17   let id = int_of_string (q#param "id") in
18
19   if q#param_true "yes" then (
20     (* Delete the file. *)
21     let sth = dbh#prepare_cached "update files
22                                      set name_deleted = name, name = null
23                                    where hostid = ? and id = ?
24                                      and name is not null" in
25     sth#execute [`Int hostid; `Int id];
26
27     dbh#commit ();
28
29     (* Email notify. *)
30     let subject = "File #" ^ string_of_int id ^ " has been deleted." in
31     let body = fun () ->
32       "Page: http://" ^ hostname ^ "/_files?deleted=1" in
33
34     email_notify ~body ~subject dbh hostid;
35
36     (* Done. *)
37     let buttons = [ ok_button "/_files" ] in
38     ok ~title:"File deleted" ~buttons
39       q "File was deleted successfully."
40   ) else
41     q#redirect ("http://" ^ hostname ^ "/_files")
42
43 let () =
44   register_script run