All new templates. Site menu displays basically everywhere.
[cocanwiki.git] / scripts / images.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: images.ml,v 1.6 2004/09/09 09:35:33 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_template
14
15 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
16   let template = get_template dbh hostid "images.html" in
17
18   let deleted = q#param_true "deleted" in
19   template#conditional "deleted" deleted;
20
21   let sql =
22     "select id, name, name_deleted, width, height, alt, octet_length (image),
23             tn_width, tn_height
24        from images
25       where hostid = ? and " ^
26     (if not deleted then "name is not null"
27      else "name_deleted is not null") ^
28     " order by 2, 3" in
29   let sth = dbh#prepare_cached sql in
30   sth#execute [`Int hostid];
31
32   let table =
33     sth#map
34       (fun row ->
35          let id, name, width, height, alt, size, tn_width, tn_height,
36            is_deleted, has_thumbnail =
37            match row with
38              | [`Int id; `String name; `Null; `Int width; `Int height;
39                 `String alt; `Int size; `Int tn_width; `Int tn_height] ->
40                  id, name, width, height, alt, size, tn_width, tn_height,
41                  false, true
42              | [`Int id; `Null; `String name; `Int width; `Int height;
43                 `String alt; `Int size; `Int tn_width; `Int tn_height] ->
44                  id, name, width, height, alt, size, tn_width, tn_height,
45                  true, true
46              | [`Int id; `String name; `Null; `Int width; `Int height;
47                 `String alt; `Int size; `Null; `Null] ->
48                  id, name, width, height, alt, size, 0, 0,
49                  false, false
50              | [`Int id; `Null; `String name; `Int width; `Int height;
51                 `String alt; `Int size; `Null; `Null] ->
52                  id, name, width, height, alt, size, 0, 0,
53                  true, false
54              | _ -> assert false in
55          [ "id", Template.VarString (string_of_int id);
56            "name", Template.VarString name;
57            "width", Template.VarString (string_of_int width);
58            "height", Template.VarString (string_of_int height);
59            "alt", Template.VarString alt;
60            "ksize", Template.VarString (string_of_int (size / 1024));
61            "tn_width", Template.VarString (string_of_int tn_width);
62            "tn_height", Template.VarString (string_of_int tn_height);
63            "is_deleted", Template.VarConditional is_deleted;
64            "has_thumbnail", Template.VarConditional has_thumbnail ]) in
65
66   template#table "images" table;
67
68   q#template template
69
70 let () =
71   register_script ~restrict:[CanEdit] run