3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
5 define('ADMIN_PAGES_DB_FIELDS', 'title,filename,navbar,nav_title,content,description,keywords');
8 require_once('code/wfpl/format.php');
9 require_once('code/wfpl/email.php');
11 function format_cms_filename($str) {
12 $str = format_filename($str);
13 $str = str_replace('.', '_', $str);
18 function admin_pages_get_fields() {
21 $data['title'] = format_oneline(_REQUEST_cut('title'));
22 $data['filename'] = format_cms_filename(_REQUEST_cut('filename'));
23 $data['navbar'] = format_options(_REQUEST_cut('navbar'), 'navbar');
24 $data['nav_title'] = format_oneline(_REQUEST_cut('nav_title'));
25 $data['content'] = format_unix(_REQUEST_cut('content'));
26 $data['description'] = format_unix(_REQUEST_cut('description'));
27 $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
33 function admin_pages_main() {
34 if(!logged_in_as_admin()) {
35 $_REQUEST['url'] = this_url();
39 $id = _REQUEST_cut('edit_id');
41 return admin_pages_main_form($id);
44 $id = _REQUEST_cut('admin_pages_delete_id');
46 return admin_pages_main_delete($id);
49 if(_REQUEST_cut('new')) {
50 return admin_pages_main_form();
53 if(_REQUEST_cut('list')) {
54 return admin_pages_main_listing();
57 $id = _REQUEST_cut('id');
59 return admin_pages_main_display($id);
62 if(isset($_POST['title'])) {
63 return admin_pages_main_form();
67 return admin_pages_main_listing();
70 # admin-only access to view pages with no filename
71 function admin_pages_main_display($id) {
72 cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
75 function admin_pages_main_delete($id) {
76 db_delete('cms_pages', 'where id=%i', $id);
77 message('Page deleted.');
78 return './admin_pages';
81 function admin_pages_get_images() {
82 $images = db_get_assocs('cms_images', 'image,name,caption,sizes', 'order by name, caption, image');
84 foreach($images as &$image) {
85 $image['id'] = '' . $id;
91 function admin_pages_main_listing() {
92 $listing_rows = db_get_assocs('cms_pages', 'id,filename,coalesce(nullif(nav_title,\'\'), title) as title', 'order by concat(nav_title,title)');
93 tem_set('listings', $listing_rows);
96 function admin_pages_main_form($id = false) {
102 $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
103 $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
104 if($rows) for($i = 0; $i < count($rows); ++$i) {
105 list($other_id, $other_title, $other_ord) = $rows[$i];
106 if($other_id != $id) { # don't display ourselves
107 $navbar_options[] = array($i + 1, "After \"$other_title\"");
110 pulldown('navbar', $navbar_options, PULLDOWN_2D);
112 if(isset($_POST['title'])) {
113 $data = admin_pages_get_fields();
115 # We'll save anything (no required fields)
117 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
119 if($data['navbar'] && $data['filename'] == '') {
120 message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
125 db_update_assoc('cms_pages', $data, 'where id=%i', $id);
127 message('Page updated.');
129 db_insert_assoc('cms_pages', $data);
131 message('Page saved.');
133 if($data['filename']) {
134 return "./$data[filename]";
136 return "./admin_pages?id=$id";
139 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
140 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
141 if($data['navbar']) {
142 $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
144 $data['navbar'] = 'ignored';
147 # form not submitted, set default values:
148 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
151 tem_set('wfpl_images', admin_pages_get_images());
152 tem_set('form', $data);
153 tem_set('$head'); # wysiwyg init goes in <head>