JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
page edit history
authorJason Woofenden <jason@jasonwoof.com>
Tue, 20 Jun 2017 04:14:29 +0000 (00:14 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 20 Jun 2017 04:14:29 +0000 (00:14 -0400)
admin_pages.html
admin_pages.php
inc/cms.php
inc/db_upgrade.php
template.html

index faa0eab..26c7f18 100644 (file)
 
        <!--~listing {~-->
                <h1>~$host~ Admin Control Panel</h1>
-               <h2>Manage Pages</h2>
 
-               <!--~rows once_if {~-->
-                       <p><a href="admin_pages?new=1">[Add a new page]</a></p>
+               <h2>Manage Pages</h2>
 
-                       <table cellspacing="0" cellpadding="4" border="0" summary="" class="evenodd">
+               <table cellspacing="0" cellpadding="4" border="0" summary="" class="evenodd">
+                       <tr>
+                               <td><em>(new)</em></td>
+                               <td>
+                                       <form style="display: inline-block" action="admin_pages"><input type="hidden" name="new" value="1"><input type="submit" value="create"></form>
+                               </td>
+                       </tr>
+                       <!--~no_home {~-->
                                <tr>
-                                       <th><a href="?sort=~sorting-by-filename~filename">Filename</a></th>
-                                       <th><a href="?sort=~sorting-by-title~title">Title</a></th>
-                                       <th>&nbsp;</th>
-                               </tr><!--~rows {~-->
-                               <tr>
-                                       <td class="listing"><a href="admin_pages?edit_id=~id~">~filename html~<!--~filename empty {~--><em>(offline)</em><!--~}~--></a></td>
-                                       <td class="listing"><a href="admin_pages?edit_id=~id~">~title html~<!--~title empty {~--><em>(untitled)</em><!--~}~--></a></td>
-                                       <td><a href="admin_pages?admin_pages_delete_id=~id~" onclick="return confirm('Permanently delete?')">[delete]</a></td>
-                               </tr><!--~}~-->
+                                       <td><em>(home page)</em></td>
+                                       <td>
+                                               <form style="display: inline-block" action="admin_pages">
+                                                       <input type="hidden" name="new" value="1">
+                                                       <input type="hidden" name="new_filename" value="index">
+                                                       <input type="submit" value="create"></form>
+                                       </td>
+                               </tr>
+                       <!--~}~-->
+                       <!--~rows {~-->
+                       <tr>
+                               <td>~title html~<!--~title empty {~--><em>(untitled)</em><!--~}~--></td>
+                               <td>
+                                       <form style="display: inline-block" action="admin_pages"><input type="hidden" name="edit_id" value="~id~"><input type="submit" value="edit"></form>
+                                       <form style="display: inline-block" action="admin_pages"><input type="hidden" name="history_page_id" value="~id~"><input type="submit" value="history"></form>
+                                       <form style="display: inline-block" action="admin_pages" onsubmit="return confirm('Permanently delete?')"><input type="hidden" name="admin_pages_delete_id" value="~id~"><input type="submit" value="delete"></form>
+                               </td>
+                       </tr><!--~}~-->
 
-                       </table>
-               <!--~}~-->
-               <!--~rows once_else {~-->
-                       <p>No pages in database.</p>
-               <!--~}~-->
+               </table>
+       <!--~}~-->
+       <!--~page_history {~-->
+               <h1>~$host~ Admin Control Panel</h1>
+
+               <h2>Edit history for page "~title html~"</h2>
 
-               <p><a href="admin_pages?new=1">[Add a new page]</a></p>
+               <table cellspacing="0" cellpadding="4" border="0" summary="" class="evenodd">
+                       <tr>
+                               <th>title</th>
+                               <th>at</th>
+                               <th>by</th>
+                               <th></th>
+                       </tr>
+                       <!--~rows {~-->
+                               <tr>
+                                       <td>~title html~<!--~title empty {~--><em>(untitled)</em><!--~}~--></td>
+                                       <td class="timestamp">~timestamp~</td>
+                                       <td>~who html~</td>
+                                       <td>
+                                               <form style="display: inline-block" action="admin_pages"><input type="hidden" name="history_id" value="~history_id~"><input type="submit" value="view"></form>
+                                       </td>
+                               </tr>
+                       <!--~}~-->
+               </table>
        <!--~}~-->
 <!--~}~-->
 </body>
index 4081f94..06aa3ea 100644 (file)
@@ -43,6 +43,21 @@ function admin_pages_main() {
                return admin_pages_main_form($id);
        }
 
+       $id = _REQUEST_cut('edit_history_id');
+       if($id) {
+               return admin_pages_main_form(null, $id);
+       }
+
+       $id = _REQUEST_cut('history_page_id');
+       if($id) {
+               return admin_pages_main_page_history($id);
+       }
+
+       $id = _REQUEST_cut('history_id');
+       if($id) {
+               return admin_pages_main_history($id);
+       }
+
        $id = _REQUEST_cut('admin_pages_delete_id');
        if($id) {
                return admin_pages_main_delete($id);
@@ -71,7 +86,8 @@ function admin_pages_main() {
 
 # admin-only access to view pages with no filename
 function admin_pages_main_display($id) {
-       cms_display_content($GLOBALS['wfpl_main_template'], 'where id=%i', $id);
+       $page = db_get_assoc('cms_pages', 'id,title,keywords,description,layout,content,sidebar_content', 'where id=%"', $id);
+       cms_display_content($GLOBALS['wfpl_main_template'], $page);
        $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_id'] = $id;
 }
 
@@ -103,24 +119,57 @@ function admin_pages_get_images() {
 
 function admin_pages_main_listing() {
        $data = array();
-       $desc = '';
-       $sort = _REQUEST_cut('sort');
-       if ($sort && substr($sort, 0, 1) === '-') {
-               $sort = substr($sort, 1);
-               $desc = ' DESC ';
-       } else {
-               $data["sorting-by-$sort"] = '-';
+
+       $rows = db_get_assocs('cms_pages', 'id,coalesce(nullif(nav_title,""), title) as title,filename', 'order by coalesce(nullif(nav_title,""), title), filename limit 2000');
+       $have_home_page = false;
+       if (count($rows) !== 0) {
+               $data['rows'] = $rows;
+               foreach ($rows as &$row) {
+                       if ($row['filename'] === 'index') {
+                               $have_home_page = true;
+                               break;
+                       }
+               } unset($row);
        }
-       $legal_sorts = explode(',', ADMIN_PAGES_DB_FIELDS);
-       if (!$sort || !in_array($sort, $legal_sorts)) {
-               $sort = 'filename';
+       if ($have_home_page === false) {
+               $data['no_home'] = true;
        }
-
-       $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) {
+function admin_pages_main_page_history ($id) {
+       $data = array();
+
+       $rows = db_get_assocs('history_cms_pages left join users on history_cms_pages.history_user_id = users.id', 'history_cms_pages.id as page_id,history_cms_pages.history_id,coalesce(nullif(history_cms_pages.nav_title,""), history_cms_pages.title) as title,history_when as timestamp,coalesce(nullif(users.name,""), users.username) as who', 'where history_cms_pages.id=%i order by history_id desc', $id);
+       if (count($rows) !== 0) {
+               $data['rows'] = $rows;
+               $data['title'] = $rows[0]['title'];
+       }
+       tem_set('page_history', $data);
+}
+
+function admin_pages_main_history ($id) {
+       $cms_page = db_get_assoc('history_cms_pages', 'id,title,keywords,description,layout,content,sidebar_content', 'where history_id=%"', $id);
+       if (!$cms_page) {
+               message("Error 9393 please inform developer");
+               return './admin_pages';
+       }
+       message('You are viewing an archived version of this page. To publish this version (revert the page) click the "Edit this page" link at the top, then the "save" at the bottom of the editor page. Feel free to make changes before saving.');
+       cms_display_content($GLOBALS['wfpl_main_template'], $cms_page);
+       $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_id'] = null;
+       $GLOBALS['wfpl_main_template']->data['$admin_links']['$edit_page_history_id'] = $id;
+}
+
+function admin_pages_main_form($id = false, $history_id = false) {
+       if ($history_id) {
+               message('The fields below have been filled with an archived version of this page. To publish this version (revert the page) click "save" button at the bottom. Feel free to make changes before saving.');
+               $history_data = db_get_assoc('history_cms_pages', 'id,'.ADMIN_PAGES_DB_FIELDS, 'where history_id=%i', $history_id);
+               if (!$history_data) {
+                       message("Error 3938 please inform developer");
+                       return './admin_pages';
+               }
+               $id = $history_data['id'];
+       }
        if($id) {
                tem_set('id', $id);
        }
@@ -155,23 +204,38 @@ function admin_pages_main_form($id = false) {
                        $data['navbar'] = 0;
                }
 
+               # save
                if($id) {
                        db_update_assoc('cms_pages', $data, 'where id=%i', $id);
-                       $id = $id;
                        message('Page updated.');
                } else {
                        db_insert_assoc('cms_pages', $data);
                        $id = db_auto_id();
                        message('Page saved.');
                }
-               if($data['filename']) {
-                       return "./$data[filename]";
+
+               # save to version history
+               $data['id'] = $id;
+               $data['history_when'] = time();
+               $s = session_auth();
+               $data['history_user_id'] = $s['id'];
+               db_insert_assoc('history_cms_pages', $data);
+
+               # redirect
+               if ($data['filename'] === 'index') {
+                       return './';
+               } elseif($data['filename'] !== '') {
+                       return "./{$data['filename']}";
                } else {
                        return "./admin_pages?id=$id";
                }
        } elseif($id) {
                # we've recieved an edit id, but no data. So we grab the values to be edited from the database
-               $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
+               if ($history_id) {
+                       $data = $history_data;
+               } else {
+                       $data = db_get_assoc('cms_pages', ADMIN_PAGES_DB_FIELDS, 'where id=%i', $id);
+               }
                if($data['navbar']) {
                        $data['navbar'] = db_count('cms_pages', 'where navbar!=0 && navbar<%i', $data['navbar']);
                } else {
index 06324f0..5075dd7 100644 (file)
@@ -53,7 +53,8 @@ function cms_display($basename, &$tem) {
                $tem->set('$cms_css', $hi_css);
        }
 
-       $cms_page_id = cms_display_content($tem, 'where filename=%"', $basename);
+       $cms_page = db_get_assoc('cms_pages', 'id,title,keywords,description,layout,content,sidebar_content', 'where filename=%"', $basename);
+       cms_display_content($tem, $cms_page);
 
        $footer = db_get_value('cms_pages', 'content', 'where filename="_footer"');
        if ($footer) {
@@ -62,23 +63,20 @@ function cms_display($basename, &$tem) {
 
        if(session_auth_can('admin_links')) {
                $admin_links = array();
-               if($cms_page_id) {
-                       $admin_links['$edit_page_id'] = $cms_page_id;
+               if($cms_page) {
+                       $admin_links['$edit_page_id'] = $cms_page['id'];
                }
                $tem->set('$admin_links', $admin_links);
        }
 
-       if($cms_page_id) {
+       if($cms_page) {
                return true;
        } else {
                return false;
        }
 }
 
-function cms_display_content(&$tem /*, 'where clause %", %i', string, int */) {
-       $args = array_slice(func_get_args(), 1);
-       $args = array_merge(array('cms_pages', 'id,title,keywords,description,layout,content,sidebar_content'), $args);
-       $row = call_user_func_array('db_get_assoc', $args);
+function cms_display_content(&$tem, $row) {
        if($row) {
                $tem->set('$cms_title', $row['title']);
                $tem->set('$meta_keywords', $row['keywords']);
@@ -99,5 +97,4 @@ function cms_display_content(&$tem /*, 'where clause %", %i', string, int */) {
        } else {
                $tem->set('$layout_centerer_class', 'full');
        }
-       return false;
 }
index 8bfa14f..c856fad 100644 (file)
@@ -115,3 +115,54 @@ db_send_query(<<<EOLsql
 EOLsql
 );
 }
+
+function db_upgrade_to_2() {
+db_send_query(<<<EOLsql
+       create table history_cms_pages (
+               history_id int unique auto_increment,
+               history_when int not null default 0,
+               history_user_id int not null default 0,
+               id int,
+               filename varchar(200) not null default "",
+               title varchar(200) not null default "",
+               nav_title varchar(200) not null default "",
+               navbar int not null default 0,
+               layout int not null default 0,
+               content mediumtext not null default "",
+               sidebar_content mediumtext not null default "",
+               description text not null default "",
+               keywords text not null default ""
+       );
+EOLsql
+);
+db_send_query(<<<EOLsql
+       insert into history_cms_pages (
+               history_when,
+               history_user_id,
+               id,
+               filename,
+               title,
+               nav_title,
+               navbar,
+               layout,
+               content,
+               sidebar_content,
+               description,
+               keywords
+       ) select
+               0,
+               0,
+               id,
+               filename,
+               title,
+               nav_title,
+               navbar,
+               layout,
+               content,
+               sidebar_content,
+               description,
+               keywords
+       from cms_pages;
+EOLsql
+);
+}
index 288065f..794f585 100644 (file)
@@ -19,7 +19,7 @@
 
                <header id="site_header">
                        <!--~$admin_links {~-->
-                               <div id="admin_links">You are logged in as an administrator. &nbsp; <a href="admin_pages?~$edit_page_id {~edit_id=~$edit_page_id~~}~~$edit_page_id unset {~&amp;new=1&amp;new_filename=~$basename~~}~">Edit this page</a> &mdash; <a href="admin">Control panel</a> &mdash; <a href="logout">Log out</a></div>
+                               <div id="admin_links">You are logged in as an administrator. &nbsp; <a href="admin_pages?~$edit_page_history_id {~edit_history_id=~$edit_page_history_id~~}~~$edit_page_id {~edit_id=~$edit_page_id~~}~~$edit_page_id unset {~~$edit_page_history_id unset {~&amp;new=1&amp;new_filename=~$basename~~}~~}~">Edit this page</a> &mdash; <a href="admin">Control panel</a> &mdash; <a href="logout">Log out</a></div>
                        <!--~}~-->
                        <!--~$cms_header~-->
                </header>