|
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
|
|
|
*/ |
|
84
|
|
|
public function submitReview($record, $data) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!$record->canEdit() |
|
87
|
|
|
|| !$record->hasMethod('canBeReviewedBy') |
|
88
|
|
|
|| !$record->canBeReviewedBy(Security::getCurrentUser()) |
|
89
|
|
|
) { |
|
90
|
|
|
throw new ValidationException(_t( |
|
91
|
|
|
__CLASS__ . '.ErrorReviewPermissionDenied', |
|
92
|
|
|
'It seems you don\'t have the necessary permissions to submit a content review' |
|
93
|
|
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$notes = (!empty($data['Review']) ? $data['Review'] : _t(__CLASS__ . '.NoComments', '(no comments)')); |
|
97
|
|
|
$record->addReviewNote(Security::getCurrentUser(), $notes); |
|
98
|
|
|
$record->advanceReviewDate(); |
|
99
|
|
|
|
|
100
|
|
|
$request = $this->controller->getRequest(); |
|
101
|
|
|
$message = _t(__CLASS__ . '.Success', 'Review successfully added'); |
|
102
|
|
|
|
|
103
|
|
|
if ($request->getHeader('X-Formschema-Request')) { |
|
104
|
|
|
return $message; |
|
105
|
|
|
} elseif (Director::is_ajax()) { |
|
106
|
|
|
$response = HTTPResponse::create($message, 200); |
|
107
|
|
|
$response->addHeader('Content-Type', 'text/html; charset=utf-8'); |
|
108
|
|
|
return $response; |
|
109
|
|
|
} else { |
|
110
|
|
|
return $this->controller->redirectBack(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|