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
|
|
|
|
13
|
|
|
private static $db = array( |
|
|
|
|
14
|
|
|
'Title' => 'Varchar(255)', |
15
|
|
|
'KeyValuePairs' => 'Text', |
16
|
|
|
'SortBy' => "Enum('LastEdited,Created,Title')')", |
17
|
|
|
'SortByDirection' => "Enum('DESC,ASC')')", |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
private static $has_one = array( |
|
|
|
|
21
|
|
|
'Page' => 'SiteTree', |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
private static $many_many = array( |
|
|
|
|
25
|
|
|
'Documents' => 'DMSDocument', |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
private static $many_many_extraFields = array( |
|
|
|
|
29
|
|
|
'Documents' => array( |
30
|
|
|
'BelongsToSet' => 'Boolean(1)', // Flag indicating if a document was added directly to a set - in which case it is set - or added via the query-builder. |
31
|
|
|
), |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned. |
36
|
|
|
* |
37
|
|
|
* You can attach an extension to this event: |
38
|
|
|
* |
39
|
|
|
* <code> |
40
|
|
|
* public function updateDocuments($document) |
41
|
|
|
* { |
42
|
|
|
* // do something |
43
|
|
|
* } |
44
|
|
|
* </code> |
45
|
|
|
* |
46
|
|
|
* @return DataList|null |
47
|
|
|
*/ |
48
|
|
|
public function getDocuments() |
49
|
|
|
{ |
50
|
|
|
$documents = $this->Documents(); |
|
|
|
|
51
|
|
|
$this->extend('updateDocuments', $documents); |
52
|
|
|
return $documents; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Put the "documents" list into the main tab instead of its own tab, and replace the default "Add Document" button |
57
|
|
|
* with a customised button for DMS documents |
58
|
|
|
* |
59
|
|
|
* @return FieldList |
60
|
|
|
*/ |
61
|
|
|
public function getCMSFields() |
62
|
|
|
{ |
63
|
|
|
// PHP 5.3 only |
64
|
|
|
$self = $this; |
65
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { |
66
|
|
|
$fields->removeFieldsFromTab( |
67
|
|
|
'Root.Main', |
68
|
|
|
array('KeyValuePairs', 'SortBy', 'SortByDirection') |
69
|
|
|
); |
70
|
|
|
// Don't put the GridField for documents in until the set has been created |
71
|
|
|
if (!$self->isInDB()) { |
72
|
|
|
$fields->addFieldToTab( |
73
|
|
|
'Root.Main', |
74
|
|
|
LiteralField::create( |
75
|
|
|
'GridFieldNotice', |
76
|
|
|
'<p class="message warning">' . _t( |
77
|
|
|
'DMSDocumentSet.GRIDFIELD_NOTICE', |
78
|
|
|
'Managing documents will be available once you have created this document set.' |
79
|
|
|
) . '</p>' |
80
|
|
|
), |
81
|
|
|
'Title' |
82
|
|
|
); |
83
|
|
|
} else { |
84
|
|
|
// Document listing |
85
|
|
|
$gridFieldConfig = GridFieldConfig::create() |
86
|
|
|
->addComponents( |
87
|
|
|
new GridFieldToolbarHeader(), |
88
|
|
|
new GridFieldFilterHeader(), |
89
|
|
|
new GridFieldSortableHeader(), |
90
|
|
|
new GridFieldDataColumns(), |
91
|
|
|
new GridFieldEditButton(), |
92
|
|
|
// Special delete dialog to handle custom behaviour of unlinking and deleting |
93
|
|
|
new DMSGridFieldDeleteAction(), |
94
|
|
|
new GridFieldDetailForm() |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) { |
98
|
|
|
$paginatorComponent = new GridFieldPaginatorWithShowAll(15); |
99
|
|
|
} else { |
100
|
|
|
$paginatorComponent = new GridFieldPaginator(15); |
101
|
|
|
} |
102
|
|
|
$gridFieldConfig->addComponent($paginatorComponent); |
103
|
|
|
|
104
|
|
|
if (class_exists('GridFieldSortableRows')) { |
105
|
|
|
$sortableComponent = new GridFieldSortableRows('DocumentSort'); |
106
|
|
|
// setUsePagination method removed from newer version of SortableGridField. |
107
|
|
|
if (method_exists($sortableComponent, 'setUsePagination')) { |
108
|
|
|
$sortableComponent->setUsePagination(false)->setForceRedraw(true); |
109
|
|
|
} |
110
|
|
|
$gridFieldConfig->addComponent($sortableComponent); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDataColumns') |
|
|
|
|
114
|
|
|
->setDisplayFields(DMSDocument::create()->config()->get('display_fields')) |
115
|
|
|
->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) |
116
|
|
|
->setFieldFormatting( |
117
|
|
|
array( |
118
|
|
|
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>$FilenameWithoutID</a>', |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
// Override delete functionality with this class |
123
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDetailForm') |
|
|
|
|
124
|
|
|
->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); |
125
|
|
|
$gridField = GridField::create( |
126
|
|
|
'Documents', |
127
|
|
|
false, |
128
|
|
|
$self->Documents(), |
|
|
|
|
129
|
|
|
$gridFieldConfig |
130
|
|
|
); |
131
|
|
|
$gridField->setModelClass('DMSDocument'); |
132
|
|
|
$gridField->addExtraClass('documents'); |
133
|
|
|
|
134
|
|
|
$gridFieldConfig->addComponent( |
135
|
|
|
$addNewButton = new DMSGridFieldAddNewButton, |
136
|
|
|
'GridFieldExportButton' |
137
|
|
|
); |
138
|
|
|
$addNewButton->setDocumentSetId($self->ID); |
139
|
|
|
|
140
|
|
|
$fields->removeByName('Documents'); |
141
|
|
|
$fields->addFieldToTab('Root.Main', $gridField); |
142
|
|
|
$self->addQueryFields($fields); |
143
|
|
|
} |
144
|
|
|
}); |
145
|
|
|
$this->addRequirements(); |
146
|
|
|
return parent::getCMSFields(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Add required CSS and Javascript requirements for managing documents |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
protected function addRequirements() |
155
|
|
|
{ |
156
|
|
|
// Javascript to customize the grid field for the DMS document (overriding entwine |
157
|
|
|
// in FRAMEWORK_DIR.'/javascript/GridField.js' |
158
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js'); |
159
|
|
|
Requirements::css(DMS_DIR . '/dist/css/dmsbundle.css'); |
160
|
|
|
|
161
|
|
|
// Javascript for the link editor pop-up in TinyMCE |
162
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js'); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Adds the query fields to build the document logic to the DMSDocumentSet. |
169
|
|
|
* |
170
|
|
|
* To extend use the following from within an Extension subclass: |
171
|
|
|
* |
172
|
|
|
* <code> |
173
|
|
|
* public function updateQueryFields($result) |
174
|
|
|
* { |
175
|
|
|
* // Do something here |
176
|
|
|
* } |
177
|
|
|
* </code> |
178
|
|
|
* |
179
|
|
|
* @param FieldList $fields |
180
|
|
|
*/ |
181
|
|
|
public function addQueryFields($fields) |
182
|
|
|
{ |
183
|
|
|
/** @var DMSDocument $doc */ |
184
|
|
|
$doc = singleton('DMSDocument'); |
185
|
|
|
/** @var FormField $field */ |
186
|
|
|
$dmsDocFields = $doc->scaffoldSearchFields(array('fieldClasses' => true)); |
187
|
|
|
$membersMap = Member::get()->map('ID', 'Name')->toArray(); |
188
|
|
|
asort($membersMap); |
189
|
|
|
foreach ($dmsDocFields as $field) { |
190
|
|
|
// Apply field customisations where necessary |
191
|
|
|
if (in_array($field->getName(), array('CreatedByID', 'LastEditedByID', 'LastEditedByID'))) { |
192
|
|
|
/** @var ListboxField $field */ |
193
|
|
|
$field->setMultiple(true)->setSource($membersMap); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
$keyValPairs = JsonField::create('KeyValuePairs', $dmsDocFields->toArray()); |
197
|
|
|
|
198
|
|
|
// Now lastly add the sort fields |
199
|
|
|
$sortedBy = FieldGroup::create('SortedBy', array( |
200
|
|
|
DropdownField::create('SortBy', '', array( |
201
|
|
|
'LastEdited' => 'Last changed', |
202
|
|
|
'Created' => 'Created', |
203
|
|
|
'Title' => 'Document title', |
204
|
|
|
), 'LastEdited'), |
205
|
|
|
DropdownField::create('SortByDirection', '', $this->dbObject('SortByDirection')->enumValues(), 'DESC'), |
206
|
|
|
)); |
207
|
|
|
|
208
|
|
|
$sortedBy->setTitle(_t('DMSDocumentSet.SORTED_BY', 'Sort the document set by:')); |
209
|
|
|
$fields->addFieldsToTab('Root.QueryBuilder', array($keyValPairs, $sortedBy)); |
210
|
|
|
$this->extend('updateQueryFields', $fields); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function onBeforeWrite() |
214
|
|
|
{ |
215
|
|
|
parent::onBeforeWrite(); |
216
|
|
|
|
217
|
|
|
$this->saveLinkedDocuments(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned. |
222
|
|
|
* |
223
|
|
|
* @return ArrayList|null |
224
|
|
|
*/ |
225
|
|
|
public function saveLinkedDocuments() |
226
|
|
|
{ |
227
|
|
|
// Documents that belong to just this set. |
228
|
|
|
/** @var ManyManyList $originals */ |
229
|
|
|
$originals = $this->Documents(); |
|
|
|
|
230
|
|
|
if (!(empty($this->KeyValuePairs)) && $this->isChanged('KeyValuePairs')) { |
231
|
|
|
$keyValuesPair = Convert::json2array($this->KeyValuePairs); |
232
|
|
|
/** @var DMSDocument $dmsDoc */ |
233
|
|
|
$dmsDoc = singleton('DMSDocument'); |
234
|
|
|
$context = $dmsDoc->getDefaultSearchContext(); |
235
|
|
|
|
236
|
|
|
$sortBy = $this->SortBy ? $this->SortBy : 'LastEdited'; |
237
|
|
|
$sortByDirection = $this->SortByDirection ? $this->SortByDirection : 'DESC'; |
238
|
|
|
$sortedBy = sprintf('%s %s', $sortBy, $sortByDirection); |
239
|
|
|
/** @var DataList $documents */ |
240
|
|
|
$documents = $context->getResults($keyValuesPair, $sortedBy); |
|
|
|
|
241
|
|
|
$now = SS_Datetime::now()->Rfc2822(); |
242
|
|
|
$documents = $documents->where( |
243
|
|
|
"\"EmbargoedIndefinitely\" = 0 AND ". |
244
|
|
|
" \"EmbargoedUntilPublished\" = 0 AND ". |
245
|
|
|
"(\"EmbargoedUntilDate\" IS NULL OR " . |
246
|
|
|
"(\"EmbargoedUntilDate\" IS NOT NULL AND '{$now}' >= \"EmbargoedUntilDate\")) AND " . |
247
|
|
|
"\"ExpireAtDate\" IS NULL OR (\"ExpireAtDate\" IS NOT NULL AND '{$now}' < \"ExpireAtDate\")" |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
// Remove all BelongsToSet as the rules have changed |
251
|
|
|
$originals->removeByFilter('"BelongsToSet" = 0'); |
252
|
|
|
foreach ($documents as $document) { |
253
|
|
|
$originals->add($document, array('BelongsToSet' => 0)); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
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.