Source of file DMSSiteTreeExtension.php
Size: 3,636 Bytes - Last Modified: 2021-12-23T10:39:26+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/DMSSiteTreeExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | <?php namespace Sunnysideup\DMS\Extensions; use Sunnysideup\DMS\Model\DMSDocumentSet; use SilverStripe\Forms\FieldList; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\Dev\Deprecation; use SilverStripe\ORM\ArrayList; use SilverStripe\Versioned\Versioned; use SilverStripe\ORM\DataExtension; /** * @package dms */ class DMSSiteTreeExtension extends DataExtension { private static $has_many = array( 'DocumentSets' => DMSDocumentSet::class ); public function updateCMSFields(FieldList $fields) { // Ability to disable document sets for a Page if (!$this->owner->config()->get('documents_enabled')) { return; } // Hides the DocumentSets tab if the user has no permisions if (!Permission::checkMember( Member::currentUser(), array('ADMIN', 'CMS_ACCESS_DMSDocumentAdmin') ) ) { return; } $gridField = GridField::create( 'DocumentSets', false, $this->owner->DocumentSets(), //->Sort('DocumentSort'), $config = new GridFieldConfig_RelationEditor ); $gridField->addExtraClass('documentsets'); // Only show document sets in the autocompleter that have not been assigned to a page already $config->getComponentByType(GridFieldAddExistingAutocompleter::class)->setSearchList( DMSDocumentSet::get()->filter(array('PageID' => 0)) ); $fields->addFieldToTab( 'Root.DocumentSets', $gridField ); $fields ->findOrMakeTab('Root.DocumentSets') ->setTitle(_t( __CLASS__ . '.DocumentSetsTabTitle', 'Document Sets ({count})', array('count' => $this->owner->DocumentSets()->count()) )); } /** * Get a list of all documents from all document sets for the owner page * * @return ArrayList */ public function getAllDocuments() { $documents = ArrayList::create(); foreach ($this->owner->DocumentSets() as $documentSet) { /** @var DocumentSet $documentSet */ $documents->merge($documentSet->getDocuments()); } $documents->removeDuplicates(); return $documents; } public function onBeforeDelete() { //@todo: UPGRADE: should we really delete the documents, even from the sets? // Only remove if record doesn't still exist on live stage. if ($this->owner->isOnDraft() || $this->owner->isPublished()) { //do nothing... } else { $dmsDocuments = $this->owner->getAllDocuments(); 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 $document->delete(); } } } } /** * Returns the title of the page with the total number of documents it has associated with it across * all document sets * * @return string */ public function getTitleWithNumberOfDocuments() { return $this->owner->Title . ' (' . $this->owner->getAllDocuments()->count() . ')'; } } |