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'] = '596';
27 $GLOBALS['image_max_height'] = '1600';
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['name']);
55 $data['caption'] = format_oneline($_REQUEST['caption']);
56 $data['sizes'] = format_unix($_REQUEST['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['delete_image'] == 'Yes') {
63 $data['image'] = format_image_w_h_thumb_w_h($_REQUEST['old_image']);
71 # You may pass a "where clause" for the db query.
72 function admin_images_display_listing($where = 'order by name, caption') {
73 $rows = db_get_assocs('cms_images', 'id,image,name,caption', $where);
74 if($rows == false || count($rows) == 0) {
75 tem_set('listings', array('empty_listing' => true));
79 # make sure there's something clickable
80 foreach($rows as &$row) {
81 if($row['name'] == '') {
85 tem_set('listings', array(
86 'populated_listing' => true,
91 function admin_images_main() {
92 if(!logged_in_as_admin()) {
93 $_REQUEST['url'] = this_url();
97 if(isset($_REQUEST['admin_images_id'])) {
98 return admin_images_display_main();
100 return admin_images_edit_main();
104 function admin_images_display_main() {
105 $id = format_int($_REQUEST['admin_images_id']);
106 unset($_REQUEST['admin_images_id']);
108 message('Error: Broken link');
109 return './admin_images';
111 $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
113 message('Error: Image 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_hw.", $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_edit_main() {
165 $edit_id = format_int($_REQUEST['admin_images_edit_id']);
166 unset($_REQUEST['admin_images_edit_id']);
168 # add hidden field for database id of row we're editing
169 tem_set('admin_images_edit_id', $edit_id);
170 tem_set('editing', 'show');
171 tem_set('edit_msg', 'show');
174 $delete_id = format_int($_REQUEST['admin_images_delete_id']);
175 unset($_REQUEST['admin_images_delete_id']);
177 db_delete('cms_images', 'where id=%i', $delete_id);
178 message('Image deleted.');
180 return './admin_images';
184 if(!isset($_REQUEST['admin_images_new']) && !isset($_REQUEST['name'])) {
185 admin_images_display_listing();
189 tem_set('new_msg', 'show');
192 if(isset($_POST['name'])) {
193 $data = admin_images_get_fields();
196 # 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.
198 # resize image as needed
199 if($data['image'] && $data['sizes']) {
200 $big_src = enc_image_src($data['image']);
201 $row = explode("\n", $data['sizes']);
202 foreach($row as $max_hw) {
203 $max_hw = format_width_height($max_hw);
207 list($max_width, $max_height) = explode('x', $max_hw);
208 $src = str_replace('.', "-$max_hw.", $big_src);
209 if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
210 imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
217 db_update_assoc('cms_images', $data, 'where id=%i', $edit_id);
218 message('Image updated.');
219 $saved_id = $edit_id;
221 db_insert_assoc('cms_images', $data);
222 message('Image saved.');
223 $saved_id = db_auto_id();
226 # return user to display page where they can see instructions, etc
227 return "./admin_images?admin_images_id=$saved_id";
230 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
231 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $edit_id);
233 # form not submitted, set default values:
234 $data = array('sizes' => '275x500');
237 tem_set('upload_max_filesize', upload_max_filesize());
239 tem_set('form', $data);