+<?php
+
+# This form requires wfpl. See: http://jasonwoof.org/wfpl
+
+# This form was initially auto-generated. If you would like to alter the
+# parameters and generate a new one try this URL:
+#
+# 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
+
+
+# SETUP
+
+# To save results to a database, you'll need to create the cms_images table
+# (the file admin_images.sql should help with this), and create the file
+# 'code/db_connect.php' which calls db_connect() see:
+# code/wfpl/examples/db_connect.php
+#
+# if you rename any of the database fields, you'll need to update this:
+
+define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption,sizes');
+
+# Set this to the path to your uploads directory. It can be relative to the
+# location of this script. IT MUST END WITH A SLASH
+$GLOBALS['upload_directory'] = 'cms_images/';
+
+$GLOBALS['image_max_width'] = '596';
+$GLOBALS['image_max_height'] = '1600';
+$GLOBALS['image_thumb_max_width'] = '70';
+$GLOBALS['image_thumb_max_height'] = '70';
+$GLOBALS['image_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
+
+
+require_once('code/wfpl/format.php');
+require_once('code/wfpl/email.php');
+require_once('code/wfpl/upload.php');
+
+# example: 200x300
+function format_width_height($str) {
+ $fields = explode('x', $str);
+ if(count($fields) != 2) {
+ return '';
+ }
+
+ list($width, $height) = $fields;
+ $width = format_int_0($width);
+ $height = format_int_0($height);
+
+ return "${width}x$height";
+}
+
+function admin_images_get_fields() {
+ $data = array();
+
+ $data['name'] = format_oneline($_REQUEST['name']);
+ $data['caption'] = format_oneline($_REQUEST['caption']);
+ $data['sizes'] = format_unix($_REQUEST['sizes']);
+ if($_FILES['image'] && $_FILES['image']['error'] == 0) {
+ $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']);
+ } else {
+ if($_REQUEST['delete_image'] == 'Yes') {
+ $data['image'] = '';
+ } else {
+ $data['image'] = format_image_w_h_thumb_w_h($_REQUEST['old_image']);
+ }
+ }
+
+ return $data;
+}
+
+
+# You may pass a "where clause" for the db query.
+function admin_images_display_listing($where = 'order by name, caption') {
+ $rows = db_get_assocs('cms_images', 'id,image,name,caption', $where);
+ if($rows == false || count($rows) == 0) {
+ tem_set('listings', array('empty_listing' => true));
+ return;
+ }
+
+ # make sure there's something clickable
+ foreach($rows as &$row) {
+ if($row['name'] == '') {
+ $row['name'] = '--';
+ }
+ }
+ tem_set('listings', array(
+ 'populated_listing' => true,
+ 'rows' => $rows));
+ return true;
+}
+
+function admin_images_main() {
+ if(!logged_in_as_admin()) {
+ $_REQUEST['url'] = this_url();
+ return 'admin_login';
+ }
+
+ if(isset($_REQUEST['admin_images_id'])) {
+ return admin_images_display_main();
+ } else {
+ return admin_images_edit_main();
+ }
+}
+
+function admin_images_display_main() {
+ $id = format_int($_REQUEST['admin_images_id']);
+ unset($_REQUEST['admin_images_id']);
+ if(!$id) {
+ message('Error: Broken link');
+ return './admin_images';
+ }
+ $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
+ if(!$data) {
+ message('Error: Image not found');
+ return './admin_images';
+ }
+
+ # Find pages that have this image on it
+ if($data['image']) {
+ $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
+ if($references) {
+ $data['references'] = array(
+ 'data' => $references,
+ 'count' => count($references));
+ }
+ }
+
+ # display smaller versions with instructions and example code
+ $smaller == array();
+ if($data['image'] && $data['sizes']) {
+ $big_src = enc_image_src($data['image']);
+ $row = explode("\n", $data['sizes']);
+ foreach($row as $max_hw) {
+ $max_hw = format_width_height($max_hw);
+ if($max_hw == '') {
+ continue;
+ }
+ list($max_width, $max_height) = explode('x', $max_hw);
+ $src = str_replace('.', "-$max_hw.", $big_src);
+ $dimensions = image_dimensions($src);
+ if($dimensions) {
+ list($width, $height) = explode('x', $dimensions);
+ } else {
+ $width = $max_width;
+ $height = $max_height;
+ }
+
+ $smaller[] = array(
+ 'src' => $src,
+ 'max_width' => $max_width,
+ 'max_height' => $max_height,
+ 'width' => $width,
+ 'height' => $height);
+ }
+ }
+ if($smaller) {
+ $data['smaller'] = $smaller;
+ } else {
+ tem_set('no_sizes');
+ }
+
+ tem_set('display', $data);
+}
+
+function admin_images_edit_main() {
+ $edit_id = format_int($_REQUEST['admin_images_edit_id']);
+ unset($_REQUEST['admin_images_edit_id']);
+ if($edit_id) {
+ # add hidden field for database id of row we're editing
+ tem_set('admin_images_edit_id', $edit_id);
+ tem_set('editing', 'show');
+ tem_set('edit_msg', 'show');
+ }
+
+ $delete_id = format_int($_REQUEST['admin_images_delete_id']);
+ unset($_REQUEST['admin_images_delete_id']);
+ if($delete_id) {
+ db_delete('cms_images', 'where id=%i', $delete_id);
+ message('Image deleted.');
+
+ return './admin_images';
+ }
+
+ if(!$edit_id) {
+ if(!isset($_REQUEST['admin_images_new']) && !isset($_REQUEST['name'])) {
+ admin_images_display_listing();
+ return;
+ }
+
+ tem_set('new_msg', 'show');
+ }
+
+ if(isset($_POST['name'])) {
+ $data = admin_images_get_fields();
+
+ # save anything
+ # 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.
+
+ # resize image as needed
+ if($data['image'] && $data['sizes']) {
+ $big_src = enc_image_src($data['image']);
+ $row = explode("\n", $data['sizes']);
+ foreach($row as $max_hw) {
+ $max_hw = format_width_height($max_hw);
+ if($max_hw == '') {
+ continue;
+ }
+ list($max_width, $max_height) = explode('x', $max_hw);
+ $src = str_replace('.', "-$max_hw.", $big_src);
+ if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
+ imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
+ }
+ }
+ }
+
+ # save to database
+ if($edit_id) {
+ db_update_assoc('cms_images', $data, 'where id=%i', $edit_id);
+ message('Image updated.');
+ $saved_id = $edit_id;
+ } else {
+ db_insert_assoc('cms_images', $data);
+ message('Image saved.');
+ $saved_id = db_auto_id();
+ }
+
+ # return user to display page where they can see instructions, etc
+ return "./admin_images?admin_images_id=$saved_id";
+
+ } elseif($edit_id) {
+ # we've recieved an edit id, but no data. So we grab the values to be edited from the database
+ $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $edit_id);
+ } else {
+ # form not submitted, set default values:
+ $data = array('sizes' => '275x500');
+ }
+
+ tem_set('upload_max_filesize', upload_max_filesize());
+
+ tem_set('form', $data);
+}
+
+?>