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/upload.php');
37 function format_width_height($str) {
38 $fields = explode('x', $str);
39 if(count($fields) != 2) {
43 list($width, $height) = $fields;
44 $width = format_int_0($width);
45 $height = format_int_0($height);
47 return "${width}x$height";
50 function admin_images_get_fields() {
53 $data['name'] = format_oneline(_REQUEST_cut('name'));
54 $data['caption'] = format_oneline(_REQUEST_cut('caption'));
55 $data['sizes'] = format_unix(_REQUEST_cut('sizes'));
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']);
60 if(_REQUEST_cut('delete_image') == 'Yes') {
63 $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
66 unset($_FILES['image']);
72 function admin_images_main() {
73 if(!logged_in_as_admin()) {
74 $_REQUEST['url'] = this_url();
78 $id = _REQUEST_cut('edit_id');
80 return admin_images_main_form($id);
83 $id = _REQUEST_cut('admin_images_delete_id');
85 return admin_images_main_delete($id);
88 if(_REQUEST_cut('new')) {
89 return admin_images_main_form();
92 if(_REQUEST_cut('list')) {
93 return admin_images_main_listing();
96 $id = _REQUEST_cut('id');
98 return admin_images_main_display($id);
101 if(isset($_POST['name'])) {
102 return admin_images_main_form();
106 return admin_images_main_listing();
109 function admin_images_main_display($id) {
110 $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
112 message("Error: Broken Link (Image #$id not found)");
113 return './admin_images';
116 # Find pages that have this image on it
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
120 $data['references'] = array(
121 'data' => $references,
122 'count' => count($references));
126 # display smaller versions with instructions and example code
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);
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);
140 list($width, $height) = explode('x', $dimensions);
143 $height = $max_height;
148 'max_width' => $max_width,
149 'max_height' => $max_height,
151 'height' => $height);
155 $data['smaller'] = $smaller;
160 tem_set('display', $data);
163 function admin_images_main_delete($id) {
164 $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
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);
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
185 foreach ($filenames as $filename) {
186 if (file_exists($filename)) {
190 db_delete('cms_images', 'where id=%i', $id);
191 message('Image deleted.');
193 message("Couldn't find image to delete. Maybe it's already been deleted?");
195 return './admin_images';
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);
203 function admin_images_main_form($id = false) {
208 if(isset($_POST['name'])) {
209 $data = admin_images_get_fields();
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.)
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);
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');
233 db_update_assoc('cms_images', $data, 'where id=%i', $id);
234 message('Image updated.');
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();
242 # return user to display page where they can see instructions, etc
243 return "./admin_images";
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);
249 # form not submitted, set default values:
250 $data = array('sizes' => '275x500');
253 tem_set('upload_max_filesize', upload_max_filesize());
255 tem_set('form', $data);