JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
finish responsive images, revamp admin_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,sizes');
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         $id = _REQUEST_cut('id');
74         if($id) {
75                 return admin_images_main_display($id);
76         }
77
78         if(isset($_POST['name'])) {
79                 return admin_images_main_form();
80         }
81
82         # default action:
83         return admin_images_main_listing();
84 }
85
86 function admin_images_main_display($id) {
87         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
88         if(!$data) {
89                 message("Error: Broken Link (Image #$id not found)");
90                 return './admin_images';
91         }
92
93         # Find pages that have this image on it
94         if($data['image']) {
95                 $references = db_get_assocs('cms_pages', 'title,filename', 'where content like "%%%s%%" order by concat(nav_title,title)', substr(enc_image_src($data['image']), 0, -4));
96                 if($references) {
97                         $data['references'] = array(
98                                 'data' => $references,
99                                 'count' => count($references));
100                 }
101         }
102
103         tem_set('display', $data);
104 }
105
106 function admin_images_main_delete($id) {
107         $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
108         if ($data) {
109                 $src = enc_image_src($data['image']);
110                 if ($src) {
111                         $filenames = array($src);
112                         foreach ($GLOBALS['wfpl_image_widths'] as $w) {
113                                 $filenames [] = substr($src, 0, -4) . 'w' . $w . substr($src, -4);
114                         }
115                         foreach ($filenames as $filename) {
116                                 if (file_exists($filename)) {
117                                         unlink($filename);
118                                 }
119                         }
120                 }
121                 db_delete('cms_images', 'where id=%i', $id);
122                 message('Image deleted.');
123         } else {
124                 message("Couldn't find image to delete. Maybe it's already been deleted?");
125         }
126         return './admin_images';
127 }
128
129 function admin_images_main_listing() {
130         $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
131         tem_set('listings', $listing_rows);
132 }
133
134 function admin_images_main_form($id = false) {
135         if($id) {
136                 tem_set('id', $id);
137         }
138
139         if(isset($_POST['name'])) {
140                 $data = admin_images_get_fields();
141
142                 # save anything
143                 # 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.)
144
145                 # save to database
146                 if($id) {
147                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
148                         message('Image updated.');
149                         $saved_id = $id;
150                 } else {
151                         db_insert_assoc('cms_images', $data);
152                         message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
153                         $saved_id = db_auto_id();
154                 }
155
156                 # return user to display page where they can see instructions, etc
157                 return "./admin_images";
158
159         } elseif($id) {
160                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
161                 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
162         } else {
163                 # form not submitted, set default values:
164                 $data = array('sizes' => '275x500');
165         }
166
167         tem_set('upload_max_filesize', upload_max_filesize());
168
169         tem_set('form', $data);
170 }