JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new template naming conventions, etc
[wfpl-cms.git] / admin_pages.php
1 <?php
2
3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
4
5 define('ADMIN_PAGES_DB_FIELDS', 'title,filename,navbar,nav_title,content,description,keywords');
6
7
8 require_once('code/wfpl/format.php');
9 require_once('code/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         $data['title'] = format_oneline($_REQUEST['title']);
21         $data['filename'] = format_cms_filename($_REQUEST['filename']);
22         $data['navbar'] = format_oneline($_REQUEST['navbar'], 'navbar');
23         $data['nav_title'] = format_oneline($_REQUEST['nav_title']);
24         $data['content'] = format_unix($_REQUEST['content']);
25         $data['description'] = format_unix($_REQUEST['description']);
26         $data['keywords'] = format_unix($_REQUEST['keywords']);
27
28         return $data;
29 }
30
31
32 # You may pass a "where clause" for the db query.
33 function admin_pages_display_listing($where = 'order by concat(nav_title,title)') {
34         $rows = db_get_assocs('cms_pages', 'id,filename,coalesce(nullif(nav_title,\'\'), title) as title', $where);
35         if($rows == false || count($rows) == 0) {
36                 tem_set('listings', array('empty_listing' => true));
37                 return;
38         }
39
40         # make sure there's something clickable
41         foreach($rows as &$row) {
42                 if($row['filename'] == '') {
43                         $row['filename'] = '-- offline --';
44                 }
45                 if($row['title'] == '') {
46                         $row['title'] = '-- untitled --';
47                 }
48         }
49         tem_set('listings', array(
50                 'populated_listing' => true,
51                 'rows' => $rows));
52         return true;
53 }
54
55 function admin_pages_main() {
56         if(!logged_in_as_admin()) {
57                 $_REQUEST['url'] = this_url();
58                 return 'admin_login';
59         }
60
61         tem_set('this_host', this_host());
62
63         if(isset($_REQUEST['admin_pages_id'])) {
64                 return admin_pages_display_main();
65         } else {
66                 return admin_pages_edit_main();
67         }
68 }
69
70 # admin-only access to view pages with no filename
71 function admin_pages_display_main() {
72         $id = format_int($_REQUEST['admin_pages_id']);
73         unset($_REQUEST['admin_pages_id']);
74         if(!$id) {
75                 message('Error: Broken link');
76                 return './admin_pages';
77         }
78         cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
79 }
80
81 function admin_pages_edit_main() {
82         $edit_id = format_int($_REQUEST['admin_pages_edit_id']);
83         unset($_REQUEST['admin_pages_edit_id']);
84         if($edit_id) {
85                 # add hidden field for database id of row we're editing
86                 tem_set('admin_pages_edit_id', $edit_id);
87                 tem_set('editing', 'show');
88                 tem_set('edit_msg', 'show');
89         }
90
91         $delete_id = format_int($_REQUEST['admin_pages_delete_id']);
92         unset($_REQUEST['admin_pages_delete_id']);
93         if($delete_id) {
94                 db_delete('cms_pages', 'where id=%i', $delete_id);
95                 message('Page deleted.');
96
97                 return './admin_pages';
98         }
99
100         if(!$edit_id) {
101                 if(!isset($_REQUEST['admin_pages_new']) && !isset($_REQUEST['title'])) {
102                         admin_pages_display_listing();
103                         return;
104                 }
105
106                 tem_set('new_msg', 'show');
107         }
108
109         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
110         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
111         if($rows) for($i = 0; $i < count($rows); ++$i) {
112                 list($other_id, $other_title, $other_ord) = $rows[$i];
113                 if($other_id != $edit_id) { # don't display ourselves
114                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
115                 }
116         }
117         pulldown('navbar', $navbar_options, PULLDOWN_2D);
118
119         if(isset($_POST['title'])) {
120                 $data = admin_pages_get_fields();
121
122                 # We'll save anything (no required fields)
123
124                 $data['navbar'] = db_reposition('cms_pages', $edit_id, $data['navbar'], 'navbar', 'page');
125
126                 if($data['navbar'] && $data['filename'] == '') {
127                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
128                         $data['navbar'] = 0;
129                 }
130
131                 if($edit_id) {
132                         db_update_assoc('cms_pages', $data, 'where id=%i', $edit_id);
133                         $id = $edit_id;
134                         message('Page updated.');
135                 } else {
136                         db_insert_assoc('cms_pages', $data);
137                         $id = db_auto_id();
138                         message('Page saved.');
139                 }
140                 if($data['filename']) {
141                         return "./$data[filename]";
142                 } else {
143                         return "./admin_pages?admin_pages_id=$id";
144                 }
145         } elseif($edit_id) {
146                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
147                 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $edit_id);
148                 if($data['navbar']) {
149                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
150                 } else {
151                         $data['navbar'] = 'ignored';
152                 }
153         } else {
154                 # form not submitted, you can set default values like so:
155                 #$data = array('title' => 'Yes');
156                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
157         }
158
159         tem_set('form', $data);
160         tem_set('$head', 'show'); # wysiwyg init goes in <head>
161 }