'Varchar(255)',
'KeyValuePairs' => 'Text',
'SortBy' => "Enum('LastEdited,Created,Title')')",
'SortByDirection' => "Enum('DESC,ASC')')",
);
private static $has_one = array(
'Page' => 'SiteTree',
);
private static $many_many = array(
'Documents' => 'DMSDocument',
);
private static $many_many_extraFields = array(
'Documents' => array(
// 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)',
),
);
private static $summary_fields = array(
'Title' => 'Title',
'Documents.Count' => 'No. Documents'
);
/**
* 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:
*
*
* public function updateDocuments($document)
* {
* // do something
* }
*
*
* @return DataList|null
*/
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()
{
// PHP 5.3 only
$self = $this;
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) {
$fields->removeFieldsFromTab(
'Root.Main',
array('KeyValuePairs', 'SortBy', 'SortByDirection')
);
// Don't put the GridField for documents in until the set has been created
if (!$self->isInDB()) {
$fields->addFieldToTab(
'Root.Main',
LiteralField::create(
'GridFieldNotice',
'
* public function updateQueryFields($result)
* {
* // Do something here
* }
*
*
* @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);
foreach ($dmsDocFields as $field) {
if ($field instanceof ListboxField) {
$map = ($field->getName() === 'Tags__ID') ? $doc->getAllTagsMap() : $membersMap;
$field->setMultiple(true)->setSource($map);
}
}
$keyValPairs = DMSJsonField::create('KeyValuePairs', $dmsDocFields->toArray());
// Now lastly add the sort fields
$sortedBy = FieldGroup::create('SortedBy', array(
DropdownField::create('SortBy', '', array(
'LastEdited' => 'Last changed',
'Created' => 'Created',
'Title' => 'Document title',
), 'LastEdited'),
DropdownField::create('SortByDirection', '', $this->dbObject('SortByDirection')->enumValues(), 'DESC'),
));
$sortedBy->setTitle(_t('DMSDocumentSet.SORTED_BY', 'Sort the document set by:'));
$fields->addFieldsToTab('Root.QueryBuilder', array($keyValPairs, $sortedBy));
$this->extend('updateQueryFields', $fields);
}
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()
{
if (empty($this->KeyValuePairs) || !$this->isChanged('KeyValuePairs')) {
return;
}
$keyValuesPair = Convert::json2array($this->KeyValuePairs);
/** @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));
}
return $originals;
}
/**
* Customise the display fields for the documents GridField
*
* @return array
*/
public function getDocumentDisplayFields()
{
return array_merge(
(array) DMSDocument::create()->config()->get('display_fields'),
array('ManuallyAdded' => _t('DMSDocumentSet.ADDEDMETHOD', 'Added'))
);
}
protected function validate()
{
$result = parent::validate();
if (!$this->getTitle()) {
$result->error(_t('DMSDocumentSet.VALIDATION_NO_TITLE', '\'Title\' is required.'));
}
return $result;
}
}