2012-07-31 04:12:20 +02:00
|
|
|
<?php
|
|
|
|
|
2014-01-10 03:19:12 +01:00
|
|
|
/**
|
|
|
|
* @package dms
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
class DMSDocumentAddController extends LeftAndMain
|
|
|
|
{
|
|
|
|
private static $url_segment = 'pages/adddocument';
|
|
|
|
private static $url_priority = 60;
|
|
|
|
private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin';
|
|
|
|
private static $menu_title = 'Edit Page';
|
|
|
|
private static $tree_class = 'SiteTree';
|
|
|
|
private static $session_namespace = 'CMSMain';
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Allowed file upload extensions, will be merged with `$allowed_extensions` from {@link File}
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $allowed_extensions = array();
|
2015-12-17 19:48:37 +01:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'getEditForm',
|
|
|
|
'documentautocomplete',
|
|
|
|
'linkdocument',
|
|
|
|
'documentlist'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom currentPage() method to handle opening the 'root' folder
|
|
|
|
*
|
2017-05-02 04:49:41 +02:00
|
|
|
* @return SiteTree
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
|
|
|
public function currentPage()
|
|
|
|
{
|
|
|
|
$id = $this->currentPageID();
|
|
|
|
|
|
|
|
if ($id && is_numeric($id) && $id > 0) {
|
|
|
|
return Versioned::get_by_stage('SiteTree', 'Stage', sprintf(
|
2017-05-01 05:54:48 +02:00
|
|
|
'ID = %s',
|
|
|
|
(int) $id
|
2015-12-17 19:48:37 +01:00
|
|
|
))->first();
|
|
|
|
} else {
|
|
|
|
// ID is either '0' or 'root'
|
|
|
|
return singleton('SiteTree');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return fake-ID "root" if no ID is found (needed to upload files into the root-folder)
|
2017-05-02 04:49:41 +02:00
|
|
|
*
|
|
|
|
* @return int
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
|
|
|
public function currentPageID()
|
|
|
|
{
|
|
|
|
return ($result = parent::currentPageID()) === null ? 0 : $result;
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Get the current document set, if a document set ID was provided
|
|
|
|
*
|
|
|
|
* @return DMSDocumentSet
|
|
|
|
*/
|
|
|
|
public function getCurrentDocumentSet()
|
|
|
|
{
|
|
|
|
if ($id = $this->getRequest()->getVar('dsid')) {
|
|
|
|
return DMSDocumentSet::get()->byid($id);
|
|
|
|
}
|
|
|
|
return singleton('DMSDocumentSet');
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
* @todo what template is used here? AssetAdmin_UploadContent.ss doesn't seem to be used anymore
|
|
|
|
*/
|
|
|
|
public function getEditForm($id = null, $fields = null)
|
|
|
|
{
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/AssetUploadField.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/AssetUploadField.css');
|
2017-05-02 04:49:41 +02:00
|
|
|
Requirements::css(DMS_DIR . '/css/DMSMainCMS.css');
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/** @var SiteTree $page */
|
2015-12-17 19:48:37 +01:00
|
|
|
$page = $this->currentPage();
|
2017-05-02 04:49:41 +02:00
|
|
|
/** @var DMSDocumentSet $documentSet */
|
|
|
|
$documentSet = $this->getCurrentDocumentSet();
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
$uploadField = DMSUploadField::create('AssetUploadField', '');
|
|
|
|
$uploadField->setConfig('previewMaxWidth', 40);
|
|
|
|
$uploadField->setConfig('previewMaxHeight', 30);
|
|
|
|
// Required to avoid Solr reindexing (often used alongside DMS) to
|
|
|
|
// return 503s because of too many concurrent reindex requests
|
|
|
|
$uploadField->setConfig('sequentialUploads', 1);
|
|
|
|
$uploadField->addExtraClass('ss-assetuploadfield');
|
|
|
|
$uploadField->removeExtraClass('ss-uploadfield');
|
|
|
|
$uploadField->setTemplate('AssetUploadField');
|
2017-05-02 04:49:41 +02:00
|
|
|
$uploadField->setRecord($documentSet);
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
$uploadField->getValidator()->setAllowedExtensions($this->getAllowedExtensions());
|
2015-12-17 19:48:37 +01:00
|
|
|
$exts = $uploadField->getValidator()->getAllowedExtensions();
|
|
|
|
|
|
|
|
asort($exts);
|
|
|
|
$backlink = $this->Backlink();
|
|
|
|
$done = "
|
2017-05-02 04:49:41 +02:00
|
|
|
<a class=\"ss-ui-button ss-ui-action-constructive cms-panel-link ui-corner-all\" href=\"" . $backlink . "\">
|
|
|
|
" . _t('UploadField.DONE', 'DONE') . "
|
2012-08-03 04:58:43 +02:00
|
|
|
</a>";
|
2012-08-06 07:52:18 +02:00
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
$addExistingField = new DMSDocumentAddExistingField(
|
|
|
|
'AddExisting',
|
|
|
|
_t('DMSDocumentAddExistingField.ADDEXISTING', 'Add Existing')
|
|
|
|
);
|
|
|
|
$addExistingField->setRecord($documentSet);
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'getEditForm',
|
|
|
|
new FieldList(
|
2017-05-01 05:54:48 +02:00
|
|
|
new TabSet(
|
2017-05-02 04:49:41 +02:00
|
|
|
_t('DMSDocumentAddController.MAINTAB', 'Main'),
|
2017-05-01 05:54:48 +02:00
|
|
|
new Tab(
|
2017-05-02 04:49:41 +02:00
|
|
|
_t('UploadField.FROMCOMPUTER', 'From your computer'),
|
2015-12-17 19:48:37 +01:00
|
|
|
new HiddenField('ID', false, $page->ID),
|
2017-05-02 04:49:41 +02:00
|
|
|
new HiddenField('DSID', false, $documentSet->ID),
|
2015-12-17 19:48:37 +01:00
|
|
|
$uploadField,
|
|
|
|
new LiteralField(
|
|
|
|
'AllowedExtensions',
|
|
|
|
sprintf(
|
|
|
|
'<p>%s: %s</p>',
|
|
|
|
_t('AssetAdmin.ALLOWEDEXTS', 'Allowed extensions'),
|
|
|
|
implode('<em>, </em>', $exts)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
2017-05-01 05:54:48 +02:00
|
|
|
new Tab(
|
2017-05-02 04:49:41 +02:00
|
|
|
_t('UploadField.FROMCMS', 'From the CMS'),
|
2015-12-17 19:48:37 +01:00
|
|
|
$addExistingField
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new FieldList(
|
|
|
|
new LiteralField('doneButton', $done)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$form->addExtraClass('center cms-edit-form ' . $this->BaseCSSClasses());
|
|
|
|
$form->Backlink = $backlink;
|
|
|
|
// Don't use AssetAdmin_EditForm, as it assumes a different panel structure
|
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function Breadcrumbs($unlinked = false)
|
|
|
|
{
|
|
|
|
$items = parent::Breadcrumbs($unlinked);
|
|
|
|
|
|
|
|
// The root element should explicitly point to the root node.
|
|
|
|
$items[0]->Link = Controller::join_links(singleton('CMSPageEditController')->Link('show'), 0);
|
|
|
|
|
|
|
|
// Enforce linkage of hierarchy to AssetAdmin
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$baselink = $this->Link('show');
|
|
|
|
if (strpos($item->Link, $baselink) !== false) {
|
|
|
|
$item->Link = str_replace($baselink, singleton('CMSPageEditController')->Link('show'), $item->Link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$items->push(new ArrayData(array(
|
2017-05-02 04:49:41 +02:00
|
|
|
'Title' => _t('DMSDocumentSet.ADDDOCUMENTBUTTON', 'Add Document'),
|
2015-12-17 19:48:37 +01:00
|
|
|
'Link' => $this->Link()
|
|
|
|
)));
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
return $items;
|
|
|
|
}
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Returns the link to be used to return the user after uploading a document. If a document set ID (dsid) is present
|
|
|
|
* then it will be redirected back to the page that owns the document set. @todo redirect back to the document set
|
|
|
|
*
|
|
|
|
* If no document set ID is present then we assume that it has been added from the model admin, so redirect back to
|
|
|
|
* that instead.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public function Backlink()
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
if (!$this->getRequest()->getVar('dsid')) {
|
|
|
|
$modelAdmin = new DMSDocumentAdmin;
|
|
|
|
$modelAdmin->init();
|
|
|
|
return $modelAdmin->Link();
|
|
|
|
}
|
|
|
|
return Controller::join_links(singleton('CMSPagesController')->Link(), 'edit/show', $this->currentPageID());
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function documentautocomplete()
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
$term = (string) $this->getRequest()->getVar('term');
|
|
|
|
$termSql = Convert::raw2sql($term);
|
2015-12-17 19:48:37 +01:00
|
|
|
$data = DMSDocument::get()
|
2017-05-02 04:49:41 +02:00
|
|
|
->where(
|
|
|
|
'("ID" LIKE \'%' . $termSql . '%\' OR "Filename" LIKE \'%' . $termSql . '%\''
|
|
|
|
. ' OR "Title" LIKE \'%' . $termSql . '%\')'
|
|
|
|
)
|
|
|
|
->sort('ID ASC')
|
|
|
|
->limit(20);
|
2015-12-17 19:48:37 +01:00
|
|
|
|
|
|
|
$return = array();
|
|
|
|
foreach ($data as $doc) {
|
|
|
|
$return[] = array(
|
|
|
|
'label' => $doc->ID . ' - ' . $doc->Title,
|
|
|
|
'value' => $doc->ID
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
return Convert::raw2json($return);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 05:35:39 +02:00
|
|
|
/**
|
|
|
|
* Link an existing document to the given document set ID
|
|
|
|
* @return string JSON
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public function linkdocument()
|
|
|
|
{
|
|
|
|
$return = array('error' => _t('UploadField.FIELDNOTSET', 'Could not add document to page'));
|
2017-05-05 05:35:39 +02:00
|
|
|
$documentSet = $this->getCurrentDocumentSet();
|
|
|
|
if (!empty($documentSet)) {
|
2017-05-02 04:49:41 +02:00
|
|
|
$document = DMSDocument::get()->byId($this->getRequest()->getVar('documentID'));
|
2017-05-05 05:35:39 +02:00
|
|
|
$documentSet->Documents()->add($document);
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
$buttonText = '<button class="ss-uploadfield-item-edit ss-ui-button ui-corner-all"'
|
2017-05-02 04:49:41 +02:00
|
|
|
. ' title="' . _t('DMSDocument.EDITDOCUMENT', 'Edit this document') . '" data-icon="pencil">'
|
|
|
|
. _t('DMSDocument.EDIT', 'Edit') . '<span class="toggle-details">'
|
|
|
|
. '<span class="toggle-details-icon"></span></span></button>';
|
2015-12-17 19:48:37 +01:00
|
|
|
|
|
|
|
// Collect all output data.
|
|
|
|
$return = array(
|
|
|
|
'id' => $document->ID,
|
|
|
|
'name' => $document->getTitle(),
|
|
|
|
'thumbnail_url' => $document->Icon($document->getExtension()),
|
2017-05-01 05:54:48 +02:00
|
|
|
'edit_url' => $this->getEditForm()->Fields()->fieldByName('Main.From your computer.AssetUploadField')
|
|
|
|
->getItemHandler($document->ID)->EditLink(),
|
2015-12-17 19:48:37 +01:00
|
|
|
'size' => $document->getFileSizeFormatted(),
|
|
|
|
'buttons' => $buttonText,
|
|
|
|
'showeditform' => true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
return Convert::raw2json($return);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Returns HTML representing a list of documents that are associated with the given page ID, across all document
|
|
|
|
* sets.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public function documentlist()
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
if (!$this->getRequest()->getVar('pageID')) {
|
2015-12-17 19:48:37 +01:00
|
|
|
return $this->httpError(400);
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
$page = SiteTree::get()->byId($this->getRequest()->getVar('pageID'));
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
if ($page && $page->getAllDocuments()->count() > 0) {
|
2015-12-17 19:48:37 +01:00
|
|
|
$list = '<ul>';
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
foreach ($page->getAllDocuments() as $document) {
|
2015-12-17 19:48:37 +01:00
|
|
|
$list .= sprintf(
|
|
|
|
'<li><a class="add-document" data-document-id="%s">%s</a></li>',
|
|
|
|
$document->ID,
|
|
|
|
$document->ID . ' - '. Convert::raw2xml($document->Title)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$list .= '</ul>';
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
2017-05-01 05:54:48 +02:00
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'<p>%s</p>',
|
2015-12-17 19:48:37 +01:00
|
|
|
_t('DMSDocumentAddController.NODOCUMENTS', 'There are no documents attached to the selected page.')
|
|
|
|
);
|
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of allowed file upload extensions, merged with {@link File} and extra configuration from this
|
|
|
|
* class
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllowedExtensions()
|
|
|
|
{
|
|
|
|
return array_filter(
|
|
|
|
array_merge(
|
|
|
|
(array) Config::inst()->get('File', 'allowed_extensions'),
|
|
|
|
(array) $this->config()->get('allowed_extensions')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2012-07-31 04:12:20 +02:00
|
|
|
}
|