JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
little space below menu
[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,layout,content,sidebar_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['layout'] = format_options(_REQUEST_cut('layout'), 'layout');
23         $data['content'] = format_unix(_REQUEST_cut('content'));
24         if (isset($_REQUEST['sidebar_content'])) { // probs not needed
25                 $data['sidebar_content'] = format_unix(_REQUEST_cut('sidebar_content'));
26         }
27         if (isset($_REQUEST['description'])) {
28                 $data['description'] = format_unix(_REQUEST_cut('description'));
29         }
30         if (isset($_REQUEST['keywords'])) {
31                 $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
32         }
33
34         return $data;
35 }
36
37
38 function admin_pages_main() {
39         session_auth_must('edit_page');
40
41         $id = _REQUEST_cut('edit_id');
42         if($id) {
43                 return admin_pages_main_form($id);
44         }
45
46         $id = _REQUEST_cut('admin_pages_delete_id');
47         if($id) {
48                 return admin_pages_main_delete($id);
49         }
50
51         if(_REQUEST_cut('new')) {
52                 return admin_pages_main_form();
53         }
54
55         if(_REQUEST_cut('list')) {
56                 return admin_pages_main_listing();
57         }
58
59         $id = _REQUEST_cut('id');
60         if($id) {
61                 return admin_pages_main_display($id);
62         }
63
64         if(isset($_POST['title'])) {
65                 return admin_pages_main_form();
66         }
67
68         # default action:
69         return admin_pages_main_listing();
70 }
71
72 # admin-only access to view pages with no filename
73 function admin_pages_main_display($id) {
74         cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
75         $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_id'] = $id;
76 }
77
78 function admin_pages_main_delete($id) {
79         db_delete('cms_pages', 'where id=%i', $id);
80         message('Page deleted.');
81         return './admin_pages';
82 }
83
84 # get all images from admin_images (for cms)
85 function admin_pages_get_images() {
86         $out = [];
87         $rows = db_get_assocs('cms_images', 'image,name,caption', "order by coalesce(nullif(name, ''), caption), created_at");
88         if ($rows) {
89                 $id = -1;
90                 foreach($rows as &$row) { $id += 1;
91                         $parts = explode(' ', $row['image'] . '      ', 7);
92                         $out[] = [
93                                 'id' => '' . $id,
94                                 'src' => $parts[0],
95                                 'aspect' => ''.(round(100000 * ((int)$parts[2]) / ((int)$parts[1]) / 1000)).'%',
96                                 'name' => $row['name'],
97                                 'caption' => $row['caption']
98                         ];
99                 } unset($row);
100         }
101         return $out;
102 }
103
104 function admin_pages_main_listing() {
105         $data = array();
106         $desc = '';
107         $sort = _REQUEST_cut('sort');
108         if ($sort && substr($sort, 0, 1) === '-') {
109                 $sort = substr($sort, 1);
110                 $desc = ' DESC ';
111         } else {
112                 $data["sorting-by-$sort"] = '-';
113         }
114         $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
115         if (!$sort || !in_array($sort, $legal_sorts)) {
116                 $sort = 'filename';
117         }
118
119         $data['rows'] = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,filename', "order by $sort $desc limit 1000");
120         tem_set('listing', $data);
121 }
122
123 function admin_pages_main_form($id = false) {
124         if($id) {
125                 tem_set('id', $id);
126         }
127
128         tem_set('$basename', 'admin_pages');
129
130         pulldown('layout', [
131                 ['0', "Full (no sidebar)"],
132                 ['1', "With Plain Sidebar"],
133                 ['2', "With Bordered Sidebar"]
134         ]);
135
136         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
137         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
138         if($rows) for($i = 0; $i < count($rows); ++$i) {
139                 list($other_id, $other_title, $other_ord) = $rows[$i];
140                 if($other_id != $id) { # don't display ourselves
141                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
142                 }
143         }
144         pulldown('navbar', $navbar_options, PULLDOWN_2D);
145
146         if(isset($_POST['title'])) {
147                 $data = admin_pages_get_fields();
148
149                 # We'll save anything (no required fields)
150
151                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
152
153                 if($data['navbar'] && $data['filename'] == '') {
154                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
155                         $data['navbar'] = 0;
156                 }
157
158                 if($id) {
159                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
160                         $id = $id;
161                         message('Page updated.');
162                 } else {
163                         db_insert_assoc('cms_pages', $data);
164                         $id = db_auto_id();
165                         message('Page saved.');
166                 }
167                 if($data['filename']) {
168                         return "./$data[filename]";
169                 } else {
170                         return "./admin_pages?id=$id";
171                 }
172         } elseif($id) {
173                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
174                 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
175                 if($data['navbar']) {
176                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
177                 } else {
178                         $data['navbar'] = 'ignored';
179                 }
180         } else {
181                 # form not submitted, set default values:
182                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
183         }
184
185         if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
186                 $data['sidebar_editor_display'] = 'none';
187         } else {
188                 $data['sidebar_editor_display'] = 'block';
189         }
190
191         tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
192         tem_set('wfpl_image_width_full', WFPL_IMAGE_WIDTH_FULL);
193         tem_set('wfpl_image_width_small', WFPL_IMAGE_WIDTH_SMALL);
194         tem_set('wfpl_image_width_thumb', WFPL_IMAGE_WIDTH_THUMB);
195         tem_set('form', $data);
196         tem_set('$head'); # wysiwyg init goes in <head>
197 }