1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\ContactAdmin\BulkActions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\View\ArrayData; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\FormAction; |
11
|
|
|
use SilverStripe\TagField\TagField; |
12
|
|
|
use SilverStripe\Admin\LeftAndMain; |
13
|
|
|
use SilverStripe\Forms\HiddenField; |
14
|
|
|
use SilverStripe\Control\Controller; |
15
|
|
|
use SilverStripe\Forms\DropdownField; |
16
|
|
|
use SilverStripe\ORM\ValidationResult; |
17
|
|
|
use SilverStripe\Control\HTTPResponse; |
18
|
|
|
use SilverStripe\Core\Injector\Injector; |
19
|
|
|
use SilverStripe\Control\PjaxResponseNegotiator; |
20
|
|
|
use SilverCommerce\ContactAdmin\Model\ContactTag; |
21
|
|
|
use SilverCommerce\ContactAdmin\Model\ContactList; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Bulk action handler that adds selected records to a list |
25
|
|
|
* |
26
|
|
|
* @author ilateral |
27
|
|
|
* @package Contacts |
28
|
|
|
*/ |
29
|
|
|
class AddTagsHandler extends AddRelatedHandler |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
private static $url_segment = 'addtags'; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Front-end label for this handler's action |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $label = 'Add Tags'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* RequestHandler allowed actions |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private static $allowed_actions = [ |
|
|
|
|
46
|
|
|
'index', |
47
|
|
|
'Form' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return i18n localized front-end label |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getI18nLabel() |
56
|
|
|
{ |
57
|
|
|
return _t(__CLASS__ . '.Label', $this->getLabel()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates and return the editing interface |
62
|
|
|
* |
63
|
|
|
* @return string Form's HTML |
64
|
|
|
*/ |
65
|
|
|
public function index() |
66
|
|
|
{ |
67
|
|
|
$leftandmain = Injector::inst()->create(LeftAndMain::class); |
68
|
|
|
|
69
|
|
|
$form = $this->Form(); |
70
|
|
|
$form->setTemplate($leftandmain->getTemplatesWithSuffix('_EditForm')); |
71
|
|
|
$form->addExtraClass('cms-edit-form center cms-content'); |
72
|
|
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content'); |
73
|
|
|
|
74
|
|
|
if ($this->request->isAjax()) { |
75
|
|
|
$response = new HTTPResponse( |
76
|
|
|
Convert::raw2json(['Content' => $form->forAjaxTemplate()->getValue()]) |
|
|
|
|
77
|
|
|
); |
78
|
|
|
$response->addHeader('X-Pjax', 'Content'); |
79
|
|
|
$response->addHeader('Content-Type', 'text/json'); |
80
|
|
|
$response->addHeader('X-Title', 'SilverStripe - Bulk ' . $this->gridField->list->dataClass . ' Editing'); |
81
|
|
|
|
82
|
|
|
return $response; |
83
|
|
|
} else { |
84
|
|
|
$controller = $this->getToplevelController(); |
85
|
|
|
return $controller->customise(['Content' => $form]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return a form with a dropdown to select the list you want to use |
91
|
|
|
* |
92
|
|
|
* @return Form |
93
|
|
|
*/ |
94
|
|
|
public function Form() |
95
|
|
|
{ |
96
|
|
|
$crumbs = $this->Breadcrumbs(); |
97
|
|
|
|
98
|
|
|
if ($crumbs && $crumbs->count()>=2) { |
99
|
|
|
$one_level_up = $crumbs->offsetGet($crumbs->count()-2); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$record_ids = ""; |
103
|
|
|
$query_string = ""; |
104
|
|
|
$recordList = $this->getRecordIDList(); |
105
|
|
|
|
106
|
|
|
foreach ($this->getRecordIDList() as $id) { |
107
|
|
|
$record_ids .= $id . ','; |
108
|
|
|
$query_string .= "records[]={$id}&"; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Cut off the last 2 parts of the string |
112
|
|
|
$record_ids = substr($record_ids, 0, -1); |
113
|
|
|
$query_string = substr($query_string, 0, -1); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$form = new Form( |
116
|
|
|
$this, |
117
|
|
|
'Form', |
118
|
|
|
$fields = FieldList::create( |
119
|
|
|
HiddenField::create("RecordIDs", "", $record_ids), |
120
|
|
|
TagField::create( |
121
|
|
|
'Tags', |
122
|
|
|
null, |
123
|
|
|
ContactTag::get() |
124
|
|
|
)->setDescription(_t( |
125
|
|
|
"Contacts.TagDescription", |
126
|
|
|
"List of tags related to this contact, seperated by a comma." |
127
|
|
|
))->setShouldLazyLoad(true) |
128
|
|
|
), |
129
|
|
|
$actions = FieldList::create( |
130
|
|
|
FormAction::create('doAddTags', _t("Contacts.Add", 'Add')) |
131
|
|
|
->setAttribute('id', 'bulkEditingSaveBtn') |
132
|
|
|
->addExtraClass('btn btn-success') |
133
|
|
|
->setAttribute('data-icon', 'accept') |
134
|
|
|
->setUseButtonTag(true), |
135
|
|
|
FormAction::create('Cancel', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.CANCEL_BTN_LABEL', 'Cancel')) |
136
|
|
|
->setAttribute('id', 'bulkEditingUpdateCancelBtn') |
137
|
|
|
->addExtraClass('btn btn-danger cms-panel-link') |
138
|
|
|
->setAttribute('data-icon', 'decline') |
139
|
|
|
->setAttribute('href', $one_level_up->Link) |
|
|
|
|
140
|
|
|
->setUseButtonTag(true) |
141
|
|
|
->setAttribute('src', '') |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
if ($crumbs && $crumbs->count() >= 2) { |
146
|
|
|
$form->Backlink = $one_level_up->Link; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// override form action URL back to bulkEditForm |
150
|
|
|
// and add record ids GET var |
151
|
|
|
$form->setFormAction( |
152
|
|
|
$this->Link('Form?records[]='.implode('&', $recordList)) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
return $form; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Saves the changes made in the bulk edit into the dataObject |
160
|
|
|
* |
161
|
|
|
* @return HTTPResponse |
162
|
|
|
*/ |
163
|
|
|
public function doAddTags($data, /** @scrutinizer ignore-unused */ $form) |
164
|
|
|
{ |
165
|
|
|
$className = $this->gridField->list->dataClass; |
166
|
|
|
$controller = $this->getToplevelController(); |
167
|
|
|
$form = $controller->EditForm(); |
168
|
|
|
$return = []; |
169
|
|
|
|
170
|
|
|
if (isset($data['RecordIDs'])) { |
171
|
|
|
$ids = explode(",", $data['RecordIDs']); |
172
|
|
|
} else { |
173
|
|
|
$ids = []; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$tags_list = (isset($data['Tags'])) ? $data['Tags'] : []; |
177
|
|
|
|
178
|
|
|
try { |
179
|
|
|
foreach ($tags_list as $tag_name) { |
180
|
|
|
if (!empty($tag_name)) { |
181
|
|
|
$tag = ContactTag::get()->find("Title", $tag_name); |
182
|
|
|
if (!$tag) { |
183
|
|
|
$tag = ContactTag::create([ |
184
|
|
|
"Title" => $tag_name |
185
|
|
|
]); |
186
|
|
|
$tag->write(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
foreach ($ids as $record_id) { |
190
|
|
|
$record = DataObject::get_by_id($className, $record_id); |
191
|
|
|
|
192
|
|
|
if ($record->hasMethod("Tags")) { |
193
|
|
|
$record->Tags()->add($tag); |
194
|
|
|
$return[] = $record->ID; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} catch (\Exception $e) { |
200
|
|
|
$form->sessionMessage( |
201
|
|
|
$e->getMessage(), |
202
|
|
|
ValidationResult::TYPE_ERROR |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$responseNegotiator = new PjaxResponseNegotiator([ |
206
|
|
|
'CurrentForm' => function () use (&$form) { |
207
|
|
|
return $form->forTemplate(); |
208
|
|
|
}, |
209
|
|
|
'default' => function () use (&$controller) { |
210
|
|
|
return $controller->redirectBack(); |
211
|
|
|
} |
212
|
|
|
]); |
213
|
|
|
|
214
|
|
|
if ($controller->getRequest()->isAjax()) { |
215
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $responseNegotiator->respond($controller->getRequest()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (count($tags_list)) { |
222
|
|
|
$message = "Added " . count($return) . " contacts to tags '" . implode(",", $tags_list) . "'"; |
223
|
|
|
} else { |
224
|
|
|
$message = _t("Contacts.NoListSelected", "No list selected"); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$form->sessionMessage( |
228
|
|
|
$message, |
229
|
|
|
ValidationResult::TYPE_GOOD |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
// Changes to the record properties might've excluded the record from |
233
|
|
|
// a filtered list, so return back to the main view if it can't be found |
234
|
|
|
$link = $controller->Link(); |
235
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); |
236
|
|
|
return $controller->redirect($link); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|