├── .editorconfig ├── LICENSE.md ├── README.md ├── VersionViewerScreenShot.png ├── _config └── config.yml ├── code ├── VersionedRevertDODetailsForm.php └── VersionedRevertModelAdmin.php ├── composer.json ├── css └── styles.css ├── javascript ├── gridfield_versioned_revert.js └── history.js └── templates └── HistoryList.ss /.editorconfig: -------------------------------------------------------------------------------- 1 | # For more information about the properties used in this file, 2 | # please see the EditorConfig documentation: 3 | # http://editorconfig.org 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | indent_size = 4 9 | indent_style = space 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | 13 | [{*.yml,package.json}] 14 | indent_size = 2 15 | 16 | # The indent size used in the package.json file cannot be changed: 17 | # https://github.com/npm/npm/pull/3180#issuecomment-16336516 18 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Bluehouse Group 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the 8 | documentation and/or other materials provided with the distribution. 9 | * Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software 10 | without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 13 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 14 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 15 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 16 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 17 | OF SUCH DAMAGE. 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Data Object Version Viewer 2 | ========================== 3 | ![Screenshot](https://github.com/bluehousegroup/silverstripe-data-object-version-viewer/blob/master/VersionViewerScreenShot.png) 4 | 5 | ### Install with Composer 6 | composer require bluehousegroup/silverstripe-data-object-version-viewer 7 | 8 | ## Usage 9 | 10 | - Extend `silverstripe-versioneddataobjects` to add a 'History' button to a GridField or ModelAdmin 11 | - View, revert to, and publish a previous versions of a data object 12 | 13 | ## Example code 14 | 15 | The implementation is very similar to the versioneddataobjects module, on which this module depends. 16 | 17 | ### Within your DataObject class 18 | 19 | ```php 20 | class Slice extends DataObject 21 | { 22 | private static $db = [ 23 | 'Content' => 'Text' 24 | ]; 25 | 26 | private static $has_one = [ 27 | 'Parent' => 'SiteTree' 28 | ]; 29 | 30 | private static $extensions = [ 31 | 'Heyday\VersionedDataObjects\VersionedDataObject' 32 | ]; 33 | } 34 | ``` 35 | 36 | To use `VersionedDataObject` records in a GridField, `GridFieldDetailForm` needs to be replaced with `VersionedRevertDODetailsForm`: 37 | 38 | ```php 39 | // ... 40 | 41 | public function getCMSFields() 42 | { 43 | $fields = parent::getCMSFields(); 44 | 45 | $fields->addFieldToTab( 46 | 'Root.Slices', 47 | new GridField( 48 | 'Slices', 49 | 'Slices', 50 | $this->Slices(), 51 | $config = GridFieldConfig_RelationEditor::create() 52 | ) 53 | ); 54 | 55 | $config->removeComponentsByType('GridFieldDetailForm'); 56 | $config->addComponent(new VersionedRevertDODetailsForm()); 57 | 58 | return $fields; 59 | } 60 | 61 | // ... 62 | ``` 63 | 64 | ### Versioned DataObjects in a ModelAdmin 65 | 66 | ```php 67 | class SliceAdmin extends VersionedRevertModelAdmin 68 | { 69 | private static $menu_title = 'Slices'; 70 | 71 | private static $url_segment = 'slice'; 72 | 73 | private static $managed_models = [ 74 | 'Slice' 75 | ]; 76 | } 77 | ``` 78 | 79 | ## Notes 80 | 81 | This module is intended to be compatible with the betterbuttons module. Toward that end, it removes the betterbuttons's Versioning button group in order to keep in tact the buttons added by the versiondataobjects plug-in. This change affects only the forms configured with the VersionedRevertDODetailsForm extension. 82 | -------------------------------------------------------------------------------- /VersionViewerScreenShot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bluehousegroup/silverstripe-data-object-version-viewer/f31e9769c7622f98a980209499428d6fe6df26cd/VersionViewerScreenShot.png -------------------------------------------------------------------------------- /_config/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | Name: silverstripe-data-object-version-viewer 3 | --- 4 | # Remove betterbuttons' "Versioning..." group -- it conflicts with versioneddataobjects 5 | BetterButtonsActions: 6 | versioned_edit: 7 | Group_Versioning: false -------------------------------------------------------------------------------- /code/VersionedRevertDODetailsForm.php: -------------------------------------------------------------------------------- 1 | 'history', 23 | '$Action!' => '$Action', 24 | '' => 'edit', 25 | ); 26 | 27 | public function ItemEditForm() { 28 | 29 | Requirements::javascript('silverstripe-data-object-version-viewer/javascript/gridfield_versioned_revert.js'); 30 | 31 | $form = parent::ItemEditForm(); 32 | $form->addExtraClass('do-version-viewer-form'); 33 | // Only add 'History' button for existing records 34 | if ($this->record->exists()) { 35 | $form->Actions()->push(FormAction::create('goHistory','History')); 36 | } 37 | 38 | return $form; 39 | } 40 | 41 | public function goHistory($data, $form) { 42 | $controller = Controller::curr(); 43 | $controller->getResponse()->addHeader("X-Pjax", "Content"); 44 | $controller->redirect(Controller::join_links($this->Link(), 'history')); 45 | } 46 | 47 | /** 48 | * @return Form 49 | */ 50 | public function ItemHistoryForm() { 51 | VersionedReadingMode::setStageReadingMode(); 52 | 53 | $selectedVersionId = $this->request->param('VersionID'); 54 | 55 | $fields = $this->component->getFields(); 56 | if(!$fields) $fields = $this->record->getCMSFields(); 57 | $fields = $fields->makeReadonly(); 58 | 59 | $actions = new FieldList(); 60 | 61 | $form = new Form( 62 | $this, 63 | 'ItemHistoryForm', 64 | $fields, 65 | $actions, 66 | $this->component->getValidator() 67 | ); 68 | 69 | // get the selected version 70 | if ($selectedVersionId) { 71 | $selectedVersion = Versioned::get_version($this->record->ClassName, $this->record->ID, $selectedVersionId); 72 | } 73 | else { 74 | // no version parameter so get the current version. 75 | $selectedVersion = $this->record; 76 | $selectedVersionId = $selectedVersion->Version; 77 | } 78 | 79 | $actions->push( 80 | FormAction::create( 81 | 'goBackToEdit', 82 | 'Back to Edit' 83 | ) 84 | ->setUseButtonTag(true) 85 | ->setAttribute('data-icon', 'back') 86 | ); 87 | 88 | $actions->push( 89 | $rollback = FormAction::create( 90 | 'goRevert', 91 | 'Revert to this version' 92 | ) 93 | ->setUseButtonTag(true) 94 | ); 95 | 96 | // This is hidden with CSS and triggered by the select links using jQuery. 97 | $actions->push( 98 | $selectbtn = FormAction::create( 99 | 'goSelectVersion', 100 | 'Select Version' 101 | ) 102 | ); 103 | 104 | // Don't allow rollback to the version we're already on. 105 | if ($selectedVersionId == $this->record->Version) { 106 | $rollback->setReadonly(true); 107 | } 108 | 109 | $form->loadDataFrom($selectedVersion, Form::MERGE_DEFAULT); 110 | 111 | $versions = $this->record->allVersions(); 112 | 113 | // Build an ArrayList of history ready for rendering in a template. 114 | $historyList = ArrayList::create(); 115 | 116 | foreach($versions as $version) { 117 | 118 | $publishedby = Member::get()->byId($version->PublisherID); 119 | $authoredby = Member::get()->byId($version->AuthorID); 120 | 121 | $lastEditDT = new SS_Datetime(); 122 | $lastEditDT->setValue($version->LastEdited); 123 | 124 | $historyList->push ( 125 | ArrayData::create(array( 126 | 'version' => $version->Version, 127 | 'published_status' => $version->WasPublished ? 'Published' : 'Draft', 128 | 'published_by' => $publishedby ? $publishedby->getName() : '', 129 | 'authored_by' => $authoredby ? $authoredby->getName() : '', 130 | 'last_edit_dt' => $lastEditDT, 131 | 'is_selected' => $version->Version == $selectedVersionId, 132 | )) 133 | ); 134 | } 135 | 136 | $data = new ArrayData(array( 137 | 'historyList' => $historyList, 138 | 'url' => '/' . Controller::join_links($this->Link(), 'history') 139 | )); 140 | 141 | // render the list and add it to the form as literal html. 142 | $fields->push( 143 | LiteralField::create('historyList', $data->renderWith('HistoryList')) 144 | ); 145 | 146 | // we need this for the rollback action. 147 | $fields->push( 148 | HiddenField::create('version_id', '', $selectedVersionId) 149 | ); 150 | 151 | // TODO Coupling with CMS 152 | $toplevelController = $this->getToplevelController(); 153 | // if($toplevelController && $toplevelController instanceof LeftAndMain) { 154 | // Always show with base template (full width, no other panels), 155 | // regardless of overloaded CMS controller templates. 156 | // TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller 157 | $form->setTemplate('LeftAndMain_EditForm'); 158 | $form->addExtraClass('cms-content cms-edit-form center'); 159 | $form->setAttribute('data-pjax-fragment', 'CurrentForm Content'); 160 | if($form->Fields()->hasTabset()) { 161 | $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); 162 | $form->addExtraClass('cms-tabset'); 163 | } 164 | 165 | $form->Backlink = $this->getBackLink(); 166 | // } 167 | 168 | $cb = $this->component->getItemEditFormCallback(); 169 | if($cb) $cb($form, $this); 170 | $this->extend("updateItemHistoryForm", $form); 171 | 172 | VersionedReadingMode::restoreOriginalReadingMode(); 173 | 174 | return $form; 175 | } 176 | 177 | public function history($request) { 178 | $controller = $this->getToplevelController(); 179 | $form = $this->ItemHistoryForm($this->gridField, $request); 180 | 181 | $return = $this->customise(array( 182 | 'Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(), 183 | 'ItemEditForm' => $form, 184 | ))->renderWith($this->template); 185 | 186 | if($request->isAjax()) { 187 | return $return; 188 | } else { 189 | // If not requested by ajax, we need to render it within the controller context+template 190 | return $controller->customise(array( 191 | // TODO CMS coupling 192 | 'Content' => $return, 193 | )); 194 | } 195 | } 196 | 197 | public function goRevert($data, $form) { 198 | // this does basically the same as page revert. 199 | VersionedReadingMode::setStageReadingMode(); 200 | 201 | $selectedVersionId = $data['version_id']; 202 | $this->record->doRollbackTo($selectedVersionId); 203 | 204 | VersionedReadingMode::restoreOriginalReadingMode(); 205 | 206 | $controller = $this->getToplevelController(); 207 | $controller->getResponse()->addHeader("X-Pjax", "Content"); 208 | $controller->redirect(Controller::join_links($this->Link(), 'edit')); 209 | } 210 | 211 | public function goBackToEdit($data, $form) { 212 | $controller = $this->getToplevelController(); 213 | $controller->getResponse()->addHeader("X-Pjax", "Content"); 214 | $controller->redirect(Controller::join_links($this->Link(), 'edit')); 215 | } 216 | 217 | public function goSelectVersion($data, $form) { 218 | $controller = $this->getToplevelController(); 219 | $controller->getResponse()->addHeader("X-Pjax", "Content"); 220 | $url = Controller::join_links($this->Link(), 'history'); 221 | 222 | // Add the version to the end of the URL if we're selecting other than the current version. 223 | if (isset($data['vid']) && $data['vid'] != $this->record->Version) { 224 | $url = Controller::join_links($url, $data['vid']); 225 | } 226 | 227 | $controller->redirect($url); 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /code/VersionedRevertModelAdmin.php: -------------------------------------------------------------------------------- 1 | Fields(); 12 | $grid_field = $form_fields->fieldByName($this->sanitiseClassName($this->modelClass)); 13 | $grid_field->getConfig()->removeComponentsByType('GridFieldDetailForm'); 14 | $grid_field->getConfig()->addComponent(new VersionedRevertDODetailsForm()); 15 | 16 | return $form; 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bluehousegroup/silverstripe-data-object-version-viewer", 3 | "description": "Allows viewing of version history for data objects inside of the SilverStripe CMS", 4 | "homepage": "https://github.com/bluehousegroup/silverstripe-data-object-version-viewer", 5 | "type": "silverstripe-module", 6 | "keywords": ["silverstripe", "data objects", "version"], 7 | "license": "BSD-3-Clause", 8 | "authors": [ 9 | { 10 | "name": "Isaiah Keepin", 11 | "email": "Isaiah@bluehousegroup.com", 12 | "homepage": "http://bluehousegroup.com", 13 | "role": "Developer" 14 | }, 15 | { 16 | "name": "Andrew Rousseau", 17 | "email": "Andrew@bluehousegroup.com", 18 | "homepage": "http://bluehousegroup.com", 19 | "role": "Developer" 20 | }, 21 | { 22 | "name": "Jared Fullerton", 23 | "email": "Jared@bluehousegroup.com", 24 | "homepage": "http://bluehousegroup.com", 25 | "role": "Developer" 26 | } 27 | ], 28 | "require": { 29 | "silverstripe/framework": "~3.1", 30 | "heyday/silverstripe-versioneddataobjects": "^2.0" 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /css/styles.css: -------------------------------------------------------------------------------- 1 | .history-panel { 2 | margin: 0 20px; 3 | border-top: 1px solid #ccc; 4 | } 5 | 6 | .history-list th { 7 | font-weight: bold; 8 | } 9 | 10 | .history-list td, table.history-list th { 11 | padding: 3px 20px; 12 | border-bottom: 1px solid #ccc; 13 | } 14 | 15 | .history-list tr.selected td { 16 | color: #fff; 17 | } 18 | 19 | .history-list tr.selected { 20 | background-color: #888; 21 | } 22 | 23 | #action_goSelectVersion { 24 | display:none; 25 | } 26 | -------------------------------------------------------------------------------- /javascript/gridfield_versioned_revert.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | $.entwine('ss', function($) { 3 | // Adds back in functionality blocked by betterbuttons 4 | $('.do-version-viewer-form.better-buttons-form.cms-edit-form.changed').entwine({ 5 | onmatch: function(e) { 6 | this.find('button[name=action_save]').button('option', 'showingAlternate', true); 7 | this.find('button[name=action_publish]').button('option', 'showingAlternate', true); 8 | this._super(e); 9 | }, 10 | onunmatch: function(e) { 11 | var saveButton = this.find('button[name=action_save]'); 12 | if(saveButton.data('button')) saveButton.button('option', 'showingAlternate', false); 13 | var publishButton = this.find('button[name=action_publish]'); 14 | if(publishButton.data('button')) publishButton.button('option', 'showingAlternate', false); 15 | this._super(e); 16 | } 17 | }); 18 | 19 | }); 20 | })(jQuery); -------------------------------------------------------------------------------- /javascript/history.js: -------------------------------------------------------------------------------- 1 | jQuery(document ).ready(function( $ ) { 2 | // we're using an invisible button to do a standard ajax post. 3 | $('.cms-container').on('click', '.select-version', function(e) { 4 | e.preventDefault(); 5 | var vid = $(this).closest('tr').data('vid'); 6 | $('#vid').val(vid); 7 | $('#action_goSelectVersion').trigger('click'); 8 | }); 9 | 10 | $('.cms-container').on('click', '#action_goRevert', function(e) { 11 | var vid = $('.history-list tr.selected').data('vid'); 12 | return confirm('Revert to version ' + vid + '?'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /templates/HistoryList.ss: -------------------------------------------------------------------------------- 1 | <% require css(silverstripe-data-object-version-viewer/css/styles.css) %> 2 | <% require javascript(silverstripe-data-object-version-viewer/javascript/history.js) %> 3 | 4 |
5 |

History

6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | <% loop $historyList %> 17 | class="selected"<% end_if %>> 18 | 19 | 20 | 21 | <% end_loop %> 22 | 23 | 24 |
VerStatusPublished byAuthorLast editedSelect
$version $published_status $published_by $authored_by $last_edit_dt.NiceUS $last_edit_dt.Time<% if not $is_selected %>Select<% end_if %>
25 |
26 | 27 | --------------------------------------------------------------------------------