JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
first entry in page history links to actual page
[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('admin_pages');
40
41         $id = _REQUEST_cut('edit_id');
42         if($id) {
43                 return admin_pages_main_form($id);
44         }
45
46         $id = _REQUEST_cut('edit_history_id');
47         if($id) {
48                 return admin_pages_main_form(null, $id);
49         }
50
51         $id = _REQUEST_cut('history_page_id');
52         if($id) {
53                 return admin_pages_main_page_history($id);
54         }
55
56         $id = _REQUEST_cut('history_id');
57         if($id) {
58                 return admin_pages_main_history($id);
59         }
60
61         $id = _REQUEST_cut('admin_pages_delete_id');
62         if($id) {
63                 return admin_pages_main_delete($id);
64         }
65
66         if(_REQUEST_cut('new')) {
67                 return admin_pages_main_form();
68         }
69
70         if(_REQUEST_cut('list')) {
71                 return admin_pages_main_listing();
72         }
73
74         $id = _REQUEST_cut('id');
75         if($id) {
76                 return admin_pages_main_display($id);
77         }
78
79         if(isset($_POST['title'])) {
80                 return admin_pages_main_form();
81         }
82
83         # default action:
84         return admin_pages_main_listing();
85 }
86
87 # admin-only access to view pages with no filename
88 function admin_pages_main_display($id) {
89         $page = db_get_assoc('cms_pages', 'id,title,keywords,description,layout,content,sidebar_content', 'where id=%"', $id);
90         cms_display_content($GLOBALS['wfpl_main_template'], $page);
91         $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_id'] = $id;
92 }
93
94 function admin_pages_main_delete($id) {
95         db_delete('cms_pages', 'where id=%i', $id);
96         message('Page deleted.');
97         return './admin_pages';
98 }
99
100 # get all images from admin_images (for cms)
101 function admin_pages_get_images() {
102         $out = [];
103         $rows = db_get_assocs('cms_images', 'image,name,caption', "order by coalesce(nullif(name, ''), caption), created_at");
104         if ($rows) {
105                 $id = -1;
106                 foreach($rows as &$row) { $id += 1;
107                         $parts = explode(' ', $row['image'] . '      ', 7);
108                         $out[] = [
109                                 'id' => '' . $id,
110                                 'src' => $parts[0],
111                                 'aspect' => ''.(round(100000 * ((int)$parts[2]) / ((int)$parts[1]) / 1000)).'%',
112                                 'name' => $row['name'],
113                                 'caption' => $row['caption']
114                         ];
115                 } unset($row);
116         }
117         return $out;
118 }
119
120 function admin_pages_main_listing() {
121         $data = array();
122
123         $rows = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,""), title) as title,filename', 'order by coalesce(nullif(nav_title,""), title), filename limit 2000');
124         $have_home_page = false;
125         if (count($rows) !== 0) {
126                 $data['rows'] = $rows;
127                 foreach ($rows as &$row) {
128                         if ($row['filename'] === 'index') {
129                                 $have_home_page = true;
130                                 break;
131                         }
132                 } unset($row);
133         }
134         if ($have_home_page === false) {
135                 $data['no_home'] = true;
136         }
137         tem_set('listing', $data);
138 }
139
140 function admin_pages_main_page_history ($id) {
141         $data = array();
142
143         $rows = db_get_assocs('history_cms_pages left join users on history_cms_pages.history_user_id = users.id', 'history_cms_pages.id as page_id,history_cms_pages.history_id,history_cms_pages.filename,coalesce(nullif(history_cms_pages.nav_title,""), history_cms_pages.title) as title,history_when as timestamp,coalesce(nullif(users.name,""), users.username) as who', 'where history_cms_pages.id=%i order by history_id desc', $id);
144         if (count($rows) !== 0) {
145                 $data['title'] = $rows[0]['title'];
146                 $rows[0]['first'] = true;
147                 if ($rows[0]['filename'] === 'index') {
148                         $rows[0]['filename'] = './';
149                 }
150                 $data['rows'] = $rows;
151         }
152         tem_set('page_history', $data);
153 }
154
155 function admin_pages_main_history ($id) {
156         $cms_page = db_get_assoc('history_cms_pages', 'id,title,keywords,description,layout,content,sidebar_content', 'where history_id=%"', $id);
157         if (!$cms_page) {
158                 message("Error 9393 please inform developer");
159                 return './admin_pages';
160         }
161         message('You are viewing an archived version of this page. To publish this version (revert the page) click the "Edit this page" link at the top, then the "save" at the bottom of the editor page. Feel free to make changes before saving.');
162         cms_display_content($GLOBALS['wfpl_main_template'], $cms_page);
163         $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_id'] = null;
164         $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_history_id'] = $id;
165 }
166
167 function admin_pages_main_form($id = false, $history_id = false) {
168         if ($history_id) {
169                 message('The fields below have been filled with an archived version of this page. To publish this version (revert the page) click "save" button at the bottom. Feel free to make changes before saving.');
170                 $history_data = db_get_assoc('history_cms_pages', 'id,'.ADMIN_PAGES_DB_FIELDS, 'where history_id=%i', $history_id);
171                 if (!$history_data) {
172                         message("Error 3938 please inform developer");
173                         return './admin_pages';
174                 }
175                 $id = $history_data['id'];
176         }
177         if($id) {
178                 tem_set('id', $id);
179         }
180
181         tem_set('$basename', 'admin_pages');
182
183         pulldown('layout', [
184                 ['0', "Full (no sidebar)"],
185                 ['1', "With Plain Sidebar"],
186                 ['2', "With Bordered Sidebar"]
187         ]);
188
189         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
190         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
191         if($rows) for($i = 0; $i < count($rows); ++$i) {
192                 list($other_id, $other_title, $other_ord) = $rows[$i];
193                 if($other_id != $id) { # don't display ourselves
194                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
195                 }
196         }
197         pulldown('navbar', $navbar_options, PULLDOWN_2D);
198
199         if(isset($_POST['title'])) {
200                 $data = admin_pages_get_fields();
201
202                 # We'll save anything (no required fields)
203
204                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
205
206                 if($data['navbar'] && $data['filename'] == '') {
207                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
208                         $data['navbar'] = 0;
209                 }
210
211                 # save
212                 if($id) {
213                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
214                         message('Page updated.');
215                 } else {
216                         db_insert_assoc('cms_pages', $data);
217                         $id = db_auto_id();
218                         message('Page saved.');
219                 }
220
221                 # save to version history
222                 $data['id'] = $id;
223                 $data['history_when'] = time();
224                 $s = session_auth();
225                 $data['history_user_id'] = $s['id'];
226                 db_insert_assoc('history_cms_pages', $data);
227
228                 # redirect
229                 if ($data['filename'] === 'index') {
230                         return './';
231                 } elseif($data['filename'] !== '') {
232                         return "./{$data['filename']}";
233                 } else {
234                         return "./admin_pages?id=$id";
235                 }
236         } elseif($id) {
237                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
238                 if ($history_id) {
239                         $data = $history_data;
240                 } else {
241                         $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
242                 }
243                 if($data['navbar']) {
244                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
245                 } else {
246                         $data['navbar'] = 'ignored';
247                 }
248         } else {
249                 # form not submitted, set default values:
250                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
251         }
252
253         if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
254                 $data['sidebar_editor_display'] = 'none';
255         } else {
256                 $data['sidebar_editor_display'] = 'block';
257         }
258
259         tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
260         tem_set('wfpl_image_width_full', WFPL_IMAGE_WIDTH_FULL);
261         tem_set('wfpl_image_width_small', WFPL_IMAGE_WIDTH_SMALL);
262         tem_set('wfpl_image_width_thumb', WFPL_IMAGE_WIDTH_THUMB);
263         tem_set('form', $data);
264         tem_set('$head'); # wysiwyg init goes in <head>
265 }