JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
first stab at paypal_ipn framework
[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 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['content'] = format_unix(_REQUEST_cut('content'));
23         $data['description'] = format_unix(_REQUEST_cut('description'));
24         $data['keywords'] = format_unix(_REQUEST_cut('keywords'));
25
26         return $data;
27 }
28
29
30 function admin_pages_main() {
31         session_auth_must('edit_page');
32
33         $id = _REQUEST_cut('edit_id');
34         if($id) {
35                 return admin_pages_main_form($id);
36         }
37
38         $id = _REQUEST_cut('admin_pages_delete_id');
39         if($id) {
40                 return admin_pages_main_delete($id);
41         }
42
43         if(_REQUEST_cut('new')) {
44                 return admin_pages_main_form();
45         }
46
47         if(_REQUEST_cut('list')) {
48                 return admin_pages_main_listing();
49         }
50
51         $id = _REQUEST_cut('id');
52         if($id) {
53                 return admin_pages_main_display($id);
54         }
55
56         if(isset($_POST['title'])) {
57                 return admin_pages_main_form();
58         }
59
60         # default action:
61         return admin_pages_main_listing();
62 }
63
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);
67 }
68
69 function admin_pages_main_delete($id) {
70         db_delete('cms_pages', 'where id=%i', $id);
71         message('Page deleted.');
72         return './admin_pages';
73 }
74
75 # get all images from admin_images (for cms)
76 function admin_pages_get_images() {
77         $out = [];
78         $rows = db_get_assocs('cms_images', 'image,name,caption', "order by coalesce(nullif(name, ''), caption), created_at");
79         if ($rows) {
80                 $id = -1;
81                 foreach($rows as &$row) { $id += 1;
82                         $parts = explode(' ', $row['image'] . '      ', 7);
83                         $out[] = [
84                                 'id' => '' . $id,
85                                 'src' => $parts[0],
86                                 'aspect' => ''.(round(100000 * ((int)$parts[2]) / ((int)$parts[1]) / 1000)).'%',
87                                 'name' => $row['name'],
88                                 'caption' => $row['caption']
89                         ];
90                 } unset($row);
91         }
92         return $out;
93 }
94
95 function admin_pages_main_listing() {
96         $data = array();
97         $desc = '';
98         $sort = _REQUEST_cut('sort');
99         if ($sort && substr($sort, 0, 1) === '-') {
100                 $sort = substr($sort, 1);
101                 $desc = ' DESC ';
102         } else {
103                 $data["sorting-by-$sort"] = '-';
104         }
105         $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
106         if (!$sort || !in_array($sort, $legal_sorts)) {
107                 $sort = 'filename';
108         }
109
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);
112 }
113
114 function admin_pages_main_form($id = false) {
115         if($id) {
116                 tem_set('id', $id);
117         }
118
119         tem_set('$basename', 'admin_pages');
120
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\"");
127                 }
128         }
129         pulldown('navbar', $navbar_options, PULLDOWN_2D);
130
131         if(isset($_POST['title'])) {
132                 $data = admin_pages_get_fields();
133
134                 # We'll save anything (no required fields)
135
136                 $data['navbar'] = db_reposition('cms_pages', $id, $data['navbar'], 'navbar', 'page');
137
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.)');
140                         $data['navbar'] = 0;
141                 }
142
143                 if($id) {
144                         db_update_assoc('cms_pages', $data, 'where id=%i', $id);
145                         $id = $id;
146                         message('Page updated.');
147                 } else {
148                         db_insert_assoc('cms_pages', $data);
149                         $id = db_auto_id();
150                         message('Page saved.');
151                 }
152                 if($data['filename']) {
153                         return "./$data[filename]";
154                 } else {
155                         return "./admin_pages?id=$id";
156                 }
157         } elseif($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']);
162                 } else {
163                         $data['navbar'] = 'ignored';
164                 }
165         } else {
166                 # form not submitted, set default values:
167                 $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
168         }
169
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>
176 }