silverstripe-dms/code/extensions/DMSSiteTreeExtension.php

104 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* @package dms
*/
2015-12-17 19:48:37 +01:00
class DMSSiteTreeExtension extends DataExtension
{
private static $has_many = array(
'DocumentSets' => 'DMSDocumentSet'
2015-12-17 19:48:37 +01:00
);
public function updateCMSFields(FieldList $fields)
{
// Ability to disable document sets for a Page
if (!$this->owner->config()->get('documents_enabled')) {
2015-12-17 19:48:37 +01:00
return;
}
$gridField = GridField::create(
'Document Sets',
2015-12-17 19:48:37 +01:00
false,
$this->owner->DocumentSets(), //->Sort('DocumentSort'),
new GridFieldConfig_RecordEditor
2015-12-17 19:48:37 +01:00
);
$gridField->addExtraClass('documentsets');
2015-12-17 19:48:37 +01:00
$fields->addFieldToTab(
'Root.Document Sets (' . $this->owner->DocumentSets()->count() . ')',
$gridField
2015-12-17 19:48:37 +01:00
);
}
2015-12-17 19:48:37 +01:00
/**
* Get a list of document sets for the owner page
*
* @return ArrayList
*/
public function getDocumentSets()
{
return $this->owner->DocumentSets();
2015-12-17 19:48:37 +01: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
*/
public function getAllDocuments()
2015-12-17 19:48:37 +01:00
{
$documents = ArrayList::create();
foreach ($this->getDocumentSets() as $documentSet) {
/** @var DocumentSet $documentSet */
$documents->merge($documentSet->getDocuments());
}
$documents->removeDuplicates();
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) {
$dmsDocuments = $this->owner->getAllDocuments();
2015-12-17 19:48:37 +01:00
foreach ($dmsDocuments as $document) {
// 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()
{
$embargoedDocuments = $this->owner->getAllDocuments()->filter('EmbargoedUntilPublished', true);
if ($embargoedDocuments->count() > 0) {
2015-12-17 19:48:37 +01:00
foreach ($embargoedDocuments as $doc) {
$doc->EmbargoedUntilPublished = false;
$doc->write();
}
}
}
/**
* 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()
{
return $this->owner->Title . ' (' . $this->owner->getAllDocuments()->count() . ')';
2015-12-17 19:48:37 +01:00
}
2013-08-22 13:38:26 +02:00
}