JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
get a little more semantic
[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,layout,content,sidebar_content,description,keywords');
6
7
8 function format_cms_filename($str) {
9         $str = format_filename($str);
10         $str = str_replace('.', '_', $str);
11         return $str;
12 }
13
14
15 function admin_pages_get_fields() {
16         $data = array();
17
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'));
26         }
27         if (isset($_REQUEST['description'])) {
28                 $data['description'] = format_unix(_REQUEST_cut('description'));
29         }
30         if (isset($_REQUEST['keywords'])) {
31                 $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
32         }
33
34         return $data;
35 }
36
37
38 function admin_pages_main() {
39         session_auth_must('edit_page');
40
41         $id = _REQUEST_cut('edit_id');
42         if($id) {
43                 return admin_pages_main_form($id);
44         }
45
46         $id = _REQUEST_cut('admin_pages_delete_id');
47         if($id) {
48                 return admin_pages_main_delete($id);
49         }
50
51         if(_REQUEST_cut('new')) {
52                 return admin_pages_main_form();
53         }
54
55         if(_REQUEST_cut('list')) {
56                 return admin_pages_main_listing();
57         }
58
59         $id = _REQUEST_cut('id');
60         if($id) {
61                 return admin_pages_main_display($id);
62         }
63
64         if(isset($_POST['title'])) {
65                 return admin_pages_main_form();
66         }
67
68         # default action:
69         return admin_pages_main_listing();
70 }
71
72 # admin-only access to view pages with no filename
73 function admin_pages_main_display($id) {
74         cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
75 }
76
77 function admin_pages_main_delete($id) {
78         db_delete('cms_pages', 'where id=%i', $id);
79         message('Page deleted.');
80         return './admin_pages';
81 }
82
83 # get all images from admin_images (for cms)
84 function admin_pages_get_images() {
85         $out = [];
86         $rows = db_get_assocs('cms_images', 'image,name,caption', "order by coalesce(nullif(name, ''), caption), created_at");
87         if ($rows) {
88                 $id = -1;
89                 foreach($rows as &$row) { $id += 1;
90                         $parts = explode(' ', $row['image'] . '      ', 7);
91                         $out[] = [
92                                 'id' => '' . $id,
93                                 'src' => $parts[0],
94                                 'aspect' => ''.(round(100000 * ((int)$parts[2]) / ((int)$parts[1]) / 1000)).'%',
95                                 'name' => $row['name'],
96                                 'caption' => $row['caption']
97                         ];
98                 } unset($row);
99         }
100         return $out;
101 }
102
103 function admin_pages_main_listing() {
104         $data = array();
105         $desc = '';
106         $sort = _REQUEST_cut('sort');
107         if ($sort && substr($sort, 0, 1) === '-') {
108                 $sort = substr($sort, 1);
109                 $desc = ' DESC ';
110         } else {
111                 $data["sorting-by-$sort"] = '-';
112         }
113         $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
114         if (!$sort || !in_array($sort, $legal_sorts)) {
115                 $sort = 'filename';
116         }
117
118         $data['rows'] = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,filename', "order by $sort $desc limit 1000");
119         tem_set('listing', $data);
120 }
121
122 function admin_pages_main_form($id = false) {
123         if($id) {
124                 tem_set('id', $id);
125         }
126
127         tem_set('$basename', 'admin_pages');
128
129         pulldown('layout', [
130                 ['0', "Full (no sidebar)"],
131                 ['1', "With Plain Sidebar"],
132                 ['2', "With Bordered Sidebar"]
133         ]);
134
135         $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
136         $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
137         if($rows) for($i = 0; $i < count($rows); ++$i) {
138                 list($other_id, $other_title, $other_ord) = $rows[$i];
139                 if($other_id != $id) { # don't display ourselves
140                         $navbar_options[] = array($i + 1, "After \"$other_title\"");
141                 }
142         }
143         pulldown('navbar', $navbar_options, PULLDOWN_2D);
144
145         if(isset($_POST['title'])) {
146                 $data = admin_pages_get_fields();
147
148                 # We'll save anything (no required fields)
149
150                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
151
152                 if($data['navbar'] && $data['filename'] == '') {
153                         message('This page was removed from the navigation column because it does not have a filename. (Pages without filenames are visible only to admins.)');
154                         $data['navbar'] = 0;
155                 }
156
157                 if($id) {
158                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
159                         $id = $id;
160                         message('Page updated.');
161                 } else {
162                         db_insert_assoc('cms_pages', $data);
163                         $id = db_auto_id();
164                         message('Page saved.');
165                 }
166                 if($data['filename']) {
167                         return "./$data[filename]";
168                 } else {
169                         return "./admin_pages?id=$id";
170                 }
171         } elseif($id) {
172                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
173                 $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
174                 if($data['navbar']) {
175                         $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
176                 } else {
177                         $data['navbar'] = 'ignored';
178                 }
179         } else {
180                 # form not submitted, set default values:
181                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
182         }
183
184         if (!isset($data['layout']) || $data['layout'] === '' || $data['layout'] === '0') {
185                 $data['sidebar_editor_display'] = 'none';
186         } else {
187                 $data['sidebar_editor_display'] = 'block';
188         }
189
190         tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
191         tem_set('wfpl_image_width_full', WFPL_IMAGE_WIDTH_FULL);
192         tem_set('wfpl_image_width_small', WFPL_IMAGE_WIDTH_SMALL);
193         tem_set('wfpl_image_width_thumb', WFPL_IMAGE_WIDTH_THUMB);
194         tem_set('form', $data);
195         tem_set('$head'); # wysiwyg init goes in <head>
196 }