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
|
|
|
), |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
private static $summary_fields = array( |
|
|
|
|
36
|
|
|
'Title' => 'Title', |
37
|
|
|
'Documents.Count' => 'No. Documents' |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned. |
42
|
|
|
* |
43
|
|
|
* You can attach an extension to this event: |
44
|
|
|
* |
45
|
|
|
* <code> |
46
|
|
|
* public function updateDocuments($document) |
47
|
|
|
* { |
48
|
|
|
* // do something |
49
|
|
|
* } |
50
|
|
|
* </code> |
51
|
|
|
* |
52
|
|
|
* @return DataList|null |
53
|
|
|
*/ |
54
|
|
|
public function getDocuments() |
55
|
|
|
{ |
56
|
|
|
$documents = $this->Documents(); |
|
|
|
|
57
|
|
|
$this->extend('updateDocuments', $documents); |
58
|
|
|
return $documents; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Put the "documents" list into the main tab instead of its own tab, and replace the default "Add Document" button |
63
|
|
|
* with a customised button for DMS documents |
64
|
|
|
* |
65
|
|
|
* @return FieldList |
66
|
|
|
*/ |
67
|
|
|
public function getCMSFields() |
68
|
|
|
{ |
69
|
|
|
// PHP 5.3 only |
70
|
|
|
$self = $this; |
71
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { |
72
|
|
|
$fields->removeFieldsFromTab( |
73
|
|
|
'Root.Main', |
74
|
|
|
array('KeyValuePairs', 'SortBy', 'SortByDirection') |
75
|
|
|
); |
76
|
|
|
// Don't put the GridField for documents in until the set has been created |
77
|
|
|
if (!$self->isInDB()) { |
78
|
|
|
$fields->addFieldToTab( |
79
|
|
|
'Root.Main', |
80
|
|
|
LiteralField::create( |
81
|
|
|
'GridFieldNotice', |
82
|
|
|
'<p class="message warning">' . _t( |
83
|
|
|
'DMSDocumentSet.GRIDFIELD_NOTICE', |
84
|
|
|
'Managing documents will be available once you have created this document set.' |
85
|
|
|
) . '</p>' |
86
|
|
|
), |
87
|
|
|
'Title' |
88
|
|
|
); |
89
|
|
|
} else { |
90
|
|
|
// Document listing |
91
|
|
|
$gridFieldConfig = GridFieldConfig::create() |
92
|
|
|
->addComponents( |
93
|
|
|
new GridFieldButtonRow('before'), |
94
|
|
|
new GridFieldToolbarHeader(), |
95
|
|
|
new GridFieldFilterHeader(), |
96
|
|
|
new GridFieldSortableHeader(), |
97
|
|
|
new GridFieldDataColumns(), |
98
|
|
|
new GridFieldEditButton(), |
99
|
|
|
// Special delete dialog to handle custom behaviour of unlinking and deleting |
100
|
|
|
new GridFieldDeleteAction(true), |
101
|
|
|
new GridFieldDetailForm() |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) { |
105
|
|
|
$paginatorComponent = new GridFieldPaginatorWithShowAll(15); |
106
|
|
|
} else { |
107
|
|
|
$paginatorComponent = new GridFieldPaginator(15); |
108
|
|
|
} |
109
|
|
|
$gridFieldConfig->addComponent($paginatorComponent); |
110
|
|
|
|
111
|
|
|
if (class_exists('GridFieldSortableRows')) { |
112
|
|
|
$sortableComponent = new GridFieldSortableRows('DocumentSort'); |
113
|
|
|
// setUsePagination method removed from newer version of SortableGridField. |
114
|
|
|
if (method_exists($sortableComponent, 'setUsePagination')) { |
115
|
|
|
$sortableComponent->setUsePagination(false)->setForceRedraw(true); |
116
|
|
|
} |
117
|
|
|
$gridFieldConfig->addComponent($sortableComponent); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDataColumns') |
|
|
|
|
121
|
|
|
->setDisplayFields($self->getDocumentDisplayFields()) |
122
|
|
|
->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) |
123
|
|
|
->setFieldFormatting( |
124
|
|
|
array( |
125
|
|
|
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\'' |
126
|
|
|
. ' href=\'$Link\'>$FilenameWithoutID</a>', |
127
|
|
|
'ManuallyAdded' => function ($value) { |
128
|
|
|
if ($value) { |
129
|
|
|
return _t('DMSDocumentSet.MANUAL', 'Manually'); |
130
|
|
|
} |
131
|
|
|
return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder'); |
132
|
|
|
} |
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
// Override delete functionality with this class |
137
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDetailForm') |
|
|
|
|
138
|
|
|
->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); |
139
|
|
|
$gridField = GridField::create( |
140
|
|
|
'Documents', |
141
|
|
|
false, |
142
|
|
|
$self->Documents(), |
|
|
|
|
143
|
|
|
$gridFieldConfig |
144
|
|
|
); |
145
|
|
|
$gridField->setModelClass('DMSDocument'); |
146
|
|
|
$gridField->addExtraClass('documents'); |
147
|
|
|
|
148
|
|
|
$gridFieldConfig->addComponent( |
149
|
|
|
$addNewButton = new DMSGridFieldAddNewButton('buttons-before-left'), |
150
|
|
|
'GridFieldExportButton' |
151
|
|
|
); |
152
|
|
|
$addNewButton->setDocumentSetId($self->ID); |
153
|
|
|
|
154
|
|
|
$fields->removeByName('Documents'); |
155
|
|
|
$fields->addFieldsToTab( |
156
|
|
|
'Root.Main', |
157
|
|
|
array( |
158
|
|
|
$gridField, |
159
|
|
|
HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()) |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
$self->addQueryFields($fields); |
163
|
|
|
} |
164
|
|
|
}); |
165
|
|
|
$this->addRequirements(); |
166
|
|
|
return parent::getCMSFields(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Add required CSS and Javascript requirements for managing documents |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
protected function addRequirements() |
175
|
|
|
{ |
176
|
|
|
// Javascript to customize the grid field for the DMS document (overriding entwine |
177
|
|
|
// in FRAMEWORK_DIR.'/javascript/GridField.js' |
178
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js'); |
179
|
|
|
Requirements::css(DMS_DIR . '/dist/css/dmsbundle.css'); |
180
|
|
|
|
181
|
|
|
// Javascript for the link editor pop-up in TinyMCE |
182
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js'); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Adds the query fields to build the document logic to the DMSDocumentSet. |
189
|
|
|
* |
190
|
|
|
* To extend use the following from within an Extension subclass: |
191
|
|
|
* |
192
|
|
|
* <code> |
193
|
|
|
* public function updateQueryFields($result) |
194
|
|
|
* { |
195
|
|
|
* // Do something here |
196
|
|
|
* } |
197
|
|
|
* </code> |
198
|
|
|
* |
199
|
|
|
* @param FieldList $fields |
200
|
|
|
*/ |
201
|
|
|
public function addQueryFields($fields) |
202
|
|
|
{ |
203
|
|
|
/** @var DMSDocument $doc */ |
204
|
|
|
$doc = singleton('DMSDocument'); |
205
|
|
|
/** @var FormField $field */ |
206
|
|
|
$dmsDocFields = $doc->scaffoldSearchFields(array('fieldClasses' => true)); |
207
|
|
|
$membersMap = Member::get()->map('ID', 'Name')->toArray(); |
208
|
|
|
asort($membersMap); |
209
|
|
|
|
210
|
|
|
foreach ($dmsDocFields as $field) { |
211
|
|
|
if ($field instanceof ListboxField) { |
212
|
|
|
$map = ($field->getName() === 'Tags__ID') ? $doc->getAllTagsMap() : $membersMap; |
|
|
|
|
213
|
|
|
$field->setMultiple(true)->setSource($map); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
$keyValPairs = DMSJsonField::create('KeyValuePairs', $dmsDocFields->toArray()); |
217
|
|
|
|
218
|
|
|
// Now lastly add the sort fields |
219
|
|
|
$sortedBy = FieldGroup::create('SortedBy', array( |
220
|
|
|
DropdownField::create('SortBy', '', array( |
221
|
|
|
'LastEdited' => 'Last changed', |
222
|
|
|
'Created' => 'Created', |
223
|
|
|
'Title' => 'Document title', |
224
|
|
|
), 'LastEdited'), |
225
|
|
|
DropdownField::create('SortByDirection', '', $this->dbObject('SortByDirection')->enumValues(), 'DESC'), |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
$sortedBy->setTitle(_t('DMSDocumentSet.SORTED_BY', 'Sort the document set by:')); |
229
|
|
|
$fields->addFieldsToTab('Root.QueryBuilder', array($keyValPairs, $sortedBy)); |
230
|
|
|
$this->extend('updateQueryFields', $fields); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function onBeforeWrite() |
234
|
|
|
{ |
235
|
|
|
parent::onBeforeWrite(); |
236
|
|
|
|
237
|
|
|
$this->saveLinkedDocuments(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned. |
242
|
|
|
*/ |
243
|
|
|
public function saveLinkedDocuments() |
244
|
|
|
{ |
245
|
|
|
if (empty($this->KeyValuePairs) || !$this->isChanged('KeyValuePairs')) { |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$keyValuesPair = Convert::json2array($this->KeyValuePairs); |
250
|
|
|
|
251
|
|
|
/** @var DMSDocument $dmsDoc */ |
252
|
|
|
$dmsDoc = singleton('DMSDocument'); |
253
|
|
|
$context = $dmsDoc->getDefaultSearchContext(); |
254
|
|
|
|
255
|
|
|
$sortBy = $this->SortBy ? $this->SortBy : 'LastEdited'; |
256
|
|
|
$sortByDirection = $this->SortByDirection ? $this->SortByDirection : 'DESC'; |
257
|
|
|
$sortedBy = sprintf('%s %s', $sortBy, $sortByDirection); |
258
|
|
|
|
259
|
|
|
/** @var DataList $documents */ |
260
|
|
|
$documents = $context->getResults($keyValuesPair, $sortedBy); |
|
|
|
|
261
|
|
|
$documents = $this->addEmbargoConditions($documents); |
262
|
|
|
$documents = $this->addQueryBuilderSearchResults($documents); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Add embargo date conditions to a search query |
267
|
|
|
* |
268
|
|
|
* @param DataList $documents |
269
|
|
|
* @return DataList |
270
|
|
|
*/ |
271
|
|
|
protected function addEmbargoConditions(DataList $documents) |
272
|
|
|
{ |
273
|
|
|
$now = SS_Datetime::now()->Rfc2822(); |
274
|
|
|
|
275
|
|
|
return $documents->where( |
276
|
|
|
"\"EmbargoedIndefinitely\" = 0 AND " |
277
|
|
|
. " \"EmbargoedUntilPublished\" = 0 AND " |
278
|
|
|
. "(\"EmbargoedUntilDate\" IS NULL OR " |
279
|
|
|
. "(\"EmbargoedUntilDate\" IS NOT NULL AND '{$now}' >= \"EmbargoedUntilDate\")) AND " |
280
|
|
|
. "\"ExpireAtDate\" IS NULL OR (\"ExpireAtDate\" IS NOT NULL AND '{$now}' < \"ExpireAtDate\")" |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Remove all ManuallyAdded = 0 original results and add in the new documents returned by the search context |
286
|
|
|
* |
287
|
|
|
* @param DataList $documents |
288
|
|
|
* @return DataList |
289
|
|
|
*/ |
290
|
|
|
protected function addQueryBuilderSearchResults(DataList $documents) |
291
|
|
|
{ |
292
|
|
|
/** @var ManyManyList $originals Documents that belong to just this set. */ |
293
|
|
|
$originals = $this->Documents(); |
|
|
|
|
294
|
|
|
$originals->removeByFilter('"ManuallyAdded" = 0'); |
295
|
|
|
|
296
|
|
|
foreach ($documents as $document) { |
297
|
|
|
$originals->add($document, array('ManuallyAdded' => 0)); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $originals; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Customise the display fields for the documents GridField |
305
|
|
|
* |
306
|
|
|
* @return array |
307
|
|
|
*/ |
308
|
|
|
public function getDocumentDisplayFields() |
309
|
|
|
{ |
310
|
|
|
return array_merge( |
311
|
|
|
(array) DMSDocument::create()->config()->get('display_fields'), |
312
|
|
|
array('ManuallyAdded' => _t('DMSDocumentSet.ADDEDMETHOD', 'Added')) |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
protected function validate() |
317
|
|
|
{ |
318
|
|
|
$result = parent::validate(); |
319
|
|
|
|
320
|
|
|
if (!$this->getTitle()) { |
321
|
|
|
$result->error(_t('DMSDocumentSet.VALIDATION_NO_TITLE', '\'Title\' is required.')); |
322
|
|
|
} |
323
|
|
|
return $result; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
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.