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,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'];
148 tem_set('page_history', $data);
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);
154 message("Error 9393 please inform developer");
155 return './admin_pages';
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;
163 function admin_pages_main_form($id = false, $history_id = false) {
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';
171 $id = $history_data['id'];
177 tem_set('$basename', 'admin_pages');
180 ['0', "Full (no sidebar)"],
181 ['1', "With Plain Sidebar"],
182 ['2', "With Bordered Sidebar"]
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\"");
193 pulldown('navbar', $navbar_options, PULLDOWN_2D);
195 if(isset($_POST['title'])) {
196 $data = admin_pages_get_fields();
198 # We'll save anything (no required fields)
200 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
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.)');
209 db_update_assoc('cms_pages', $data, 'where id=%i', $id);
210 message('Page updated.');
212 db_insert_assoc('cms_pages', $data);
214 message('Page saved.');
217 # save to version history
219 $data['history_when'] = time();
221 $data['history_user_id'] = $s['id'];
222 db_insert_assoc('history_cms_pages', $data);
225 if ($data['filename'] === 'index') {
227 } elseif($data['filename'] !== '') {
228 return "./{$data['filename']}";
230 return "./admin_pages?id=$id";
233 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
235 $data = $history_data;
237 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
239 if($data['navbar']) {
240 $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
242 $data['navbar'] = 'ignored';
245 # form not submitted, set default values:
246 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
249 if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
250 $data['sidebar_editor_display'] = 'none';
252 $data['sidebar_editor_display'] = 'block';
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>