3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 define('ADMIN_PAGES_DB_FIELDS', 'title,filename,navbar,nav_title,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['content'] = format_unix(_REQUEST_cut('content'));
23 $data['description'] = format_unix(_REQUEST_cut('description'));
24 $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
30 function admin_pages_main() {
31 session_auth_must('edit_page');
33 $id = _REQUEST_cut('edit_id');
35 return admin_pages_main_form($id);
38 $id = _REQUEST_cut('admin_pages_delete_id');
40 return admin_pages_main_delete($id);
43 if(_REQUEST_cut('new')) {
44 return admin_pages_main_form();
47 if(_REQUEST_cut('list')) {
48 return admin_pages_main_listing();
51 $id = _REQUEST_cut('id');
53 return admin_pages_main_display($id);
56 if(isset($_POST['title'])) {
57 return admin_pages_main_form();
61 return admin_pages_main_listing();
64 # admin-only access to view pages with no filename
65 function admin_pages_main_display($id) {
66 cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
69 function admin_pages_main_delete($id) {
70 db_delete('cms_pages', 'where id=%i', $id);
71 message('Page deleted.');
72 return './admin_pages';
75 # get all images from admin_images (for cms)
76 function admin_pages_get_images() {
78 $rows = db_get_assocs('cms_images', 'image,name,caption', "order by coalesce(nullif(name, ''), caption), created_at");
81 foreach($rows as &$row) { $id += 1;
82 $parts = explode(' ', $row['image'] . ' ', 7);
86 'aspect' => ''.(round(100000 * ((int)$parts[2]) / ((int)$parts[1]) / 1000)).'%',
87 'name' => $row['name'],
88 'caption' => $row['caption']
95 function admin_pages_main_listing() {
98 $sort = _REQUEST_cut('sort');
99 if ($sort && substr($sort, 0, 1) === '-') {
100 $sort = substr($sort, 1);
103 $data["sorting-by-$sort"] = '-';
105 $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
106 if (!$sort || !in_array($sort, $legal_sorts)) {
110 $data['rows'] = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,filename', "order by $sort $desc limit 1000");
111 tem_set('listing', $data);
114 function admin_pages_main_form($id = false) {
119 tem_set('$basename', 'admin_pages');
121 $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
122 $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
123 if($rows) for($i = 0; $i < count($rows); ++$i) {
124 list($other_id, $other_title, $other_ord) = $rows[$i];
125 if($other_id != $id) { # don't display ourselves
126 $navbar_options[] = array($i + 1, "After \"$other_title\"");
129 pulldown('navbar', $navbar_options, PULLDOWN_2D);
131 if(isset($_POST['title'])) {
132 $data = admin_pages_get_fields();
134 # We'll save anything (no required fields)
136 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
138 if($data['navbar'] && $data['filename'] == '') {
139 message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
144 db_update_assoc('cms_pages', $data, 'where id=%i', $id);
146 message('Page updated.');
148 db_insert_assoc('cms_pages', $data);
150 message('Page saved.');
152 if($data['filename']) {
153 return "./$data[filename]";
155 return "./admin_pages?id=$id";
158 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
159 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
160 if($data['navbar']) {
161 $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
163 $data['navbar'] = 'ignored';
166 # form not submitted, set default values:
167 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
170 tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
171 tem_set('wfpl_image_width_full', WFPL_IMAGE_WIDTH_FULL);
172 tem_set('wfpl_image_width_small', WFPL_IMAGE_WIDTH_SMALL);
173 tem_set('wfpl_image_width_thumb', WFPL_IMAGE_WIDTH_THUMB);
174 tem_set('form', $data);
175 tem_set('$head'); # wysiwyg init goes in <head>