JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
progress on webpacking css
[wfpl-cms.git] / admin_images.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/wfpl
4
5 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption');
6
7 require_once(__DIR__.'/'.'inc/wfpl/format.php');
8 require_once(__DIR__.'/'.'inc/wfpl/upload.php');
9
10 # example: 200x300
11 function format_width_height($str) {
12     $fields = explode('x', $str);
13     if(count($fields) != 2) {
14         return '';
15     }
16
17     list($width, $height) = $fields;
18     $width = format_int_0($width);
19     $height = format_int_0($height);
20
21     return "${width}x$height";
22 }
23
24 function admin_images_get_fields() {
25     $data = array();
26
27     $data['name'] = format_oneline(_REQUEST_cut('name'));
28     $data['caption'] = format_oneline(_REQUEST_cut('caption'));
29
30     if($_FILES['image'] && $_FILES['image']['error'] == 0 && file_exists($_FILES['image']['tmp_name'])) {
31         $image_fn_ext = path_or_mime_to_ext($_FILES['image']['name'], $_FILES['image']['type']);
32         $image_fn_ext = ext_to_web_image_ext($image_fn_ext);
33         $image_fn_base = sha1_file($_FILES['image']['tmp_name']);
34         if (strlen($image_fn_base) == 40) {
35             $image_fn_base = substr($image_fn_base, 0, 16);
36             $image_filename = 'cms_images/' . $image_fn_base . '.' . $image_fn_ext;
37             $data['image'] = convert_uploaded_image('image', $image_filename);
38         }
39     } else {
40         if(_REQUEST_cut('delete_image') == 'Yes') {
41             $data['image'] = '';
42         }
43     }
44     unset($_FILES['image']);
45
46     return $data;
47 }
48
49
50 function admin_images_main() {
51     session_auth_must('admin_images');
52
53     $id = _REQUEST_cut('edit_id');
54     if($id) {
55         return admin_images_main_form($id);
56     }
57
58     $id = _REQUEST_cut('admin_images_delete_id');
59     if($id) {
60         return admin_images_main_delete($id);
61     }
62
63     if(_REQUEST_cut('new')) {
64         return admin_images_main_form();
65     }
66
67     if(_REQUEST_cut('list')) {
68         return admin_images_main_listing();
69     }
70
71     if(isset($_POST['name'])) {
72         return admin_images_main_form();
73     }
74
75     # default action:
76     return admin_images_main_listing();
77 }
78
79 function admin_images_main_delete($id) {
80     $data = db_get_assoc('cms_images', 'image', 'where id=%i', $id);
81     if ($data) {
82         $src = enc_image_src($data['image']);
83         if ($src) {
84             $filenames = array($src);
85             foreach ($GLOBALS['wfpl_image_widths'] as $w) {
86                 $filenames [] = substr($src, 0, -4) . 'w' . $w . substr($src, -4);
87             }
88             foreach ($filenames as $filename) {
89                 if (file_exists($filename)) {
90                     unlink($filename);
91                 }
92             }
93         }
94         db_delete('cms_images', 'where id=%i', $id);
95         message('Image deleted.');
96     } else {
97         message("Couldn't find image to delete. Maybe it's already been deleted?");
98     }
99     return './admin_images';
100 }
101
102 function admin_images_main_listing() {
103     $sort_by = 'name';
104     $sorts = array(
105         'age' => 'created_at desc',
106         'name' => "coalesce(nullif(name, ''), caption), created_at",
107         'caption' => "coalesce(nullif(caption, ''), name), created_at"
108     );
109     if (isset($_REQUEST['sort'])) {
110         foreach ($sorts as $s => $sql) {
111             if ($_REQUEST['sort'] == $s) {
112                 $sort_by = $s;
113                 break;
114             }
115         }
116     }
117     tem_set("sort_by_$sort_by");
118     $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by ' . $sorts[$sort_by]);
119     tem_set('listings', $listing_rows);
120 }
121
122 function admin_images_main_form($id = false) {
123     if($id) {
124         tem_set('id', $id);
125     }
126
127     if(isset($_POST['name'])) {
128         $data = admin_images_get_fields();
129
130         # save anything
131         # 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.)
132
133         # save to database
134         if($id) {
135             db_update_assoc('cms_images', $data, 'where id=%i', $id);
136             message('Image updated.');
137             $saved_id = $id;
138             return "./admin_images";
139         } else {
140             $data['created_at'] = time();
141             db_insert_assoc('cms_images', $data);
142             message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
143             $saved_id = db_auto_id();
144             return "./admin_images?sort=age";
145         }
146
147     } elseif($id) {
148         # we've recieved an edit id, but no data. So we grab the values to be edited from the database
149         $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
150     } else {
151         # form not submitted, set default values:
152         $data = array();
153     }
154
155     tem_set('upload_max_filesize', upload_max_filesize());
156
157     tem_set('form', $data);
158 }