2017-05-02 04:49:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A document set is attached to Pages, and contains many DMSDocuments
|
2017-05-08 13:57:52 +02:00
|
|
|
*
|
|
|
|
* @property Varchar Title
|
|
|
|
* @property Text KeyValuePairs
|
|
|
|
* @property Enum SortBy
|
|
|
|
* @property Enum SortByDirection
|
2017-05-02 04:49:41 +02:00
|
|
|
*/
|
|
|
|
class DMSDocumentSet extends DataObject
|
|
|
|
{
|
|
|
|
private static $db = array(
|
2017-05-08 13:57:52 +02:00
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'KeyValuePairs' => 'Text',
|
|
|
|
'SortBy' => "Enum('LastEdited,Created,Title')')",
|
|
|
|
'SortByDirection' => "Enum('DESC,ASC')')",
|
2017-05-02 04:49:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
2017-05-08 13:57:52 +02:00
|
|
|
'Page' => 'SiteTree',
|
2017-05-02 04:49:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
2017-05-08 13:57:52 +02:00
|
|
|
'Documents' => 'DMSDocument',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
'Documents' => array(
|
2017-05-17 00:58:51 +02:00
|
|
|
// Flag indicating if a document was added directly to a set - in which case it is set - or added
|
|
|
|
// via the query-builder.
|
|
|
|
'ManuallyAdded' => 'Boolean(1)',
|
2017-06-06 06:14:14 +02:00
|
|
|
'DocumentSort' => 'Int'
|
2017-05-08 13:57:52 +02:00
|
|
|
),
|
2017-05-02 04:49:41 +02:00
|
|
|
);
|
|
|
|
|
2017-05-22 04:06:06 +02:00
|
|
|
private static $summary_fields = array(
|
|
|
|
'Title' => 'Title',
|
|
|
|
'Documents.Count' => 'No. Documents'
|
|
|
|
);
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned.
|
|
|
|
*
|
|
|
|
* You can attach an extension to this event:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* public function updateDocuments($document)
|
|
|
|
* {
|
|
|
|
* // do something
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
2017-05-08 13:57:52 +02:00
|
|
|
* @return DataList|null
|
2017-05-02 04:49:41 +02:00
|
|
|
*/
|
|
|
|
public function getDocuments()
|
|
|
|
{
|
|
|
|
$documents = $this->Documents();
|
|
|
|
$this->extend('updateDocuments', $documents);
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put the "documents" list into the main tab instead of its own tab, and replace the default "Add Document" button
|
|
|
|
* with a customised button for DMS documents
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-05-09 00:13:13 +02:00
|
|
|
// PHP 5.3 only
|
|
|
|
$self = $this;
|
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) {
|
2017-05-08 13:57:52 +02:00
|
|
|
$fields->removeFieldsFromTab(
|
|
|
|
'Root.Main',
|
|
|
|
array('KeyValuePairs', 'SortBy', 'SortByDirection')
|
|
|
|
);
|
2017-05-09 00:13:13 +02:00
|
|
|
// Don't put the GridField for documents in until the set has been created
|
|
|
|
if (!$self->isInDB()) {
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
LiteralField::create(
|
|
|
|
'GridFieldNotice',
|
|
|
|
'<p class="message warning">' . _t(
|
|
|
|
'DMSDocumentSet.GRIDFIELD_NOTICE',
|
|
|
|
'Managing documents will be available once you have created this document set.'
|
|
|
|
) . '</p>'
|
|
|
|
),
|
|
|
|
'Title'
|
|
|
|
);
|
2017-05-02 04:49:41 +02:00
|
|
|
} else {
|
2017-06-06 06:14:14 +02:00
|
|
|
$fields->removeByName('DocumentSetSort');
|
2017-05-08 13:57:52 +02:00
|
|
|
// Document listing
|
|
|
|
$gridFieldConfig = GridFieldConfig::create()
|
|
|
|
->addComponents(
|
2017-05-22 04:06:06 +02:00
|
|
|
new GridFieldButtonRow('before'),
|
2017-05-08 13:57:52 +02:00
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldFilterHeader(),
|
|
|
|
new GridFieldSortableHeader(),
|
|
|
|
new GridFieldDataColumns(),
|
|
|
|
new GridFieldEditButton(),
|
|
|
|
// Special delete dialog to handle custom behaviour of unlinking and deleting
|
2017-05-17 00:58:51 +02:00
|
|
|
new GridFieldDeleteAction(true),
|
2017-05-08 13:57:52 +02:00
|
|
|
new GridFieldDetailForm()
|
|
|
|
);
|
2017-05-02 04:49:41 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) {
|
|
|
|
$paginatorComponent = new GridFieldPaginatorWithShowAll(15);
|
|
|
|
} else {
|
|
|
|
$paginatorComponent = new GridFieldPaginator(15);
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|
2017-05-08 13:57:52 +02:00
|
|
|
$gridFieldConfig->addComponent($paginatorComponent);
|
2017-05-02 04:49:41 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
if (class_exists('GridFieldSortableRows')) {
|
2017-06-06 06:14:14 +02:00
|
|
|
$gridFieldConfig->addComponent(new GridFieldSortableRows('DocumentSort'));
|
2017-05-08 13:57:52 +02:00
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
|
2017-06-06 01:40:33 +02:00
|
|
|
// Don't show which page this is if we're already editing within a page context
|
|
|
|
if (Controller::curr() instanceof CMSPageEditController) {
|
|
|
|
$fields->removeByName('PageID');
|
|
|
|
} else {
|
|
|
|
$fields->fieldByName('Root.Main.PageID')->setTitle(_t('DMSDocumentSet.SHOWONPAGE', 'Show on page'));
|
|
|
|
}
|
2017-06-02 01:36:27 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDataColumns')
|
2017-05-18 04:44:28 +02:00
|
|
|
->setDisplayFields($self->getDocumentDisplayFields())
|
2017-05-08 13:57:52 +02:00
|
|
|
->setFieldCasting(array('LastEdited' => 'Datetime->Ago'))
|
|
|
|
->setFieldFormatting(
|
|
|
|
array(
|
2017-06-02 05:50:07 +02:00
|
|
|
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\''
|
|
|
|
. ' href=\'$Link\'>$FilenameWithoutID</a>',
|
2017-05-17 00:58:51 +02:00
|
|
|
'ManuallyAdded' => function ($value) {
|
2017-05-18 04:44:28 +02:00
|
|
|
if ($value) {
|
|
|
|
return _t('DMSDocumentSet.MANUAL', 'Manually');
|
|
|
|
}
|
|
|
|
return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder');
|
|
|
|
}
|
2017-05-08 13:57:52 +02:00
|
|
|
)
|
|
|
|
);
|
2017-05-02 04:49:41 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
// Override delete functionality with this class
|
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDetailForm')
|
|
|
|
->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest');
|
|
|
|
$gridField = GridField::create(
|
|
|
|
'Documents',
|
|
|
|
false,
|
|
|
|
$self->Documents(),
|
|
|
|
$gridFieldConfig
|
|
|
|
);
|
|
|
|
$gridField->setModelClass('DMSDocument');
|
|
|
|
$gridField->addExtraClass('documents');
|
2017-05-02 04:49:41 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
$gridFieldConfig->addComponent(
|
2017-05-22 04:06:06 +02:00
|
|
|
$addNewButton = new DMSGridFieldAddNewButton('buttons-before-left'),
|
2017-05-08 13:57:52 +02:00
|
|
|
'GridFieldExportButton'
|
|
|
|
);
|
|
|
|
$addNewButton->setDocumentSetId($self->ID);
|
2017-05-02 04:49:41 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
$fields->removeByName('Documents');
|
2017-05-17 07:24:50 +02:00
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Main',
|
|
|
|
array(
|
|
|
|
$gridField,
|
|
|
|
HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey())
|
|
|
|
)
|
|
|
|
);
|
2017-05-08 13:57:52 +02:00
|
|
|
$self->addQueryFields($fields);
|
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
});
|
2017-05-09 00:13:13 +02:00
|
|
|
$this->addRequirements();
|
2017-05-02 04:49:41 +02:00
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
2017-05-09 00:13:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add required CSS and Javascript requirements for managing documents
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
protected function addRequirements()
|
|
|
|
{
|
|
|
|
// Javascript to customize the grid field for the DMS document (overriding entwine
|
|
|
|
// in FRAMEWORK_DIR.'/javascript/GridField.js'
|
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js');
|
2017-05-09 05:53:52 +02:00
|
|
|
Requirements::css(DMS_DIR . '/dist/css/dmsbundle.css');
|
2017-05-09 00:13:13 +02:00
|
|
|
|
|
|
|
// Javascript for the link editor pop-up in TinyMCE
|
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2017-05-08 13:57:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the query fields to build the document logic to the DMSDocumentSet.
|
|
|
|
*
|
|
|
|
* @param FieldList $fields
|
|
|
|
*/
|
|
|
|
public function addQueryFields($fields)
|
|
|
|
{
|
|
|
|
/** @var DMSDocument $doc */
|
|
|
|
$doc = singleton('DMSDocument');
|
|
|
|
/** @var FormField $field */
|
|
|
|
$dmsDocFields = $doc->scaffoldSearchFields(array('fieldClasses' => true));
|
|
|
|
$membersMap = Member::get()->map('ID', 'Name')->toArray();
|
|
|
|
asort($membersMap);
|
2017-05-17 00:58:51 +02:00
|
|
|
|
2017-05-08 13:57:52 +02:00
|
|
|
foreach ($dmsDocFields as $field) {
|
2017-05-17 00:58:51 +02:00
|
|
|
if ($field instanceof ListboxField) {
|
|
|
|
$map = ($field->getName() === 'Tags__ID') ? $doc->getAllTagsMap() : $membersMap;
|
|
|
|
$field->setMultiple(true)->setSource($map);
|
2017-06-06 23:31:08 +02:00
|
|
|
|
|
|
|
if ($field->getName() === 'Tags__ID') {
|
|
|
|
$field->setRightTitle(
|
|
|
|
_t(
|
|
|
|
'DMSDocumentSet.TAGS_RIGHT_TITLE',
|
|
|
|
'Tags can be set in the taxonomy area, and can be assigned when editing a document.'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-05-08 13:57:52 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 04:29:51 +02:00
|
|
|
$keyValPairs = DMSJsonField::create('KeyValuePairs', $dmsDocFields->toArray());
|
2017-05-08 13:57:52 +02:00
|
|
|
|
|
|
|
// Now lastly add the sort fields
|
|
|
|
$sortedBy = FieldGroup::create('SortedBy', array(
|
2017-05-17 00:58:51 +02:00
|
|
|
DropdownField::create('SortBy', '', array(
|
|
|
|
'LastEdited' => 'Last changed',
|
|
|
|
'Created' => 'Created',
|
|
|
|
'Title' => 'Document title',
|
|
|
|
), 'LastEdited'),
|
2017-06-06 06:53:52 +02:00
|
|
|
DropdownField::create(
|
|
|
|
'SortByDirection',
|
|
|
|
'',
|
|
|
|
array(
|
|
|
|
'DESC' => _t('DMSDocumentSet.DIRECTION_DESCENDING', 'Descending'),
|
|
|
|
'ASC' => _t('DMSDocumentSet.DIRECTION_ASCENDING', 'Ascending')
|
|
|
|
),
|
|
|
|
'DESC'
|
|
|
|
),
|
2017-05-17 00:58:51 +02:00
|
|
|
));
|
2017-05-08 13:57:52 +02:00
|
|
|
|
|
|
|
$sortedBy->setTitle(_t('DMSDocumentSet.SORTED_BY', 'Sort the document set by:'));
|
2017-06-06 23:31:08 +02:00
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.QueryBuilder',
|
|
|
|
array(
|
|
|
|
LiteralField::create(
|
|
|
|
'GridFieldNotice',
|
|
|
|
'<p class="message warning">' . _t(
|
|
|
|
'DMSDocumentSet.QUERY_BUILDER_NOTICE',
|
|
|
|
'The query builder provides the ability to add documents to a document set based on the ' .
|
|
|
|
'filters below. Please note that the set will be built using this criteria when you save the ' .
|
|
|
|
'form. This set will not be dynamically updated (see the documentation for more information).'
|
|
|
|
) . '</p>'
|
|
|
|
),
|
|
|
|
$keyValPairs,
|
|
|
|
$sortedBy
|
|
|
|
)
|
|
|
|
);
|
2017-05-08 13:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
|
|
|
$this->saveLinkedDocuments();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a list of the documents in this set. An extension hook is provided before the result is returned.
|
|
|
|
*/
|
|
|
|
public function saveLinkedDocuments()
|
|
|
|
{
|
2017-05-17 00:58:51 +02:00
|
|
|
if (empty($this->KeyValuePairs) || !$this->isChanged('KeyValuePairs')) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-08 13:57:52 +02:00
|
|
|
|
2017-05-17 00:58:51 +02:00
|
|
|
$keyValuesPair = Convert::json2array($this->KeyValuePairs);
|
2017-05-08 13:57:52 +02:00
|
|
|
|
2017-05-17 00:58:51 +02:00
|
|
|
/** @var DMSDocument $dmsDoc */
|
|
|
|
$dmsDoc = singleton('DMSDocument');
|
|
|
|
$context = $dmsDoc->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$sortBy = $this->SortBy ? $this->SortBy : 'LastEdited';
|
|
|
|
$sortByDirection = $this->SortByDirection ? $this->SortByDirection : 'DESC';
|
|
|
|
$sortedBy = sprintf('%s %s', $sortBy, $sortByDirection);
|
|
|
|
|
|
|
|
/** @var DataList $documents */
|
|
|
|
$documents = $context->getResults($keyValuesPair, $sortedBy);
|
|
|
|
$documents = $this->addEmbargoConditions($documents);
|
|
|
|
$documents = $this->addQueryBuilderSearchResults($documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add embargo date conditions to a search query
|
|
|
|
*
|
|
|
|
* @param DataList $documents
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function addEmbargoConditions(DataList $documents)
|
|
|
|
{
|
|
|
|
$now = SS_Datetime::now()->Rfc2822();
|
|
|
|
|
|
|
|
return $documents->where(
|
|
|
|
"\"EmbargoedIndefinitely\" = 0 AND "
|
|
|
|
. " \"EmbargoedUntilPublished\" = 0 AND "
|
|
|
|
. "(\"EmbargoedUntilDate\" IS NULL OR "
|
|
|
|
. "(\"EmbargoedUntilDate\" IS NOT NULL AND '{$now}' >= \"EmbargoedUntilDate\")) AND "
|
|
|
|
. "\"ExpireAtDate\" IS NULL OR (\"ExpireAtDate\" IS NOT NULL AND '{$now}' < \"ExpireAtDate\")"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all ManuallyAdded = 0 original results and add in the new documents returned by the search context
|
|
|
|
*
|
|
|
|
* @param DataList $documents
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function addQueryBuilderSearchResults(DataList $documents)
|
|
|
|
{
|
|
|
|
/** @var ManyManyList $originals Documents that belong to just this set. */
|
|
|
|
$originals = $this->Documents();
|
|
|
|
$originals->removeByFilter('"ManuallyAdded" = 0');
|
|
|
|
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
$originals->add($document, array('ManuallyAdded' => 0));
|
2017-05-08 13:57:52 +02:00
|
|
|
}
|
2017-05-17 00:58:51 +02:00
|
|
|
|
|
|
|
return $originals;
|
2017-05-08 13:57:52 +02:00
|
|
|
}
|
2017-05-18 04:44:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Customise the display fields for the documents GridField
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDocumentDisplayFields()
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
(array) DMSDocument::create()->config()->get('display_fields'),
|
2017-05-17 00:58:51 +02:00
|
|
|
array('ManuallyAdded' => _t('DMSDocumentSet.ADDEDMETHOD', 'Added'))
|
2017-05-18 04:44:28 +02:00
|
|
|
);
|
|
|
|
}
|
2017-06-02 05:50:07 +02:00
|
|
|
|
|
|
|
protected function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
|
|
|
|
if (!$this->getTitle()) {
|
|
|
|
$result->error(_t('DMSDocumentSet.VALIDATION_NO_TITLE', '\'Title\' is required.'));
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|