JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
/admin_pages: listing sorts, missing image doesn't bork ckeditor
[wfpl-cms.git] / admin_pages.php
index b1e7f16..9c7f56a 100644 (file)
@@ -1,13 +1,10 @@
 <?php
 
-# This form requires wfpl. See: http://jasonwoof.org/wfpl
+# This form requires wfpl. See: http://sametwice.com/wfpl
 
 define('ADMIN_PAGES_DB_FIELDS', 'title,filename,navbar,nav_title,content,description,keywords');
 
 
-require_once('code/wfpl/format.php');
-require_once('code/wfpl/email.php');
-
 function format_cms_filename($str) {
        $str = format_filename($str);
        $str = str_replace('.', '_', $str);
@@ -31,12 +28,7 @@ function admin_pages_get_fields() {
 
 
 function admin_pages_main() {
-       if(!logged_in_as_admin()) {
-               $_REQUEST['url'] = this_url();
-               return 'admin_login';
-       }
-
-       tem_set('this_host', this_host());
+       session_auth_must('edit_page');
 
        $id = _REQUEST_cut('edit_id');
        if($id) {
@@ -80,9 +72,47 @@ function admin_pages_main_delete($id) {
        return './admin_pages';
 }
 
+# get all images from admin_images (for cms)
+function admin_pages_get_images() {
+       $out = [];
+       $rows = db_get_assocs('cms_images', 'image,name,caption,sizes', 'order by name, caption, image');
+       if ($rows) {
+               $id = 0;
+               foreach($rows as &$row) {
+                       $parts = explode(' ', $row['image'] . '      ', 7);
+                       $out[] = [
+                               'id' => '' . $id,
+                               'thumb' => $parts[3],
+                               'image' => $parts[0],
+                               'image_width' => (int)$parts[1],
+                               'image_height' => (int)$parts[2],
+                               'sizes' => $row['sizes'],
+                               'name' => $row['name'],
+                               'caption' => $row['caption']
+                       ];
+                       $id += 1;
+               } unset($row);
+       }
+       return $out;
+}
+
 function admin_pages_main_listing() {
-       $listing_rows = db_get_assocs('cms_pages', 'id,filename,coalesce(nullif(nav_title,\'\'), title) as title', 'order by concat(nav_title,title)');
-       tem_set('listings', $listing_rows);
+       $data = array();
+       $desc = '';
+       $sort = _REQUEST_cut('sort');
+       if ($sort && substr($sort, 0, 1) === '-') {
+               $sort = substr($sort, 1);
+               $desc = ' DESC ';
+       } else {
+               $data["sorting-by-$sort"] = '-';
+       }
+       $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
+       if (!$sort || !in_array($sort, $legal_sorts)) {
+               $sort = 'filename';
+       }
+
+       $data['rows'] = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,filename', "order by $sort $desc limit 1000");
+       tem_set('listing', $data);
 }
 
 function admin_pages_main_form($id = false) {
@@ -90,6 +120,7 @@ function admin_pages_main_form($id = false) {
                tem_set('id', $id);
        }
 
+       tem_set('$basename', 'admin_pages');
 
        $navbar_options = array(array('ignored', 'Not at all'), array('0', 'First'));
        $rows = db_get_rows('cms_pages', 'id,coalesce(nullif(nav_title,\'\'), title) as title,navbar', 'where navbar != 0 order by navbar');
@@ -140,6 +171,7 @@ function admin_pages_main_form($id = false) {
                $data = array('filename' => format_cms_filename($_REQUEST['new_filename']));
        }
 
+       tem_set('wfpl_images_json', json_encode(admin_pages_get_images()));
        tem_set('form', $data);
        tem_set('$head'); # wysiwyg init goes in <head>
 }