JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
generic editable site
[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 # code/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'] = '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
31
32
33 require_once('code/wfpl/format.php');
34 require_once('code/wfpl/email.php');
35 require_once('code/wfpl/upload.php');
36
37 # example: 200x300
38 function format_width_height($str) {
39         $fields = explode('x', $str);
40         if(count($fields) != 2) {
41                 return '';
42         }
43
44         list($width, $height) = $fields;
45         $width = format_int_0($width);
46         $height = format_int_0($height);
47
48         return "${width}x$height";
49 }
50
51 function admin_images_get_fields() {
52         $data = array();
53
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']);
59         } else {
60                 if($_REQUEST['delete_image'] == 'Yes') {
61                         $data['image'] = '';
62                 } else {
63                         $data['image'] = format_image_w_h_thumb_w_h($_REQUEST['old_image']);
64                 }
65         }
66
67         return $data;
68 }
69
70
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));
76                 return;
77         }
78
79         # make sure there's something clickable
80         foreach($rows as &$row) {
81                 if($row['name'] == '') {
82                         $row['name'] = '--';
83                 }
84         }
85         tem_set('listings', array(
86                 'populated_listing' => true,
87                 'rows' => $rows));
88         return true;
89 }
90
91 function admin_images_main() {
92         if(!logged_in_as_admin()) {
93                 $_REQUEST['url'] = this_url();
94                 return 'admin_login';
95         }
96
97         if(isset($_REQUEST['admin_images_id'])) {
98                 return admin_images_display_main();
99         } else {
100                 return admin_images_edit_main();
101         }
102 }
103
104 function admin_images_display_main() {
105         $id = format_int($_REQUEST['admin_images_id']);
106         unset($_REQUEST['admin_images_id']);
107         if(!$id) {
108                 message('Error: Broken link');
109                 return './admin_images';
110         }
111         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
112         if(!$data) {
113                 message('Error: Image not found');
114                 return './admin_images';
115         }
116
117         # Find pages that have this image on it
118         if($data['image']) {
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
120                 if($references) {
121                         $data['references'] = array(
122                                 'data' => $references,
123                                 'count' => count($references));
124                 }
125         }
126
127         # display smaller versions with instructions and example code
128         $smaller == array();
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);
134                         if($max_hw == '') {
135                                 continue;
136                         }
137                         list($max_width, $max_height) = explode('x', $max_hw);
138                         $src = str_replace('.', "-$max_hw.", $big_src);
139                         $dimensions = image_dimensions($src);
140                         if($dimensions) {
141                                 list($width, $height) = explode('x', $dimensions);
142                         } else {
143                                 $width = $max_width;
144                                 $height = $max_height;
145                         }
146
147                         $smaller[] = array(
148                                 'src' => $src,
149                                 'max_width' => $max_width,
150                                 'max_height' => $max_height,
151                                 'width' => $width,
152                                 'height' => $height);
153                 }
154         }
155         if($smaller) {
156                 $data['smaller'] = $smaller;
157         } else {
158                 tem_set('no_sizes');
159         }
160
161         tem_set('display', $data);
162 }
163
164 function admin_images_edit_main() {
165         $edit_id = format_int($_REQUEST['admin_images_edit_id']);
166         unset($_REQUEST['admin_images_edit_id']);
167         if($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');
172         }
173
174         $delete_id = format_int($_REQUEST['admin_images_delete_id']);
175         unset($_REQUEST['admin_images_delete_id']);
176         if($delete_id) {
177                 db_delete('cms_images', 'where id=%i', $delete_id);
178                 message('Image deleted.');
179
180                 return './admin_images';
181         }
182
183         if(!$edit_id) {
184                 if(!isset($_REQUEST['admin_images_new']) && !isset($_REQUEST['name'])) {
185                         admin_images_display_listing();
186                         return;
187                 }
188
189                 tem_set('new_msg', 'show');
190         }
191
192         if(isset($_POST['name'])) {
193                 $data = admin_images_get_fields();
194
195                 # save anything
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.
197
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);
204                                 if($max_hw == '') {
205                                         continue;
206                                 }
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');
211                                 }
212                         }
213                 }
214
215                 # save to database
216                 if($edit_id) {
217                         db_update_assoc('cms_images', $data, 'where id=%i', $edit_id);
218                         message('Image updated.');
219                         $saved_id = $edit_id;
220                 } else {
221                         db_insert_assoc('cms_images', $data);
222                         message('Image saved.');
223                         $saved_id = db_auto_id();
224                 }
225
226                 # return user to display page where they can see instructions, etc
227                 return "./admin_images?admin_images_id=$saved_id";
228
229         } elseif($edit_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);
232         } else {
233                 # form not submitted, set default values:
234                 $data = array('sizes' => '275x500');
235         }
236
237         tem_set('upload_max_filesize', upload_max_filesize());
238
239         tem_set('form', $data);
240 }
241
242 ?>