JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
/admin_pages: fix cms_images icons for tall images
[wfpl-cms.git] / admin_images.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/wfpl
4
5 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption');
6
7 require_once(DOCROOT . 'inc/wfpl/format.php');
8 require_once(DOCROOT . 'inc/wfpl/upload.php');
9
10 # example: 200x300
11 function format_width_height($str) {
12         $fields = explode('x', $str);
13         if(count($fields) != 2) {
14                 return '';
15         }
16
17         list($width, $height) = $fields;
18         $width = format_int_0($width);
19         $height = format_int_0($height);
20
21         return "${width}x$height";
22 }
23
24 function admin_images_get_fields() {
25         $data = array();
26
27         $data['name'] = format_oneline(_REQUEST_cut('name'));
28         $data['caption'] = format_oneline(_REQUEST_cut('caption'));
29
30         if($_FILES['image'] && $_FILES['image']['error'] == 0 && file_exists($_FILES['image']['tmp_name'])) {
31                 $image_fn_ext = path_or_mime_to_ext($_FILES['image']['name'], $_FILES['image']['type']);
32                 $image_fn_ext = ext_to_web_image_ext($image_fn_ext);
33                 $image_fn_base = sha1_file($_FILES['image']['tmp_name']);
34                 if (strlen($image_fn_base) == 40) {
35                         $image_fn_base = substr($image_fn_base, 0, 16);
36                         $image_filename = 'cms_images/' . $image_fn_base . '.' . $image_fn_ext;
37                         $data['image'] = convert_uploaded_image('image', $image_filename);
38                 }
39         } else {
40                 if(_REQUEST_cut('delete_image') == 'Yes') {
41                         $data['image'] = '';
42                 } else {
43                         $data['image'] = format_image_w_h(_REQUEST_cut('old_image'));
44                 }
45         }
46         unset($_FILES['image']);
47
48         return $data;
49 }
50
51
52 function admin_images_main() {
53         session_auth_must('admin_images');
54
55         $id = _REQUEST_cut('edit_id');
56         if($id) {
57                 return admin_images_main_form($id);
58         }
59
60         $id = _REQUEST_cut('admin_images_delete_id');
61         if($id) {
62                 return admin_images_main_delete($id);
63         }
64
65         if(_REQUEST_cut('new')) {
66                 return admin_images_main_form();
67         }
68
69         if(_REQUEST_cut('list')) {
70                 return admin_images_main_listing();
71         }
72
73         if(isset($_POST['name'])) {
74                 return admin_images_main_form();
75         }
76
77         # default action:
78         return admin_images_main_listing();
79 }
80
81 function admin_images_main_delete($id) {
82         $data = db_get_assoc('cms_images', 'image', 'where id=%i', $id);
83         if ($data) {
84                 $src = enc_image_src($data['image']);
85                 if ($src) {
86                         $filenames = array($src);
87                         foreach ($GLOBALS['wfpl_image_widths'] as $w) {
88                                 $filenames [] = substr($src, 0, -4) . 'w' . $w . substr($src, -4);
89                         }
90                         foreach ($filenames as $filename) {
91                                 if (file_exists($filename)) {
92                                         unlink($filename);
93                                 }
94                         }
95                 }
96                 db_delete('cms_images', 'where id=%i', $id);
97                 message('Image deleted.');
98         } else {
99                 message("Couldn't find image to delete. Maybe it's already been deleted?");
100         }
101         return './admin_images';
102 }
103
104 function admin_images_main_listing() {
105         $sort_by = 'name';
106         $sorts = array(
107                 'age' => 'created_at desc',
108                 'name' => "coalesce(nullif(name, ''), caption), created_at",
109                 'caption' => "coalesce(nullif(caption, ''), name), created_at"
110         );
111         if (isset($_REQUEST['sort'])) {
112                 foreach ($sorts as $s => $sql) {
113                         if ($_REQUEST['sort'] == $s) {
114                                 $sort_by = $s;
115                                 break;
116                         }
117                 }
118         }
119         tem_set("sort_by_$sort_by");
120         $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by ' . $sorts[$sort_by]);
121         tem_set('listings', $listing_rows);
122 }
123
124 function admin_images_main_form($id = false) {
125         if($id) {
126                 tem_set('id', $id);
127         }
128
129         if(isset($_POST['name'])) {
130                 $data = admin_images_get_fields();
131
132                 # save anything
133                 # Note: If you change this to re-display the form in some cases, be sure to handle image uploads well (don't make them upload it again.)
134
135                 # save to database
136                 if($id) {
137                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
138                         message('Image updated.');
139                         $saved_id = $id;
140                         return "./admin_images";
141                 } else {
142                         $data['created_at'] = time();
143                         db_insert_assoc('cms_images', $data);
144                         message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
145                         $saved_id = db_auto_id();
146                         return "./admin_images?sort=age";
147                 }
148
149         } elseif($id) {
150                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
151                 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
152         } else {
153                 # form not submitted, set default values:
154                 $data = array();
155         }
156
157         tem_set('upload_max_filesize', upload_max_filesize());
158
159         tem_set('form', $data);
160 }