JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
image paste code works captionless, refactor
[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_cut('name'));
55         $data['caption'] = format_oneline(_REQUEST_cut('caption'));
56         $data['sizes'] = format_unix(_REQUEST_cut('sizes'));
57
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']);
60         } else {
61                 if(_REQUEST_cut('delete_image') == 'Yes') {
62                         $data['image'] = '';
63                 } else {
64                         $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
65                 }
66         }
67         unset($_FILES['image']);
68
69         return $data;
70 }
71
72
73 function admin_images_main() {
74         if(!logged_in_as_admin()) {
75                 $_REQUEST['url'] = this_url();
76                 return 'admin_login';
77         }
78
79         $id = _REQUEST_cut('edit_id');
80         if($id) {
81                 return admin_images_main_form($id);
82         }
83
84         $id = _REQUEST_cut('admin_images_delete_id');
85         if($id) {
86                 return admin_images_main_delete($id);
87         }
88
89         if(_REQUEST_cut('new')) {
90                 return admin_images_main_form();
91         }
92
93         if(_REQUEST_cut('list')) {
94                 return admin_images_main_listing();
95         }
96
97         $id = _REQUEST_cut('id');
98         if($id) {
99                 return admin_images_main_display($id);
100         }
101
102         if(isset($_POST['name'])) {
103                 return admin_images_main_form();
104         }
105
106         # default action:
107         return admin_images_main_listing();
108 }
109
110 function admin_images_main_display($id) {
111         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
112         if(!$data) {
113                 message("Error: Broken Link (Image #$id 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_main_delete($id) {
165         db_delete('cms_images', 'where id=%i', $id);
166         message('Image deleted.');
167         return './admin_images';
168 }
169
170 function admin_images_main_listing() {
171         $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
172         tem_set('listings', $listing_rows);
173 }
174
175 function admin_images_main_form($id = false) {
176         if($id) {
177                 tem_set('id', $id);
178         }
179
180         if(isset($_POST['name'])) {
181                 $data = admin_images_get_fields();
182
183                 # save anything
184                 # 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.)
185
186                 # resize image as needed
187                 if($data['image'] && $data['sizes']) {
188                         $big_src = enc_image_src($data['image']);
189                         $row = explode("\n", $data['sizes']);
190                         foreach($row as $max_hw) {
191                                 $max_hw = format_width_height($max_hw);
192                                 if($max_hw == '') {
193                                         continue;
194                                 }
195                                 list($max_width, $max_height) = explode('x', $max_hw);
196                                 $src = str_replace('.', "-$max_hw.", $big_src);
197                                 if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
198                                         imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
199                                 }
200                         }
201                 }
202
203                 # save to database
204                 if($id) {
205                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
206                         message('Image updated.');
207                         $saved_id = $id;
208                 } else {
209                         db_insert_assoc('cms_images', $data);
210                         message('Image saved.');
211                         $saved_id = db_auto_id();
212                 }
213
214                 # return user to display page where they can see instructions, etc
215                 return "./admin_images?id=$saved_id";
216
217         } elseif($id) {
218                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
219                 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
220         } else {
221                 # form not submitted, set default values:
222                 $data = array('sizes' => '275x500');
223         }
224
225         tem_set('upload_max_filesize', upload_max_filesize());
226
227         tem_set('form', $data);
228 }