JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
progress on webpacking css
[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     } elseif ($id) {
177         if (db_count('history_cms_pages', 'where id=%i', $id) > 1) {
178             tem_set('archived_versions');
179         }
180     }
181     if($id) {
182         tem_set('id', $id);
183     }
184
185     tem_set('$basename', 'admin_pages');
186
187     pulldown('layout', [
188         ['0', "Full (no sidebar)"],
189         ['1', "With Plain Sidebar"],
190         ['2', "With Bordered Sidebar"]
191     ]);
192
193     $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
194     $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
195     if($rows) for($i = 0; $i < count($rows); ++$i) {
196         list($other_id, $other_title, $other_ord) = $rows[$i];
197         if($other_id != $id) { # don't display ourselves
198             $navbar_options[] = array($i + 1, "After \"$other_title\"");
199         }
200     }
201     pulldown('navbar', $navbar_options, PULLDOWN_2D);
202
203     if(isset($_POST['title'])) {
204         $data = admin_pages_get_fields();
205
206         # We'll save anything (no required fields)
207
208         $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
209
210         if($data['navbar'] && $data['filename'] == '') {
211             message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
212             $data['navbar'] = 0;
213         }
214
215         # save
216         if($id) {
217             db_update_assoc('cms_pages', $data, 'where id=%i', $id);
218             message('Page updated.');
219         } else {
220             db_insert_assoc('cms_pages', $data);
221             $id = db_auto_id();
222             message('Page saved.');
223         }
224
225         # save to version history
226         $data['id'] = $id;
227         $data['history_when'] = time();
228         $s = session_auth();
229         $data['history_user_id'] = $s['id'];
230         db_insert_assoc('history_cms_pages', $data);
231
232         # redirect
233         if ($data['filename'] === 'index') {
234             return './';
235         } elseif($data['filename'] !== '') {
236             return "./{$data['filename']}";
237         } else {
238             return "./admin_pages?id=$id";
239         }
240     } elseif($id) {
241         # we've recieved an edit id, but no data. So we grab the values to be edited from the database
242         if ($history_id) {
243             $data = $history_data;
244         } else {
245             $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
246         }
247         if($data['navbar']) {
248             $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
249         } else {
250             $data['navbar'] = 'ignored';
251         }
252     } else {
253         # form not submitted, set default values:
254         $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
255     }
256
257     if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
258         $data['sidebar_editor_display'] = 'none';
259     } else {
260         $data['sidebar_editor_display'] = 'block';
261     }
262
263     tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
264     tem_set('wfpl_image_width_full', WFPL_IMAGE_WIDTH_FULL);
265     tem_set('wfpl_image_width_small', WFPL_IMAGE_WIDTH_SMALL);
266     tem_set('wfpl_image_width_thumb', WFPL_IMAGE_WIDTH_THUMB);
267     tem_set('form', $data);
268     tem_set('$head'); # wysiwyg init goes in <head>
269 }