2012-07-17 07:58:33 +02:00
|
|
|
<?php
|
|
|
|
class DMSSiteTreeExtension extends DataExtension {
|
|
|
|
|
2013-08-22 13:38:26 +02:00
|
|
|
private static $belongs_many_many = array(
|
2012-07-17 07:58:33 +02:00
|
|
|
'Documents' => 'DMSDocument'
|
|
|
|
);
|
2012-08-21 22:57:40 +02:00
|
|
|
|
2013-08-22 13:38:26 +02:00
|
|
|
private static $noDocumentsList = array();
|
|
|
|
private static $showDocumentsList = array();
|
2012-08-27 04:20:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not show the documents tab on the array of pages set here
|
|
|
|
* @static
|
|
|
|
* @param $mixed Array of page types to not show the Documents tab on
|
|
|
|
*/
|
2012-11-22 02:15:13 +01:00
|
|
|
static function no_documents_tab($array = array()) {
|
|
|
|
if (empty($array)) return;
|
2012-08-27 04:20:21 +02:00
|
|
|
if (is_array($array)) {
|
|
|
|
self::$noDocumentsList = $array;
|
|
|
|
} else {
|
|
|
|
self::$noDocumentsList = array($array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-22 02:15:13 +01:00
|
|
|
/**
|
|
|
|
* Only show the documents tab on the list of pages set here. Any pages set in the no_documents_tab array will
|
|
|
|
* still not be shown. If this isn't called, or if it is called with an empty array, all pages will get Document tabs.
|
|
|
|
* @static
|
|
|
|
* @param $array Array of page types to show the Documents tab on
|
|
|
|
*/
|
|
|
|
static function show_documents_tab($array = array()) {
|
|
|
|
if (empty($array)) return;
|
|
|
|
if (is_array($array)) {
|
|
|
|
self::$showDocumentsList = $array;
|
|
|
|
} else {
|
|
|
|
self::$showDocumentsList = array($array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 02:33:01 +02:00
|
|
|
function updateCMSFields(FieldList $fields){
|
2012-08-27 04:20:21 +02:00
|
|
|
//prevent certain pages from having a Document tab in the CMS
|
|
|
|
if (in_array($this->owner->ClassName,self::$noDocumentsList)) return;
|
2012-11-22 02:15:13 +01:00
|
|
|
if (count(self::$showDocumentsList) > 0 && !in_array($this->owner->ClassName,self::$showDocumentsList)) return;
|
2012-08-27 04:20:21 +02:00
|
|
|
|
2012-08-01 05:00:43 +02:00
|
|
|
//javascript to customize the grid field for the DMS document (overriding entwine in FRAMEWORK_DIR.'/javascript/GridField.js'
|
2012-11-20 22:22:40 +01:00
|
|
|
Requirements::javascript(DMS_DIR.'/javascript/DMSGridField.js');
|
|
|
|
Requirements::css(DMS_DIR.'/css/DMSMainCMS.css');
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2012-08-21 04:21:23 +02:00
|
|
|
//javascript for the link editor pop-up in TinyMCE
|
2012-11-20 22:22:40 +01:00
|
|
|
Requirements::javascript(DMS_DIR."/javascript/DocumentHtmlEditorFieldToolbar.js");
|
2012-08-21 04:21:23 +02:00
|
|
|
|
2012-07-31 04:12:20 +02:00
|
|
|
// Document listing
|
|
|
|
$gridFieldConfig = GridFieldConfig::create()->addComponents(
|
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldFilterHeader(),
|
2013-12-16 02:48:48 +01:00
|
|
|
//new GridFieldSortableHeader(),
|
|
|
|
new GridFieldOrderableRows('DocumentSort'),
|
2012-07-31 04:12:20 +02:00
|
|
|
new GridFieldDataColumns(),
|
|
|
|
new GridFieldEditButton(),
|
2012-08-01 05:00:43 +02:00
|
|
|
new DMSGridFieldDeleteAction(), //special delete dialog to handle custom behaviour of unlinking and deleting
|
2012-07-31 04:12:20 +02:00
|
|
|
new GridFieldDetailForm()
|
|
|
|
//GridFieldLevelup::create($folder->ID)->setLinkSpec('admin/assets/show/%d')
|
|
|
|
);
|
2012-09-11 03:03:16 +02:00
|
|
|
|
|
|
|
if(class_exists('GridFieldPaginatorWithShowAll')){
|
|
|
|
$paginatorComponent = new GridFieldPaginatorWithShowAll(15);
|
|
|
|
}else{
|
|
|
|
$paginatorComponent = new GridFieldPaginator(15);
|
|
|
|
}
|
|
|
|
$gridFieldConfig->addComponent($paginatorComponent);
|
|
|
|
|
2012-08-21 22:57:40 +02:00
|
|
|
if(class_exists('GridFieldSortableRows')) {
|
|
|
|
$sortableComponent = new GridFieldSortableRows('DocumentSort');
|
2013-04-15 04:03:58 +02:00
|
|
|
//setUsePagenation method removed from newer version of SortableGridField.
|
|
|
|
if(method_exists($sortableComponent,'setUsePagination')){
|
|
|
|
$sortableComponent->setUsePagination(false)->setForceRedraw(true);
|
|
|
|
}
|
2012-08-21 22:57:40 +02:00
|
|
|
$gridFieldConfig->addComponent($sortableComponent);
|
|
|
|
}
|
2012-08-21 01:31:53 +02:00
|
|
|
|
2012-08-21 01:42:04 +02:00
|
|
|
// HACK: Create a singleton of DMSDocument to ensure extensions are applied before we try to get display fields.
|
2012-08-21 01:31:53 +02:00
|
|
|
singleton('DMSDocument');
|
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(Config::inst()->get('DMSDocument', 'display_fields'))
|
2012-09-03 04:57:54 +02:00
|
|
|
->setFieldCasting(array('LastChanged'=>"Datetime->Ago"))
|
2012-08-27 05:07:01 +02:00
|
|
|
->setFieldFormatting(array('FilenameWithoutID'=>'<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>$FilenameWithoutID</a>'));
|
2012-08-01 08:37:53 +02:00
|
|
|
|
|
|
|
//override delete functionality with this class
|
|
|
|
$gridFieldConfig->getComponentByType('GridFieldDetailForm')->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest');
|
|
|
|
|
2012-07-31 04:12:20 +02:00
|
|
|
$gridField = GridField::create(
|
|
|
|
'Documents',
|
|
|
|
false,
|
2012-10-10 02:44:37 +02:00
|
|
|
$this->owner->Documents()->Sort('DocumentSort'),
|
2012-07-31 04:12:20 +02:00
|
|
|
$gridFieldConfig
|
|
|
|
);
|
2012-09-11 07:29:52 +02:00
|
|
|
$gridField->addExtraClass('documents');
|
2012-07-31 04:12:20 +02:00
|
|
|
|
|
|
|
$uploadBtn = new LiteralField(
|
|
|
|
'UploadButton',
|
|
|
|
sprintf(
|
|
|
|
'<a class="ss-ui-button ss-ui-action-constructive cms-panel-link" data-pjax-target="Content" data-icon="add" href="%s">%s</a>',
|
|
|
|
Controller::join_links(singleton('DMSDocumentAddController')->Link(), '?ID=' . $this->owner->ID),
|
2012-08-14 03:02:53 +02:00
|
|
|
"Add Documents"
|
2012-07-31 04:12:20 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$fields->addFieldsToTab(
|
2012-09-03 03:29:13 +02:00
|
|
|
'Root.Documents (' . $this->owner->Documents()->Count() . ')',
|
2012-07-31 04:12:20 +02:00
|
|
|
array(
|
|
|
|
$uploadBtn,
|
|
|
|
$gridField
|
2012-07-27 05:38:42 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2012-08-07 04:38:40 +02:00
|
|
|
|
2012-08-21 22:57:40 +02:00
|
|
|
/**
|
|
|
|
* Overloaded to enforce sorting
|
|
|
|
*/
|
|
|
|
function Documents() {
|
|
|
|
return $this->owner->getManyManyComponents('Documents')->sort('DocumentSort');
|
|
|
|
}
|
|
|
|
|
2012-08-07 04:38:40 +02:00
|
|
|
function onBeforeDelete() {
|
2012-10-15 01:01:06 +02:00
|
|
|
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->Documents();
|
|
|
|
foreach($dmsDocuments as $document) {
|
|
|
|
//if the document is only associated with one page, i.e. only associated with this page
|
|
|
|
if ($document->Pages()->Count() <= 1) {
|
|
|
|
//delete the document before deleting this page
|
|
|
|
$document->delete();
|
|
|
|
}
|
2012-08-07 04:38:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 04:14:31 +02:00
|
|
|
|
|
|
|
function onBeforePublish() {
|
|
|
|
$embargoedDocuments = $this->owner->Documents()->filter('EmbargoedUntilPublished',true);
|
|
|
|
if ($embargoedDocuments->Count() > 0) {
|
|
|
|
foreach($embargoedDocuments as $doc) {
|
|
|
|
$doc->EmbargoedUntilPublished = false;
|
|
|
|
$doc->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-08-14 01:45:16 +02:00
|
|
|
|
|
|
|
function getTitleWithNumberOfDocuments() {
|
|
|
|
return $this->owner->Title . ' (' . $this->owner->Documents()->Count() . ')';
|
|
|
|
}
|
2013-08-22 13:38:26 +02:00
|
|
|
}
|