2012-07-17 07:58:33 +02:00
|
|
|
<?php
|
2014-01-10 03:19:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package dms
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
class DMSSiteTreeExtension extends DataExtension
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
'DocumentSets' => 'DMSDocumentSet'
|
2015-12-17 19:48:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
// Ability to disable document sets for a Page
|
|
|
|
if (!$this->owner->config()->get('documents_enabled')) {
|
2015-12-17 19:48:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-14 01:05:19 +02:00
|
|
|
// Hides the DocumentSets tab if the user has no permisions
|
|
|
|
if (!Permission::checkMember(
|
|
|
|
Member::currentUser(),
|
|
|
|
array('ADMIN', 'CMS_ACCESS_DMSDocumentAdmin')
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
$gridField = GridField::create(
|
2017-12-01 02:29:41 +01:00
|
|
|
'DocumentSets',
|
2015-12-17 19:48:37 +01:00
|
|
|
false,
|
2017-05-02 04:49:41 +02:00
|
|
|
$this->owner->DocumentSets(), //->Sort('DocumentSort'),
|
2017-06-06 01:16:44 +02:00
|
|
|
$config = new GridFieldConfig_RelationEditor
|
2015-12-17 19:48:37 +01:00
|
|
|
);
|
2017-05-02 04:49:41 +02:00
|
|
|
$gridField->addExtraClass('documentsets');
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-06-06 01:16:44 +02:00
|
|
|
// Only show document sets in the autocompleter that have not been assigned to a page already
|
|
|
|
$config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchList(
|
|
|
|
DMSDocumentSet::get()->filter(array('PageID' => 0))
|
|
|
|
);
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
$fields->addFieldToTab(
|
2017-12-01 02:29:41 +01:00
|
|
|
'Root.DocumentSets',
|
2017-05-02 04:49:41 +02:00
|
|
|
$gridField
|
2015-12-17 19:48:37 +01:00
|
|
|
);
|
2017-12-01 02:29:41 +01:00
|
|
|
|
|
|
|
$fields
|
|
|
|
->findOrMakeTab('Root.DocumentSets')
|
|
|
|
->setTitle(_t(
|
|
|
|
__CLASS__ . '.DocumentSetsTabTitle',
|
|
|
|
'Document Sets ({count})',
|
|
|
|
array('count' => $this->owner->DocumentSets()->count())
|
|
|
|
));
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2018-04-11 02:34:52 +02:00
|
|
|
/**
|
|
|
|
* Get a list of document sets for the owner page
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getDocumentSets()
|
|
|
|
{
|
2018-04-11 04:10:55 +02:00
|
|
|
return $this->owner->DocumentSets();
|
2018-04-11 02:34:52 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
/**
|
2017-05-02 04:49:41 +02:00
|
|
|
* Get a list of all documents from all document sets for the owner page
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
2017-05-02 04:49:41 +02:00
|
|
|
public function getAllDocuments()
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
$documents = ArrayList::create();
|
|
|
|
|
2018-04-11 04:10:55 +02:00
|
|
|
foreach ($this->getDocumentSets() as $documentSet) {
|
2017-05-02 04:49:41 +02:00
|
|
|
/** @var DocumentSet $documentSet */
|
|
|
|
$documents->merge($documentSet->getDocuments());
|
|
|
|
}
|
2017-05-05 04:51:52 +02:00
|
|
|
$documents->removeDuplicates();
|
2017-05-02 04:49:41 +02:00
|
|
|
|
|
|
|
return $documents;
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onBeforeDelete()
|
|
|
|
{
|
|
|
|
if (Versioned::current_stage() == 'Live') {
|
|
|
|
$existsOnOtherStage = !$this->owner->getIsDeletedFromStage();
|
|
|
|
} else {
|
|
|
|
$existsOnOtherStage = $this->owner->getExistsOnLive();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only remove if record doesn't still exist on live stage.
|
|
|
|
if (!$existsOnOtherStage) {
|
2017-05-05 04:51:52 +02:00
|
|
|
$dmsDocuments = $this->owner->getAllDocuments();
|
2015-12-17 19:48:37 +01:00
|
|
|
foreach ($dmsDocuments as $document) {
|
2017-05-05 04:51:52 +02:00
|
|
|
// If the document is only associated with one page, i.e. only associated with this page
|
|
|
|
if ($document->getRelatedPages()->count() <= 1) {
|
|
|
|
// Delete the document before deleting this page
|
2015-12-17 19:48:37 +01:00
|
|
|
$document->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onBeforePublish()
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
$embargoedDocuments = $this->owner->getAllDocuments()->filter('EmbargoedUntilPublished', true);
|
2017-05-05 04:51:52 +02:00
|
|
|
if ($embargoedDocuments->count() > 0) {
|
2015-12-17 19:48:37 +01:00
|
|
|
foreach ($embargoedDocuments as $doc) {
|
|
|
|
$doc->EmbargoedUntilPublished = false;
|
|
|
|
$doc->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Returns the title of the page with the total number of documents it has associated with it across
|
|
|
|
* all document sets
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public function getTitleWithNumberOfDocuments()
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
return $this->owner->Title . ' (' . $this->owner->getAllDocuments()->count() . ')';
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
2013-08-22 13:38:26 +02:00
|
|
|
}
|