| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\ContentReview\Forms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Control\Director; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Injector\Injectable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Forms\FormAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Forms\HiddenField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Forms\TextareaField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\ORM\ValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\Security\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ReviewContentHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use Injectable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Parent controller for this form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Form name to use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param Controller       $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string           $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function __construct($controller = null, $name = 'ReviewContentForm') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->controller = $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Bootstrap the form fields for the content review modal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param DataObject $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function Form($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $placeholder = _t(__CLASS__ . '.Placeholder', 'Add comments (optional)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $title = _t(__CLASS__ . '.MarkAsReviewedAction', 'Mark as reviewed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $fields = FieldList::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             HiddenField::create('ID', null, $object->ID), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             HiddenField::create('ClassName', null, $object->baseClass()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             TextareaField::create('Review', '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 ->setAttribute('placeholder', $placeholder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ->setSchemaData(['attributes' => ['placeholder' => $placeholder]]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $action = FormAction::create('savereview', $title) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->setTitle($title) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->setUseButtonTag(false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ->addExtraClass('review-content__action btn btn-primary'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $actions = FieldList::create([$action]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $form = Form::create($this->controller, $this->name, $fields, $actions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->setHTMLID('Form_EditForm_ReviewContent') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ->addExtraClass('form--no-dividers review-content__form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Validate, and save the submitted form's review | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param  DataObject $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param  array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return HTTPResponse|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @throws ValidationException If the user cannot submit the review | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function submitReview($record, $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (!$this->canSubmitReview($record)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             throw new ValidationException(_t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 __CLASS__ . '.ErrorReviewPermissionDenied', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'It seems you don\'t have the necessary permissions to submit a content review' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $notes = (!empty($data['Review']) ? $data['Review'] : _t(__CLASS__ . '.NoComments', '(no comments)')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $record->addReviewNote(Security::getCurrentUser(), $notes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $record->advanceReviewDate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $request = $this->controller->getRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $message = _t(__CLASS__ . '.Success', 'Review successfully added'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($request->getHeader('X-Formschema-Request')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } elseif (Director::is_ajax()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $response = HTTPResponse::create($message, 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $response->addHeader('Content-Type', 'text/html; charset=utf-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $this->controller->redirectBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Determine whether the user can submit a review | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param DataObject $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |     public function canSubmitReview($record) | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |         if (!$record->canEdit() | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |             || !$record->hasMethod('canBeReviewedBy') | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |             || !$record->canBeReviewedBy(Security::getCurrentUser()) | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |         ) { | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |             return false; | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |         return true; | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |