JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
06aa3ea6b67e11f293ace4be670fda59ccaf5c71
[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,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['rows'] = $rows;
146                 $data['title'] = $rows[0]['title'];
147         }
148         tem_set('page_history', $data);
149 }
150
151 function admin_pages_main_history ($id) {
152         $cms_page = db_get_assoc('history_cms_pages', 'id,title,keywords,description,layout,content,sidebar_content', 'where history_id=%"', $id);
153         if (!$cms_page) {
154                 message("Error 9393 please inform developer");
155                 return './admin_pages';
156         }
157         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.');
158         cms_display_content($GLOBALS['wfpl_main_template'], $cms_page);
159         $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_id'] = null;
160         $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_history_id'] = $id;
161 }
162
163 function admin_pages_main_form($id = false, $history_id = false) {
164         if ($history_id) {
165                 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.');
166                 $history_data = db_get_assoc('history_cms_pages', 'id,'.ADMIN_PAGES_DB_FIELDS, 'where history_id=%i', $history_id);
167                 if (!$history_data) {
168                         message("Error 3938 please inform developer");
169                         return './admin_pages';
170                 }
171                 $id = $history_data['id'];
172         }
173         if($id) {
174                 tem_set('id', $id);
175         }
176
177         tem_set('$basename', 'admin_pages');
178
179         pulldown('layout', [
180                 ['0', "Full (no sidebar)"],
181                 ['1', "With Plain Sidebar"],
182                 ['2', "With Bordered Sidebar"]
183         ]);
184
185         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
186         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
187         if($rows) for($i = 0; $i < count($rows); ++$i) {
188                 list($other_id, $other_title, $other_ord) = $rows[$i];
189                 if($other_id != $id) { # don't display ourselves
190                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
191                 }
192         }
193         pulldown('navbar', $navbar_options, PULLDOWN_2D);
194
195         if(isset($_POST['title'])) {
196                 $data = admin_pages_get_fields();
197
198                 # We'll save anything (no required fields)
199
200                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
201
202                 if($data['navbar'] && $data['filename'] == '') {
203                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
204                         $data['navbar'] = 0;
205                 }
206
207                 # save
208                 if($id) {
209                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
210                         message('Page updated.');
211                 } else {
212                         db_insert_assoc('cms_pages', $data);
213                         $id = db_auto_id();
214                         message('Page saved.');
215                 }
216
217                 # save to version history
218                 $data['id'] = $id;
219                 $data['history_when'] = time();
220                 $s = session_auth();
221                 $data['history_user_id'] = $s['id'];
222                 db_insert_assoc('history_cms_pages', $data);
223
224                 # redirect
225                 if ($data['filename'] === 'index') {
226                         return './';
227                 } elseif($data['filename'] !== '') {
228                         return "./{$data['filename']}";
229                 } else {
230                         return "./admin_pages?id=$id";
231                 }
232         } elseif($id) {
233                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
234                 if ($history_id) {
235                         $data = $history_data;
236                 } else {
237                         $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
238                 }
239                 if($data['navbar']) {
240                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
241                 } else {
242                         $data['navbar'] = 'ignored';
243                 }
244         } else {
245                 # form not submitted, set default values:
246                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
247         }
248
249         if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
250                 $data['sidebar_editor_display'] = 'none';
251         } else {
252                 $data['sidebar_editor_display'] = 'block';
253         }
254
255         tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
256         tem_set('wfpl_image_width_full', WFPL_IMAGE_WIDTH_FULL);
257         tem_set('wfpl_image_width_small', WFPL_IMAGE_WIDTH_SMALL);
258         tem_set('wfpl_image_width_thumb', WFPL_IMAGE_WIDTH_THUMB);
259         tem_set('form', $data);
260         tem_set('$head'); # wysiwyg init goes in <head>
261 }