| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\VersionedAdmin\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Admin\LeftAndMain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Admin\LeftAndMainFormRequestHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Forms\FormFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Versioned\Versioned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\VersionedAdmin\Forms\DataObjectVersionFormFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class HistoryViewerController extends LeftAndMain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private static $url_segment = 'historyviewer'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private static $url_rule = '/$Action'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private static $url_priority = 10; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private static $allowed_actions = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'versionForm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'schema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     public function getClientConfig() | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $clientConfig = parent::getClientConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $clientConfig['form']['versionForm'] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             'schemaUrl' => $this->owner->Link('schema/versionForm') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         return $clientConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Gets a JSON schema representing the current version detail form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * WARNING: Experimental API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function schema($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $formName = $request->param('FormName'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($formName !== 'versionForm') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return parent::schema($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // Get schema for history form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         // @todo Eventually all form scaffolding will be based on context rather than record ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // See https://github.com/silverstripe/silverstripe-framework/issues/6362 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $form = $this->getVersionForm([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'RecordClass' => $request->getVar('RecordClass'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'RecordID' => $request->getVar('RecordID'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'RecordVersion' => $request->getVar('RecordVersion'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // Respond with this schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $response = $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $response->addHeader('Content-Type', 'application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $schemaID = $this->getRequest()->getURL(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this->getSchemaResponse($schemaID, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function getVersionForm(array $context) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Check context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (!isset($context['RecordClass'], $context['RecordID'], $context['RecordVersion'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new InvalidArgumentException('Missing RecordID / RecordVersion / RecordClass for this form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $recordClass = $context['RecordClass']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $recordId = $context['RecordID']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $recordVersion = $context['RecordVersion']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (!$recordClass || !$recordId || !$recordVersion) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->jsonError(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // Load record and perform a canView check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $record = Versioned::get_version($recordClass, $recordId, $recordVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (!$record) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->jsonError(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if (!$record->canView()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $this->jsonError(403, _t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 __CLASS__.'.ErrorItemViewPermissionDenied', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 "You don't have the necessary permissions to view {ObjectTitle}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 ['ObjectTitle' => $record->i18n_singular_name()] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $effectiveContext = array_merge($context, ['Record' => $record]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         /** @var FormFactory $scaffolder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $scaffolder = Injector::inst()->get(DataObjectVersionFormFactory::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $form = $scaffolder->getForm($this, 'versionForm', $effectiveContext); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         // Set form handler with class name, ID and VersionID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $form->setRequestHandler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             LeftAndMainFormRequestHandler::create($form, [$recordClass, $recordId, $recordVersion]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function versionForm(HTTPRequest $request = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         if (!$request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $this->jsonError(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $recordClass = $request->getVar('RecordClass'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $recordId = $request->getVar('RecordID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $recordVersion = $request->getVar('RecordVersion'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if (!$recordClass || !$recordId || !$recordVersion) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $this->jsonError(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return $this->getVersionForm([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             'RecordClass' => $recordClass, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             'RecordID' => $recordId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             'RecordVersion' => $recordVersion, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |