3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 define('ADMIN_PAGES_DB_FIELDS', 'title,filename,navbar,nav_title,layout,content,sidebar_content,description,keywords');
8 function format_cms_filename($str) {
9 $str = format_filename($str);
10 $str = str_replace('.', '_', $str);
15 function admin_pages_get_fields() {
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'));
27 if (isset($_REQUEST['description'])) {
28 $data['description'] = format_unix(_REQUEST_cut('description'));
30 if (isset($_REQUEST['keywords'])) {
31 $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
38 function admin_pages_main() {
39 session_auth_must('admin_pages');
41 $id = _REQUEST_cut('edit_id');
43 return admin_pages_main_form($id);
46 $id = _REQUEST_cut('edit_history_id');
48 return admin_pages_main_form(null, $id);
51 $id = _REQUEST_cut('history_page_id');
53 return admin_pages_main_page_history($id);
56 $id = _REQUEST_cut('history_id');
58 return admin_pages_main_history($id);
61 $id = _REQUEST_cut('admin_pages_delete_id');
63 return admin_pages_main_delete($id);
66 if(_REQUEST_cut('new')) {
67 return admin_pages_main_form();
70 if(_REQUEST_cut('list')) {
71 return admin_pages_main_listing();
74 $id = _REQUEST_cut('id');
76 return admin_pages_main_display($id);
79 if(isset($_POST['title'])) {
80 return admin_pages_main_form();
84 return admin_pages_main_listing();
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;
94 function admin_pages_main_delete($id) {
95 db_delete('cms_pages', 'where id=%i', $id);
96 message('Page deleted.');
97 return './admin_pages';
100 # get all images from admin_images (for cms)
101 function admin_pages_get_images() {
103 $rows = db_get_assocs('cms_images', 'image,name,caption', "order by coalesce(nullif(name, ''), caption), created_at");
106 foreach($rows as &$row) { $id += 1;
107 $parts = explode(' ', $row['image'] . ' ', 7);
111 'aspect' => ''.(round(100000 * ((int)$parts[2]) / ((int)$parts[1]) / 1000)).'%',
112 'name' => $row['name'],
113 'caption' => $row['caption']
120 function admin_pages_main_listing() {
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;
134 if ($have_home_page === false) {
135 $data['no_home'] = true;
137 tem_set('listing', $data);
140 function admin_pages_main_page_history ($id) {
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'] = './';
150 $data['rows'] = $rows;
152 tem_set('page_history', $data);
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);
158 message("Error 9393 please inform developer");
159 return './admin_pages';
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;
167 function admin_pages_main_form($id = false, $history_id = false) {
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';
175 $id = $history_data['id'];
177 if (db_count('history_cms_pages', 'where id=%i', $id) > 1) {
178 tem_set('archived_versions');
185 tem_set('$basename', 'admin_pages');
188 ['0', "Full (no sidebar)"],
189 ['1', "With Plain Sidebar"],
190 ['2', "With Bordered Sidebar"]
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\"");
201 pulldown('navbar', $navbar_options, PULLDOWN_2D);
203 if(isset($_POST['title'])) {
204 $data = admin_pages_get_fields();
206 # We'll save anything (no required fields)
208 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
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.)');
217 db_update_assoc('cms_pages', $data, 'where id=%i', $id);
218 message('Page updated.');
220 db_insert_assoc('cms_pages', $data);
222 message('Page saved.');
225 # save to version history
227 $data['history_when'] = time();
229 $data['history_user_id'] = $s['id'];
230 db_insert_assoc('history_cms_pages', $data);
233 if ($data['filename'] === 'index') {
235 } elseif($data['filename'] !== '') {
236 return "./{$data['filename']}";
238 return "./admin_pages?id=$id";
241 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
243 $data = $history_data;
245 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
247 if($data['navbar']) {
248 $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
250 $data['navbar'] = 'ignored';
253 # form not submitted, set default values:
254 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
257 if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
258 $data['sidebar_editor_display'] = 'none';
260 $data['sidebar_editor_display'] = 'block';
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>