JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
let cms do header/title on /contact
[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,content,description,keywords');
6
7
8 require_once(DOCROOT . 'inc/wfpl/format.php');
9 require_once(DOCROOT . 'inc/wfpl/email.php');
10
11 function format_cms_filename($str) {
12         $str = format_filename($str);
13         $str = str_replace('.', '_', $str);
14         return $str;
15 }
16
17
18 function admin_pages_get_fields() {
19         $data = array();
20
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'));
28
29         return $data;
30 }
31
32
33 function admin_pages_main() {
34         session_auth_must('edit_page');
35
36         $id = _REQUEST_cut('edit_id');
37         if($id) {
38                 return admin_pages_main_form($id);
39         }
40
41         $id = _REQUEST_cut('admin_pages_delete_id');
42         if($id) {
43                 return admin_pages_main_delete($id);
44         }
45
46         if(_REQUEST_cut('new')) {
47                 return admin_pages_main_form();
48         }
49
50         if(_REQUEST_cut('list')) {
51                 return admin_pages_main_listing();
52         }
53
54         $id = _REQUEST_cut('id');
55         if($id) {
56                 return admin_pages_main_display($id);
57         }
58
59         if(isset($_POST['title'])) {
60                 return admin_pages_main_form();
61         }
62
63         # default action:
64         return admin_pages_main_listing();
65 }
66
67 # admin-only access to view pages with no filename
68 function admin_pages_main_display($id) {
69         cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
70 }
71
72 function admin_pages_main_delete($id) {
73         db_delete('cms_pages', 'where id=%i', $id);
74         message('Page deleted.');
75         return './admin_pages';
76 }
77
78 function admin_pages_get_images() {
79         $images = db_get_assocs('cms_images', 'image,name,caption,sizes', 'order by name, caption, image');
80         $id = 0;
81         foreach($images as &$image) {
82                 $image['id'] = '' . $id;
83                 $id += 1;
84         }
85         return $images;
86 }
87
88 function admin_pages_main_listing() {
89         $listing_rows = db_get_assocs('cms_pages', 'id,filename,coalesce(nullif(nav_title,\'\'), title) as title', 'order by concat(nav_title,title)');
90         tem_set('listings', $listing_rows);
91 }
92
93 function admin_pages_main_form($id = false) {
94         if($id) {
95                 tem_set('id', $id);
96         }
97
98         tem_set('$basename', 'admin_pages');
99
100         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
101         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
102         if($rows) for($i = 0; $i < count($rows); ++$i) {
103                 list($other_id, $other_title, $other_ord) = $rows[$i];
104                 if($other_id != $id) { # don't display ourselves
105                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
106                 }
107         }
108         pulldown('navbar', $navbar_options, PULLDOWN_2D);
109
110         if(isset($_POST['title'])) {
111                 $data = admin_pages_get_fields();
112
113                 # We'll save anything (no required fields)
114
115                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
116
117                 if($data['navbar'] && $data['filename'] == '') {
118                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
119                         $data['navbar'] = 0;
120                 }
121
122                 if($id) {
123                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
124                         $id = $id;
125                         message('Page updated.');
126                 } else {
127                         db_insert_assoc('cms_pages', $data);
128                         $id = db_auto_id();
129                         message('Page saved.');
130                 }
131                 if($data['filename']) {
132                         return "./$data[filename]";
133                 } else {
134                         return "./admin_pages?id=$id";
135                 }
136         } elseif($id) {
137                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
138                 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
139                 if($data['navbar']) {
140                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
141                 } else {
142                         $data['navbar'] = 'ignored';
143                 }
144         } else {
145                 # form not submitted, set default values:
146                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
147         }
148
149         tem_set('wfpl_images', admin_pages_get_images());
150         tem_set('form', $data);
151         tem_set('$head'); # wysiwyg init goes in <head>
152 }