JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix more paths (s/code/inc/)
[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 # inc/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('inc/wfpl/format.php');
34 require_once('inc/wfpl/upload.php');
35
36 # example: 200x300
37 function format_width_height($str) {
38         $fields = explode('x', $str);
39         if(count($fields) != 2) {
40                 return '';
41         }
42
43         list($width, $height) = $fields;
44         $width = format_int_0($width);
45         $height = format_int_0($height);
46
47         return "${width}x$height";
48 }
49
50 function admin_images_get_fields() {
51         $data = array();
52
53         $data['name'] = format_oneline(_REQUEST_cut('name'));
54         $data['caption'] = format_oneline(_REQUEST_cut('caption'));
55         $data['sizes'] = format_unix(_REQUEST_cut('sizes'));
56
57         if($_FILES['image'] && $_FILES['image']['error'] == 0) {
58                 $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']);
59         } else {
60                 if(_REQUEST_cut('delete_image') == 'Yes') {
61                         $data['image'] = '';
62                 } else {
63                         $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
64                 }
65         }
66         unset($_FILES['image']);
67
68         return $data;
69 }
70
71
72 function admin_images_main() {
73         if(!logged_in_as_admin()) {
74                 $_REQUEST['url'] = this_url();
75                 return 'admin_login';
76         }
77
78         $id = _REQUEST_cut('edit_id');
79         if($id) {
80                 return admin_images_main_form($id);
81         }
82
83         $id = _REQUEST_cut('admin_images_delete_id');
84         if($id) {
85                 return admin_images_main_delete($id);
86         }
87
88         if(_REQUEST_cut('new')) {
89                 return admin_images_main_form();
90         }
91
92         if(_REQUEST_cut('list')) {
93                 return admin_images_main_listing();
94         }
95
96         $id = _REQUEST_cut('id');
97         if($id) {
98                 return admin_images_main_display($id);
99         }
100
101         if(isset($_POST['name'])) {
102                 return admin_images_main_form();
103         }
104
105         # default action:
106         return admin_images_main_listing();
107 }
108
109 function admin_images_main_display($id) {
110         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
111         if(!$data) {
112                 message("Error: Broken Link (Image #$id not found)");
113                 return './admin_images';
114         }
115
116         # Find pages that have this image on it
117         if($data['image']) {
118                 $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
119                 if($references) {
120                         $data['references'] = array(
121                                 'data' => $references,
122                                 'count' => count($references));
123                 }
124         }
125
126         # display smaller versions with instructions and example code
127         $smaller == array();
128         if($data['image'] && $data['sizes']) {
129                 $big_src = enc_image_src($data['image']);
130                 $row = explode("\n", $data['sizes']);
131                 foreach($row as $max_hw) {
132                         $max_hw = format_width_height($max_hw);
133                         if($max_hw == '') {
134                                 continue;
135                         }
136                         list($max_width, $max_height) = explode('x', $max_hw);
137                         $src = str_replace('.', "-$max_width-$max_height.", $big_src);
138                         $dimensions = image_dimensions($src);
139                         if($dimensions) {
140                                 list($width, $height) = explode('x', $dimensions);
141                         } else {
142                                 $width = $max_width;
143                                 $height = $max_height;
144                         }
145
146                         $smaller[] = array(
147                                 'src' => $src,
148                                 'max_width' => $max_width,
149                                 'max_height' => $max_height,
150                                 'width' => $width,
151                                 'height' => $height);
152                 }
153         }
154         if($smaller) {
155                 $data['smaller'] = $smaller;
156         } else {
157                 tem_set('no_sizes');
158         }
159
160         tem_set('display', $data);
161 }
162
163 function admin_images_main_delete($id) {
164         $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
165         if ($data) {
166                 $filenames = array();
167                 $space = strpos($data['image'], ' ');
168                 $dot = strpos($data['image'], '.');
169                 if ($space !== false && $dot !== false && $dot < $space) {
170                         $base = substr($data['image'], 0, $dot);
171                         $ext = substr($data['image'], $dot, $space - $dot);
172                         $filenames[] = "$base$ext";
173                         $filenames[] = "{$base}_thumb$ext";
174                         $sizes = explode("\n", $data['sizes']);
175                         foreach ($sizes as $max_hw) {
176                                 $max_hw = format_width_height($max_hw);
177                                 if($max_hw == '') {
178                                         continue;
179                                 }
180                                 list($max_width, $max_height) = explode('x', $max_hw);
181                                 $filenames[] = "$base-{$max_width}x$max_height$ext"; # old naming scheme
182                                 $filenames[] = "$base-{$max_width}-$max_height$ext"; # new namich scheme
183                         }
184                 }
185                 foreach ($filenames as $filename) {
186                         if (file_exists($filename)) {
187                                 unlink($filename);
188                         }
189                 }
190                 db_delete('cms_images', 'where id=%i', $id);
191                 message('Image deleted.');
192         } else {
193                 message("Couldn't find image to delete. Maybe it's already been deleted?");
194         }
195         return './admin_images';
196 }
197
198 function admin_images_main_listing() {
199         $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
200         tem_set('listings', $listing_rows);
201 }
202
203 function admin_images_main_form($id = false) {
204         if($id) {
205                 tem_set('id', $id);
206         }
207
208         if(isset($_POST['name'])) {
209                 $data = admin_images_get_fields();
210
211                 # save anything
212                 # 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.)
213
214                 # resize image as needed
215                 if($data['image'] && $data['sizes']) {
216                         $big_src = enc_image_src($data['image']);
217                         $row = explode("\n", $data['sizes']);
218                         foreach($row as $max_hw) {
219                                 $max_hw = format_width_height($max_hw);
220                                 if($max_hw == '') {
221                                         continue;
222                                 }
223                                 list($max_width, $max_height) = explode('x', $max_hw);
224                                 $src = str_replace('.', "-$max_width-$max_height.", $big_src);
225                                 if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
226                                         imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
227                                 }
228                         }
229                 }
230
231                 # save to database
232                 if($id) {
233                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
234                         message('Image updated.');
235                         $saved_id = $id;
236                 } else {
237                         db_insert_assoc('cms_images', $data);
238                         message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
239                         $saved_id = db_auto_id();
240                 }
241
242                 # return user to display page where they can see instructions, etc
243                 return "./admin_images";
244
245         } elseif($id) {
246                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
247                 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
248         } else {
249                 # form not submitted, set default values:
250                 $data = array('sizes' => '275x500');
251         }
252
253         tem_set('upload_max_filesize', upload_max_filesize());
254
255         tem_set('form', $data);
256 }