JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up admin_images now that there's cke_wfpl_images
[wfpl-cms.git] / admin_images.php
1 <?php
2
3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
4
5 # This form was initially auto-generated. If you would like to alter the
6 # parameters and generate a new one try this URL:
7 #
8 # http://jasonwoof.com/metaform/?file_name=admin_images&table_name=cms_images&singular=image&plural=images&opt_email=No&opt_db=Yes&opt_listing=Yes&opt_display=Yes&opt_pass=Yes&fields=image+thumb%0D%0Aname+textbox%0D%0Acaption+textbox%0D%0Asizes+textarea&edit=yes
9
10
11 # SETUP
12
13 # To save results to a database, you'll need to create the cms_images table
14 # (the file admin_images.sql should help with this), and create the file
15 # 'code/db_connect.php' which calls db_connect() see:
16 # code/wfpl/examples/db_connect.php
17 #
18 # if you rename any of the database fields, you'll need to update this:
19
20 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption,sizes');
21
22 # Set this to the path to your uploads directory. It can be relative to the
23 # location of this script. IT MUST END WITH A SLASH
24 $GLOBALS['upload_directory'] = 'cms_images/';
25
26 $GLOBALS['image_max_width'] = '704';
27 $GLOBALS['image_max_height'] = '1900';
28 $GLOBALS['image_thumb_max_width'] = '70';
29 $GLOBALS['image_thumb_max_height'] = '70';
30 $GLOBALS['image_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
31
32
33 require_once('code/wfpl/format.php');
34 require_once('code/wfpl/email.php');
35 require_once('code/wfpl/upload.php');
36
37 # example: 200x300
38 function format_width_height($str) {
39         $fields = explode('x', $str);
40         if(count($fields) != 2) {
41                 return '';
42         }
43
44         list($width, $height) = $fields;
45         $width = format_int_0($width);
46         $height = format_int_0($height);
47
48         return "${width}x$height";
49 }
50
51 function admin_images_get_fields() {
52         $data = array();
53
54         $data['name'] = format_oneline(_REQUEST_cut('name'));
55         $data['caption'] = format_oneline(_REQUEST_cut('caption'));
56         $data['sizes'] = format_unix(_REQUEST_cut('sizes'));
57
58         if($_FILES['image'] && $_FILES['image']['error'] == 0) {
59                 $data['image'] = convert_uploaded_image('image', $GLOBALS['upload_directory'] . $GLOBALS['image_file_name'], $GLOBALS['image_max_width'], $GLOBALS['image_max_height'], $GLOBALS['image_thumb_max_width'], $GLOBALS['image_thumb_max_height']);
60         } else {
61                 if(_REQUEST_cut('delete_image') == 'Yes') {
62                         $data['image'] = '';
63                 } else {
64                         $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
65                 }
66         }
67         unset($_FILES['image']);
68
69         return $data;
70 }
71
72
73 function admin_images_main() {
74         if(!logged_in_as_admin()) {
75                 $_REQUEST['url'] = this_url();
76                 return 'admin_login';
77         }
78
79         $id = _REQUEST_cut('edit_id');
80         if($id) {
81                 return admin_images_main_form($id);
82         }
83
84         $id = _REQUEST_cut('admin_images_delete_id');
85         if($id) {
86                 return admin_images_main_delete($id);
87         }
88
89         if(_REQUEST_cut('new')) {
90                 return admin_images_main_form();
91         }
92
93         if(_REQUEST_cut('list')) {
94                 return admin_images_main_listing();
95         }
96
97         $id = _REQUEST_cut('id');
98         if($id) {
99                 return admin_images_main_display($id);
100         }
101
102         if(isset($_POST['name'])) {
103                 return admin_images_main_form();
104         }
105
106         # default action:
107         return admin_images_main_listing();
108 }
109
110 function admin_images_main_display($id) {
111         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
112         if(!$data) {
113                 message("Error: Broken Link (Image #$id not found)");
114                 return './admin_images';
115         }
116
117         # Find pages that have this image on it
118         if($data['image']) {
119                 $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)); # FIXME test that this works for smaller images
120                 if($references) {
121                         $data['references'] = array(
122                                 'data' => $references,
123                                 'count' => count($references));
124                 }
125         }
126
127         # display smaller versions with instructions and example code
128         $smaller == array();
129         if($data['image'] && $data['sizes']) {
130                 $big_src = enc_image_src($data['image']);
131                 $row = explode("\n", $data['sizes']);
132                 foreach($row as $max_hw) {
133                         $max_hw = format_width_height($max_hw);
134                         if($max_hw == '') {
135                                 continue;
136                         }
137                         list($max_width, $max_height) = explode('x', $max_hw);
138                         $src = str_replace('.', "-$max_width-$max_height.", $big_src);
139                         $dimensions = image_dimensions($src);
140                         if($dimensions) {
141                                 list($width, $height) = explode('x', $dimensions);
142                         } else {
143                                 $width = $max_width;
144                                 $height = $max_height;
145                         }
146
147                         $smaller[] = array(
148                                 'src' => $src,
149                                 'max_width' => $max_width,
150                                 'max_height' => $max_height,
151                                 'width' => $width,
152                                 'height' => $height);
153                 }
154         }
155         if($smaller) {
156                 $data['smaller'] = $smaller;
157         } else {
158                 tem_set('no_sizes');
159         }
160
161         tem_set('display', $data);
162 }
163
164 function admin_images_main_delete($id) {
165         $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
166         if ($data) {
167                 $filenames = array();
168                 $space = strpos($data['image'], ' ');
169                 $dot = strpos($data['image'], '.');
170                 if ($space !== false && $dot !== false && $dot < $space) {
171                         $base = substr($data['image'], 0, $dot);
172                         $ext = substr($data['image'], $dot, $space - $dot);
173                         $filenames[] = "$base$ext";
174                         $filenames[] = "{$base}_thumb$ext";
175                         $sizes = explode("\n", $data['sizes']);
176                         foreach ($sizes as $max_hw) {
177                                 $max_hw = format_width_height($max_hw);
178                                 if($max_hw == '') {
179                                         continue;
180                                 }
181                                 list($max_width, $max_height) = explode('x', $max_hw);
182                                 $filenames[] = "$base-{$max_width}x$max_height$ext"; # old naming scheme
183                                 $filenames[] = "$base-{$max_width}-$max_height$ext"; # new namich scheme
184                         }
185                 }
186                 foreach ($filenames as $filename) {
187                         if (file_exists($filename)) {
188                                 unlink($filename);
189                         }
190                 }
191                 db_delete('cms_images', 'where id=%i', $id);
192                 message('Image deleted.');
193         } else {
194                 message("Couldn't find image to delete. Maybe it's already been deleted?");
195         }
196         return './admin_images';
197 }
198
199 function admin_images_main_listing() {
200         $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
201         tem_set('listings', $listing_rows);
202 }
203
204 function admin_images_main_form($id = false) {
205         if($id) {
206                 tem_set('id', $id);
207         }
208
209         if(isset($_POST['name'])) {
210                 $data = admin_images_get_fields();
211
212                 # save anything
213                 # 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.)
214
215                 # resize image as needed
216                 if($data['image'] && $data['sizes']) {
217                         $big_src = enc_image_src($data['image']);
218                         $row = explode("\n", $data['sizes']);
219                         foreach($row as $max_hw) {
220                                 $max_hw = format_width_height($max_hw);
221                                 if($max_hw == '') {
222                                         continue;
223                                 }
224                                 list($max_width, $max_height) = explode('x', $max_hw);
225                                 $src = str_replace('.', "-$max_width-$max_height.", $big_src);
226                                 if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
227                                         imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
228                                 }
229                         }
230                 }
231
232                 # save to database
233                 if($id) {
234                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
235                         message('Image updated.');
236                         $saved_id = $id;
237                 } else {
238                         db_insert_assoc('cms_images', $data);
239                         message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
240                         $saved_id = db_auto_id();
241                 }
242
243                 # return user to display page where they can see instructions, etc
244                 return "./admin_images";
245
246         } elseif($id) {
247                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
248                 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
249         } else {
250                 # form not submitted, set default values:
251                 $data = array('sizes' => '275x500');
252         }
253
254         tem_set('upload_max_filesize', upload_max_filesize());
255
256         tem_set('form', $data);
257 }