JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
/admin_pages: listing sorts, missing image doesn't bork ckeditor
[wfpl-cms.git] / admin_pages.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/wfpl
4
5 define('ADMIN_PAGES_DB_FIELDS', 'title,filename,navbar,nav_title,content,description,keywords');
6
7
8 function format_cms_filename($str) {
9         $str = format_filename($str);
10         $str = str_replace('.', '_', $str);
11         return $str;
12 }
13
14
15 function admin_pages_get_fields() {
16         $data = array();
17
18         $data['title'] = format_oneline(_REQUEST_cut('title'));
19         $data['filename'] = format_cms_filename(_REQUEST_cut('filename'));
20         $data['navbar'] = format_options(_REQUEST_cut('navbar'), 'navbar');
21         $data['nav_title'] = format_oneline(_REQUEST_cut('nav_title'));
22         $data['content'] = format_unix(_REQUEST_cut('content'));
23         $data['description'] = format_unix(_REQUEST_cut('description'));
24         $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
25
26         return $data;
27 }
28
29
30 function admin_pages_main() {
31         session_auth_must('edit_page');
32
33         $id = _REQUEST_cut('edit_id');
34         if($id) {
35                 return admin_pages_main_form($id);
36         }
37
38         $id = _REQUEST_cut('admin_pages_delete_id');
39         if($id) {
40                 return admin_pages_main_delete($id);
41         }
42
43         if(_REQUEST_cut('new')) {
44                 return admin_pages_main_form();
45         }
46
47         if(_REQUEST_cut('list')) {
48                 return admin_pages_main_listing();
49         }
50
51         $id = _REQUEST_cut('id');
52         if($id) {
53                 return admin_pages_main_display($id);
54         }
55
56         if(isset($_POST['title'])) {
57                 return admin_pages_main_form();
58         }
59
60         # default action:
61         return admin_pages_main_listing();
62 }
63
64 # admin-only access to view pages with no filename
65 function admin_pages_main_display($id) {
66         cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
67 }
68
69 function admin_pages_main_delete($id) {
70         db_delete('cms_pages', 'where id=%i', $id);
71         message('Page deleted.');
72         return './admin_pages';
73 }
74
75 # get all images from admin_images (for cms)
76 function admin_pages_get_images() {
77         $out = [];
78         $rows = db_get_assocs('cms_images', 'image,name,caption,sizes', 'order by name, caption, image');
79         if ($rows) {
80                 $id = 0;
81                 foreach($rows as &$row) {
82                         $parts = explode(' ', $row['image'] . '      ', 7);
83                         $out[] = [
84                                 'id' => '' . $id,
85                                 'thumb' => $parts[3],
86                                 'image' => $parts[0],
87                                 'image_width' => (int)$parts[1],
88                                 'image_height' => (int)$parts[2],
89                                 'sizes' => $row['sizes'],
90                                 'name' => $row['name'],
91                                 'caption' => $row['caption']
92                         ];
93                         $id += 1;
94                 } unset($row);
95         }
96         return $out;
97 }
98
99 function admin_pages_main_listing() {
100         $data = array();
101         $desc = '';
102         $sort = _REQUEST_cut('sort');
103         if ($sort && substr($sort, 0, 1) === '-') {
104                 $sort = substr($sort, 1);
105                 $desc = ' DESC ';
106         } else {
107                 $data["sorting-by-$sort"] = '-';
108         }
109         $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
110         if (!$sort || !in_array($sort, $legal_sorts)) {
111                 $sort = 'filename';
112         }
113
114         $data['rows'] = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,filename', "order by $sort $desc limit 1000");
115         tem_set('listing', $data);
116 }
117
118 function admin_pages_main_form($id = false) {
119         if($id) {
120                 tem_set('id', $id);
121         }
122
123         tem_set('$basename', 'admin_pages');
124
125         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
126         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
127         if($rows) for($i = 0; $i < count($rows); ++$i) {
128                 list($other_id, $other_title, $other_ord) = $rows[$i];
129                 if($other_id != $id) { # don't display ourselves
130                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
131                 }
132         }
133         pulldown('navbar', $navbar_options, PULLDOWN_2D);
134
135         if(isset($_POST['title'])) {
136                 $data = admin_pages_get_fields();
137
138                 # We'll save anything (no required fields)
139
140                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
141
142                 if($data['navbar'] && $data['filename'] == '') {
143                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
144                         $data['navbar'] = 0;
145                 }
146
147                 if($id) {
148                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
149                         $id = $id;
150                         message('Page updated.');
151                 } else {
152                         db_insert_assoc('cms_pages', $data);
153                         $id = db_auto_id();
154                         message('Page saved.');
155                 }
156                 if($data['filename']) {
157                         return "./$data[filename]";
158                 } else {
159                         return "./admin_pages?id=$id";
160                 }
161         } elseif($id) {
162                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
163                 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
164                 if($data['navbar']) {
165                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
166                 } else {
167                         $data['navbar'] = 'ignored';
168                 }
169         } else {
170                 # form not submitted, set default values:
171                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
172         }
173
174         tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
175         tem_set('form', $data);
176         tem_set('$head'); # wysiwyg init goes in <head>
177 }