3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
5 # This form was initially auto-generated. If you would like to alter the
6 # parameters and generate a new one try this URL:
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
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
18 # if you rename any of the database fields, you'll need to update this:
20 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption,sizes');
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/';
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
33 require_once('code/wfpl/format.php');
34 require_once('code/wfpl/email.php');
35 require_once('code/wfpl/upload.php');
38 function format_width_height($str) {
39 $fields = explode('x', $str);
40 if(count($fields) != 2) {
44 list($width, $height) = $fields;
45 $width = format_int_0($width);
46 $height = format_int_0($height);
48 return "${width}x$height";
51 function admin_images_get_fields() {
54 $data['name'] = format_oneline(_REQUEST_cut('name'));
55 $data['caption'] = format_oneline(_REQUEST_cut('caption'));
56 $data['sizes'] = format_unix(_REQUEST_cut('sizes'));
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']);
61 if(_REQUEST_cut('delete_image') == 'Yes') {
64 $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
67 unset($_FILES['image']);
73 function admin_images_main() {
74 if(!logged_in_as_admin()) {
75 $_REQUEST['url'] = this_url();
79 $id = _REQUEST_cut('edit_id');
81 return admin_images_main_form($id);
84 $id = _REQUEST_cut('admin_images_delete_id');
86 return admin_images_main_delete($id);
89 if(_REQUEST_cut('new')) {
90 return admin_images_main_form();
93 if(_REQUEST_cut('list')) {
94 return admin_images_main_listing();
97 $id = _REQUEST_cut('id');
99 return admin_images_main_display($id);
102 if(isset($_POST['name'])) {
103 return admin_images_main_form();
107 return admin_images_main_listing();
110 function admin_images_main_display($id) {
111 $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
113 message("Error: Broken Link (Image #$id not found)");
114 return './admin_images';
117 # Find pages that have this image on it
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
121 $data['references'] = array(
122 'data' => $references,
123 'count' => count($references));
127 # display smaller versions with instructions and example code
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);
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);
141 list($width, $height) = explode('x', $dimensions);
144 $height = $max_height;
149 'max_width' => $max_width,
150 'max_height' => $max_height,
152 'height' => $height);
156 $data['smaller'] = $smaller;
161 tem_set('display', $data);
164 function admin_images_main_delete($id) {
165 $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
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);
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
186 foreach ($filenames as $filename) {
187 if (file_exists($filename)) {
191 db_delete('cms_images', 'where id=%i', $id);
192 message('Image deleted.');
194 message("Couldn't find image to delete. Maybe it's already been deleted?");
196 return './admin_images';
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);
204 function admin_images_main_form($id = false) {
209 if(isset($_POST['name'])) {
210 $data = admin_images_get_fields();
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.)
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);
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');
234 db_update_assoc('cms_images', $data, 'where id=%i', $id);
235 message('Image updated.');
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();
243 # return user to display page where they can see instructions, etc
244 return "./admin_images";
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);
250 # form not submitted, set default values:
251 $data = array('sizes' => '275x500');
254 tem_set('upload_max_filesize', upload_max_filesize());
256 tem_set('form', $data);