1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A document set is attached to Pages, and contains many DMSDocuments |
4
|
|
|
* |
5
|
|
|
* @property Varchar Title |
6
|
|
|
* @property Text KeyValuePairs |
7
|
|
|
* @property Enum SortBy |
8
|
|
|
* @property Enum SortByDirection |
9
|
|
|
*/ |
10
|
|
|
class DMSDocumentSet extends DataObject |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
private static $db = array( |
|
|
|
|
13
|
|
|
'Title' => 'Varchar(255)', |
14
|
|
|
'KeyValuePairs' => 'Text', |
15
|
|
|
'SortBy' => "Enum('LastEdited,Created,Title')')", |
16
|
|
|
'SortByDirection' => "Enum('DESC,ASC')')", |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
private static $has_one = array( |
|
|
|
|
20
|
|
|
'Page' => 'SiteTree', |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $many_many = array( |
|
|
|
|
24
|
|
|
'Documents' => 'DMSDocument', |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $many_many_extraFields = array( |
|
|
|
|
28
|
|
|
'Documents' => array( |
29
|
|
|
// Flag indicating if a document was added directly to a set - in which case it is set - or added |
30
|
|
|
// via the query-builder. |
31
|
|
|
'ManuallyAdded' => 'Boolean(1)', |
32
|
|
|
'DocumentSort' => 'Int' |
33
|
|
|
), |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
private static $summary_fields = array( |
|
|
|
|
37
|
|
|
'Title' => 'Title', |
38
|
|
|
'Documents.Count' => 'No. Documents' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned. |
43
|
|
|
* |
44
|
|
|
* You can attach an extension to this event: |
45
|
|
|
* |
46
|
|
|
* <code> |
47
|
|
|
* public function updateDocuments($document) |
48
|
|
|
* { |
49
|
|
|
* // do something |
50
|
|
|
* } |
51
|
|
|
* </code> |
52
|
|
|
* |
53
|
|
|
* @return DataList|null |
54
|
|
|
*/ |
55
|
|
|
public function getDocuments() |
56
|
|
|
{ |
57
|
|
|
$documents = $this->Documents(); |
|
|
|
|
58
|
|
|
$this->extend('updateDocuments', $documents); |
59
|
|
|
return $documents; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Put the "documents" list into the main tab instead of its own tab, and replace the default "Add Document" button |
64
|
|
|
* with a customised button for DMS documents |
65
|
|
|
* |
66
|
|
|
* @return FieldList |
67
|
|
|
*/ |
68
|
|
|
public function getCMSFields() |
69
|
|
|
{ |
70
|
|
|
// PHP 5.3 only |
71
|
|
|
$self = $this; |
72
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { |
73
|
|
|
$fields->removeFieldsFromTab( |
74
|
|
|
'Root.Main', |
75
|
|
|
array('KeyValuePairs', 'SortBy', 'SortByDirection') |
76
|
|
|
); |
77
|
|
|
// Don't put the GridField for documents in until the set has been created |
78
|
|
|
if (!$self->isInDB()) { |
79
|
|
|
$fields->addFieldToTab( |
80
|
|
|
'Root.Main', |
81
|
|
|
LiteralField::create( |
82
|
|
|
'GridFieldNotice', |
83
|
|
|
'<p class="message warning">' . _t( |
84
|
|
|
'DMSDocumentSet.GRIDFIELD_NOTICE', |
85
|
|
|
'Managing documents will be available once you have created this document set.' |
86
|
|
|
) . '</p>' |
87
|
|
|
), |
88
|
|
|
'Title' |
89
|
|
|
); |
90
|
|
|
} else { |
91
|
|
|
$fields->removeByName('DocumentSetSort'); |
92
|
|
|
// Document listing |
93
|
|
|
$gridFieldConfig = GridFieldConfig::create() |
94
|
|
|
->addComponents( |
95
|
|
|
new GridFieldButtonRow('before'), |
96
|
|
|
new GridFieldToolbarHeader(), |
97
|
|
|
new GridFieldFilterHeader(), |
98
|
|
|
new GridFieldSortableHeader(), |
99
|
|
|
new GridFieldDataColumns(), |
100
|
|
|
new GridFieldEditButton(), |
101
|
|
|
// Special delete dialog to handle custom behaviour of unlinking and deleting |
102
|
|
|
new GridFieldDeleteAction(true), |
103
|
|
|
new GridFieldDetailForm() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) { |
107
|
|
|
$paginatorComponent = new GridFieldPaginatorWithShowAll(15); |
108
|
|
|
} else { |
109
|
|
|
$paginatorComponent = new GridFieldPaginator(15); |
110
|
|
|
} |
111
|
|
|
$gridFieldConfig->addComponent($paginatorComponent); |
112
|
|
|
|
113
|
|
|
if (class_exists('GridFieldSortableRows')) { |
114
|
|
|
$gridFieldConfig->addComponent(new GridFieldSortableRows('DocumentSort')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Don't show which page this is if we're already editing within a page context |
118
|
|
|
if (Controller::curr() instanceof CMSPageEditController) { |
119
|
|
|
$fields->removeByName('PageID'); |
120
|
|
|
} else { |
121
|
|
|
$fields->fieldByName('Root.Main.PageID')->setTitle(_t('DMSDocumentSet.SHOWONPAGE', 'Show on page')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDataColumns') |
|
|
|
|
125
|
|
|
->setDisplayFields($self->getDocumentDisplayFields()) |
126
|
|
|
->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) |
127
|
|
|
->setFieldFormatting( |
128
|
|
|
array( |
129
|
|
|
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\'' |
130
|
|
|
. ' href=\'$Link\'>$FilenameWithoutID</a>', |
131
|
|
|
'ManuallyAdded' => function ($value) { |
132
|
|
|
if ($value) { |
133
|
|
|
return _t('DMSDocumentSet.MANUAL', 'Manually'); |
134
|
|
|
} |
135
|
|
|
return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder'); |
136
|
|
|
} |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
// Override delete functionality with this class |
141
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDetailForm') |
|
|
|
|
142
|
|
|
->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); |
143
|
|
|
$gridField = GridField::create( |
144
|
|
|
'Documents', |
145
|
|
|
false, |
146
|
|
|
$self->Documents(), |
|
|
|
|
147
|
|
|
$gridFieldConfig |
148
|
|
|
); |
149
|
|
|
$gridField->setModelClass('DMSDocument'); |
150
|
|
|
$gridField->addExtraClass('documents'); |
151
|
|
|
|
152
|
|
|
$gridFieldConfig->addComponent( |
153
|
|
|
$addNewButton = new DMSGridFieldAddNewButton('buttons-before-left'), |
154
|
|
|
'GridFieldExportButton' |
155
|
|
|
); |
156
|
|
|
$addNewButton->setDocumentSetId($self->ID); |
157
|
|
|
|
158
|
|
|
$fields->removeByName('Documents'); |
159
|
|
|
$fields->addFieldsToTab( |
160
|
|
|
'Root.Main', |
161
|
|
|
array( |
162
|
|
|
$gridField, |
163
|
|
|
HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()) |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
$self->addQueryFields($fields); |
167
|
|
|
} |
168
|
|
|
}); |
169
|
|
|
$this->addRequirements(); |
170
|
|
|
return parent::getCMSFields(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add required CSS and Javascript requirements for managing documents |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
protected function addRequirements() |
179
|
|
|
{ |
180
|
|
|
// Javascript to customize the grid field for the DMS document (overriding entwine |
181
|
|
|
// in FRAMEWORK_DIR.'/javascript/GridField.js' |
182
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js'); |
183
|
|
|
Requirements::css(DMS_DIR . '/dist/css/dmsbundle.css'); |
184
|
|
|
|
185
|
|
|
// Javascript for the link editor pop-up in TinyMCE |
186
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js'); |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Adds the query fields to build the document logic to the DMSDocumentSet. |
193
|
|
|
* |
194
|
|
|
* @param FieldList $fields |
195
|
|
|
*/ |
196
|
|
|
public function addQueryFields($fields) |
197
|
|
|
{ |
198
|
|
|
/** @var DMSDocument $doc */ |
199
|
|
|
$doc = singleton('DMSDocument'); |
200
|
|
|
/** @var FormField $field */ |
201
|
|
|
$dmsDocFields = $doc->scaffoldSearchFields(array('fieldClasses' => true)); |
202
|
|
|
$membersMap = Member::get()->map('ID', 'Name')->toArray(); |
203
|
|
|
asort($membersMap); |
204
|
|
|
|
205
|
|
|
foreach ($dmsDocFields as $field) { |
206
|
|
|
if ($field instanceof ListboxField) { |
207
|
|
|
$map = ($field->getName() === 'Tags__ID') ? $doc->getAllTagsMap() : $membersMap; |
|
|
|
|
208
|
|
|
$field->setMultiple(true)->setSource($map); |
209
|
|
|
|
210
|
|
|
if ($field->getName() === 'Tags__ID') { |
211
|
|
|
$field->setRightTitle( |
212
|
|
|
_t( |
213
|
|
|
'DMSDocumentSet.TAGS_RIGHT_TITLE', |
214
|
|
|
'Tags can be set in the taxonomy area, and can be assigned when editing a document.' |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
$keyValPairs = DMSJsonField::create('KeyValuePairs', $dmsDocFields->toArray()); |
221
|
|
|
|
222
|
|
|
// Now lastly add the sort fields |
223
|
|
|
$sortedBy = FieldGroup::create('SortedBy', array( |
224
|
|
|
DropdownField::create('SortBy', '', array( |
225
|
|
|
'LastEdited' => 'Last changed', |
226
|
|
|
'Created' => 'Created', |
227
|
|
|
'Title' => 'Document title', |
228
|
|
|
), 'LastEdited'), |
229
|
|
|
DropdownField::create( |
230
|
|
|
'SortByDirection', |
231
|
|
|
'', |
232
|
|
|
array( |
233
|
|
|
'DESC' => _t('DMSDocumentSet.DIRECTION_DESCENDING', 'Descending'), |
234
|
|
|
'ASC' => _t('DMSDocumentSet.DIRECTION_ASCENDING', 'Ascending') |
235
|
|
|
), |
236
|
|
|
'DESC' |
237
|
|
|
), |
238
|
|
|
)); |
239
|
|
|
|
240
|
|
|
$sortedBy->setTitle(_t('DMSDocumentSet.SORTED_BY', 'Sort the document set by:')); |
241
|
|
|
$fields->addFieldsToTab( |
242
|
|
|
'Root.QueryBuilder', |
243
|
|
|
array( |
244
|
|
|
LiteralField::create( |
245
|
|
|
'GridFieldNotice', |
246
|
|
|
'<p class="message warning">' . _t( |
247
|
|
|
'DMSDocumentSet.QUERY_BUILDER_NOTICE', |
248
|
|
|
'The query builder provides the ability to add documents to a document set based on the ' . |
249
|
|
|
'filters below. Please note that the set will be built using this criteria when you save the ' . |
250
|
|
|
'form. This set will not be dynamically updated (see the documentation for more information).' |
251
|
|
|
) . '</p>' |
252
|
|
|
), |
253
|
|
|
$keyValPairs, |
254
|
|
|
$sortedBy |
255
|
|
|
) |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function onBeforeWrite() |
260
|
|
|
{ |
261
|
|
|
parent::onBeforeWrite(); |
262
|
|
|
|
263
|
|
|
$this->saveLinkedDocuments(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned. |
268
|
|
|
*/ |
269
|
|
|
public function saveLinkedDocuments() |
270
|
|
|
{ |
271
|
|
|
if (empty($this->KeyValuePairs) || !$this->isChanged('KeyValuePairs')) { |
272
|
|
|
return; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$keyValuesPair = Convert::json2array($this->KeyValuePairs); |
276
|
|
|
|
277
|
|
|
/** @var DMSDocument $dmsDoc */ |
278
|
|
|
$dmsDoc = singleton('DMSDocument'); |
279
|
|
|
$context = $dmsDoc->getDefaultSearchContext(); |
280
|
|
|
|
281
|
|
|
$sortBy = $this->SortBy ? $this->SortBy : 'LastEdited'; |
282
|
|
|
$sortByDirection = $this->SortByDirection ? $this->SortByDirection : 'DESC'; |
283
|
|
|
$sortedBy = sprintf('%s %s', $sortBy, $sortByDirection); |
284
|
|
|
|
285
|
|
|
/** @var DataList $documents */ |
286
|
|
|
$documents = $context->getResults($keyValuesPair, $sortedBy); |
|
|
|
|
287
|
|
|
$documents = $this->addEmbargoConditions($documents); |
288
|
|
|
$documents = $this->addQueryBuilderSearchResults($documents); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Add embargo date conditions to a search query |
293
|
|
|
* |
294
|
|
|
* @param DataList $documents |
295
|
|
|
* @return DataList |
296
|
|
|
*/ |
297
|
|
|
protected function addEmbargoConditions(DataList $documents) |
298
|
|
|
{ |
299
|
|
|
$now = SS_Datetime::now()->Rfc2822(); |
300
|
|
|
|
301
|
|
|
return $documents->where( |
302
|
|
|
"\"EmbargoedIndefinitely\" = 0 AND " |
303
|
|
|
. " \"EmbargoedUntilPublished\" = 0 AND " |
304
|
|
|
. "(\"EmbargoedUntilDate\" IS NULL OR " |
305
|
|
|
. "(\"EmbargoedUntilDate\" IS NOT NULL AND '{$now}' >= \"EmbargoedUntilDate\")) AND " |
306
|
|
|
. "\"ExpireAtDate\" IS NULL OR (\"ExpireAtDate\" IS NOT NULL AND '{$now}' < \"ExpireAtDate\")" |
307
|
|
|
); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Remove all ManuallyAdded = 0 original results and add in the new documents returned by the search context |
312
|
|
|
* |
313
|
|
|
* @param DataList $documents |
314
|
|
|
* @return DataList |
315
|
|
|
*/ |
316
|
|
|
protected function addQueryBuilderSearchResults(DataList $documents) |
317
|
|
|
{ |
318
|
|
|
/** @var ManyManyList $originals Documents that belong to just this set. */ |
319
|
|
|
$originals = $this->Documents(); |
|
|
|
|
320
|
|
|
$originals->removeByFilter('"ManuallyAdded" = 0'); |
321
|
|
|
|
322
|
|
|
foreach ($documents as $document) { |
323
|
|
|
$originals->add($document, array('ManuallyAdded' => 0)); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return $originals; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Customise the display fields for the documents GridField |
331
|
|
|
* |
332
|
|
|
* @return array |
333
|
|
|
*/ |
334
|
|
|
public function getDocumentDisplayFields() |
335
|
|
|
{ |
336
|
|
|
return array_merge( |
337
|
|
|
(array) DMSDocument::create()->config()->get('display_fields'), |
338
|
|
|
array('ManuallyAdded' => _t('DMSDocumentSet.ADDEDMETHOD', 'Added')) |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
protected function validate() |
343
|
|
|
{ |
344
|
|
|
$result = parent::validate(); |
345
|
|
|
|
346
|
|
|
if (!$this->getTitle()) { |
347
|
|
|
$result->error(_t('DMSDocumentSet.VALIDATION_NO_TITLE', '\'Title\' is required.')); |
348
|
|
|
} |
349
|
|
|
return $result; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function canView($member = null) |
353
|
|
|
{ |
354
|
|
|
return $this->getDMSDocumentSetPermissions($member); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function canCreate($member = null) |
358
|
|
|
{ |
359
|
|
|
return $this->getDMSDocumentSetPermissions($member); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function canEdit($member = null) |
363
|
|
|
{ |
364
|
|
|
return $this->getDMSDocumentSetPermissions($member); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function canDelete($member = null) |
368
|
|
|
{ |
369
|
|
|
return $this->getDMSDocumentSetPermissions($member); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Checks if a then given (or logged in) member is either an ADMIN, SITETREE_EDIT_ALL or has access |
374
|
|
|
* to the DMSDocumentAdmin module, in which case permissions is granted. |
375
|
|
|
* |
376
|
|
|
* To extend use the following from within an Extension subclass: |
377
|
|
|
* |
378
|
|
|
* <code> |
379
|
|
|
* public function updateDMSDocumentSetPermissions($result) |
380
|
|
|
* { |
381
|
|
|
* // Do something here |
382
|
|
|
* } |
383
|
|
|
* </code> |
384
|
|
|
* @param Member $member |
|
|
|
|
385
|
|
|
* @return bool |
386
|
|
|
*/ |
387
|
|
|
public function getDMSDocumentSetPermissions(Member $member = null) |
388
|
|
|
{ |
389
|
|
View Code Duplication |
if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) { |
|
|
|
|
390
|
|
|
$member = Member::currentUser(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$result = ($member && |
394
|
|
|
Permission::checkMember( |
395
|
|
|
$member, |
396
|
|
|
array('ADMIN', 'SITETREE_EDIT_ALL', 'CMS_ACCESS_DMSDocumentAdmin') |
397
|
|
|
) |
398
|
|
|
); |
399
|
|
|
|
400
|
|
|
$this->extend('updateDMSDocumentSetPermissions', $result); |
401
|
|
|
return (bool) $result; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.