2012-07-31 04:12:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DMSDocumentAddController extends LeftAndMain {
|
|
|
|
|
|
|
|
static $url_segment = 'pages/adddocument';
|
|
|
|
static $url_priority = 60;
|
|
|
|
static $required_permission_codes = 'CMS_ACCESS_AssetAdmin';
|
|
|
|
static $menu_title = 'Edit Page';
|
|
|
|
public static $tree_class = 'SiteTree';
|
2012-11-22 22:56:28 +01:00
|
|
|
static $allowed_extensions = array();
|
|
|
|
|
2013-06-25 18:57:36 +02:00
|
|
|
private static $allowed_actions = array (
|
|
|
|
"getEditForm",
|
|
|
|
);
|
|
|
|
|
2012-11-22 22:56:28 +01:00
|
|
|
/**
|
|
|
|
* Add an array of additional allowed extensions
|
|
|
|
* @static
|
|
|
|
* @param $array
|
|
|
|
*/
|
|
|
|
static function add_allowed_extensions($array = null) {
|
|
|
|
if (empty($array)) return;
|
|
|
|
if (is_array($array)) self::$allowed_extensions = $array;
|
|
|
|
else self::$allowed_extensions = array($array);
|
|
|
|
}
|
2012-07-31 04:12:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom currentPage() method to handle opening the 'root' folder
|
|
|
|
*/
|
|
|
|
public function currentPage() {
|
|
|
|
$id = $this->currentPageID();
|
|
|
|
|
|
|
|
if($id && is_numeric($id) && $id > 0) {
|
|
|
|
return DataObject::get_by_id('SiteTree', $id);
|
|
|
|
} 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)
|
|
|
|
*/
|
|
|
|
public function currentPageID() {
|
|
|
|
if(is_numeric($this->request->requestVar('ID'))) {
|
|
|
|
return $this->request->requestVar('ID');
|
|
|
|
} elseif (is_numeric($this->urlParams['ID'])) {
|
|
|
|
return $this->urlParams['ID'];
|
|
|
|
} elseif(Session::get("{$this->class}.currentPage")) {
|
|
|
|
return Session::get("{$this->class}.currentPage");
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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');
|
2012-11-20 22:22:40 +01:00
|
|
|
Requirements::css(DMS_DIR.'/css/DMSMainCMS.css');
|
2012-07-31 04:12:20 +02:00
|
|
|
|
|
|
|
$page = $this->currentPage();
|
|
|
|
|
2012-07-31 06:49:53 +02:00
|
|
|
$uploadField = DMSUploadField::create('AssetUploadField', '');
|
2012-07-31 04:12:20 +02:00
|
|
|
$uploadField->setConfig('previewMaxWidth', 40);
|
|
|
|
$uploadField->setConfig('previewMaxHeight', 30);
|
2012-09-11 10:22:17 +02:00
|
|
|
// Required to avoid Solr reindexing (often used alongside DMS) to
|
|
|
|
// return 503s because of too many concurrent reindex requests
|
|
|
|
$uploadField->setConfig('sequentialUploads', 1);
|
2012-07-31 04:12:20 +02:00
|
|
|
$uploadField->addExtraClass('ss-assetuploadfield');
|
|
|
|
$uploadField->removeExtraClass('ss-uploadfield');
|
|
|
|
$uploadField->setTemplate('AssetUploadField');
|
2012-07-31 06:49:53 +02:00
|
|
|
$uploadField->setRecord($page);
|
2012-07-31 04:12:20 +02:00
|
|
|
|
2013-04-15 04:03:58 +02:00
|
|
|
$uploadField->getValidator()->setAllowedExtensions(array_filter(array_merge(Config::inst()->get('File', 'allowed_extensions'),self::$allowed_extensions)));
|
2012-07-31 04:12:20 +02:00
|
|
|
$exts = $uploadField->getValidator()->getAllowedExtensions();
|
2012-11-22 22:56:28 +01:00
|
|
|
|
2012-07-31 04:12:20 +02:00
|
|
|
asort($exts);
|
2012-08-03 04:58:43 +02:00
|
|
|
$backlink = $this->Backlink();
|
|
|
|
$done = "
|
|
|
|
<a class=\"ss-ui-button ss-ui-action-constructive cms-panel-link ui-corner-all\" href=\"".$backlink."\">
|
|
|
|
Done!
|
|
|
|
</a>";
|
2012-08-06 07:52:18 +02:00
|
|
|
|
|
|
|
$addExistingField = new DMSDocumentAddExistingField('AddExisting', 'Add Existing');
|
|
|
|
$addExistingField->setRecord($page);
|
2012-07-31 04:12:20 +02:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'getEditForm',
|
|
|
|
new FieldList(
|
2012-08-06 07:52:18 +02:00
|
|
|
new TabSet('Main',
|
|
|
|
new Tab('From your computer',
|
|
|
|
new HiddenField('ID', false, $page->ID),
|
|
|
|
$uploadField,
|
|
|
|
new LiteralField(
|
|
|
|
'AllowedExtensions',
|
|
|
|
sprintf(
|
|
|
|
'<p>%s: %s</p>',
|
|
|
|
_t('AssetAdmin.ALLOWEDEXTS', 'Allowed extensions'),
|
|
|
|
implode('<em>, </em>', $exts)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new Tab('From the CMS',
|
|
|
|
$addExistingField
|
2012-07-31 04:12:20 +02:00
|
|
|
)
|
2012-07-31 06:49:53 +02:00
|
|
|
)
|
2012-07-31 04:12:20 +02:00
|
|
|
),
|
2012-08-03 04:58:43 +02:00
|
|
|
new FieldList(
|
|
|
|
new LiteralField('doneButton', $done)
|
|
|
|
)
|
2012-07-31 04:12:20 +02:00
|
|
|
);
|
|
|
|
$form->addExtraClass('center cms-edit-form ' . $this->BaseCSSClasses());
|
2012-08-03 04:58:43 +02:00
|
|
|
$form->Backlink = $backlink;
|
2012-07-31 04:12:20 +02:00
|
|
|
// Don't use AssetAdmin_EditForm, as it assumes a different panel structure
|
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
|
|
|
|
/*$form->Fields()->push(
|
|
|
|
new LiteralField(
|
|
|
|
'BackLink',
|
|
|
|
sprintf(
|
|
|
|
'<a href="%s" class="backlink ss-ui-button cms-panel-link" data-icon="back">%s</a>',
|
|
|
|
Controller::join_links(singleton('AssetAdmin')->Link('show'), $folder ? $folder->ID : 0),
|
|
|
|
_t('AssetAdmin.BackToFolder', 'Back to folder')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);*/
|
|
|
|
//$form->loadDataFrom($folder);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2012-08-01 00:49:40 +02:00
|
|
|
public function Breadcrumbs($unlinked = false) {
|
2012-07-31 04:12:20 +02:00
|
|
|
$items = parent::Breadcrumbs($unlinked);
|
|
|
|
|
|
|
|
// The root element should explicitly point to the root node.
|
2012-08-01 00:49:40 +02:00
|
|
|
$items[0]->Link = Controller::join_links(singleton('CMSPageEditController')->Link('show'), 0);
|
2012-07-31 04:12:20 +02:00
|
|
|
|
|
|
|
// Enforce linkage of hierarchy to AssetAdmin
|
|
|
|
foreach($items as $item) {
|
|
|
|
$baselink = $this->Link('show');
|
|
|
|
if(strpos($item->Link, $baselink) !== false) {
|
2012-08-01 00:49:40 +02:00
|
|
|
$item->Link = str_replace($baselink, singleton('CMSPageEditController')->Link('show'), $item->Link);
|
2012-07-31 04:12:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$items->push(new ArrayData(array(
|
2012-08-01 00:49:40 +02:00
|
|
|
'Title' => 'Add Document',
|
2012-07-31 04:12:20 +02:00
|
|
|
'Link' => $this->Link()
|
|
|
|
)));
|
|
|
|
|
|
|
|
return $items;
|
2012-08-01 00:49:40 +02:00
|
|
|
}
|
2012-08-03 04:58:43 +02:00
|
|
|
|
|
|
|
public function Backlink(){
|
|
|
|
$pageID = $this->currentPageID();
|
|
|
|
return singleton('CMSPagesController')->Link().'edit/show/'.$pageID;
|
|
|
|
}
|
2012-07-31 04:12:20 +02:00
|
|
|
|
2012-08-07 03:44:42 +02:00
|
|
|
public function documentautocomplete() {
|
|
|
|
$term = $_GET['term'];
|
|
|
|
$term_sql = Convert::raw2sql($term);
|
2013-04-18 03:38:29 +02:00
|
|
|
$data = DMSDocument::get()
|
|
|
|
->where("(\"ID\" LIKE '%".$term_sql."%' OR \"Filename\" LIKE '%".$term_sql."%' OR \"Title\" LIKE '%".$term_sql."%')")
|
|
|
|
->limit(20);
|
2012-08-07 03:44:42 +02:00
|
|
|
$return = array();
|
|
|
|
foreach($data as $doc) {
|
2012-08-07 04:42:03 +02:00
|
|
|
$return[] = array(
|
|
|
|
'label' => $doc->ID . ' - ' . $doc->Title,
|
|
|
|
'value' => $doc->ID
|
|
|
|
);
|
2012-08-07 03:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return json_encode($return);
|
|
|
|
}
|
|
|
|
|
2012-08-07 04:42:03 +02:00
|
|
|
public function linkdocument() {
|
2012-08-09 08:10:50 +02:00
|
|
|
$return = array('error' => _t('UploadField.FIELDNOTSET', 'Could not add document to page'));
|
|
|
|
|
2012-08-07 04:42:03 +02:00
|
|
|
$page = $this->currentPage();
|
2012-08-09 08:10:50 +02:00
|
|
|
if (!empty($page)) {
|
|
|
|
$document = DataObject::get_by_id('DMSDocument', (int) $_GET['documentID']);
|
|
|
|
$document->addPage($page);
|
|
|
|
|
2012-08-10 02:05:34 +02:00
|
|
|
$buttonText = '<button class="ss-uploadfield-item-edit ss-ui-button ui-corner-all" title="Edit this document" data-icon="pencil">'.
|
|
|
|
'Edit<span class="toggle-details"><span class="toggle-details-icon"></span></span></button>';
|
|
|
|
|
2012-08-09 08:10:50 +02:00
|
|
|
// Collect all output data.
|
|
|
|
$return = array(
|
|
|
|
'id' => $document->ID,
|
|
|
|
'name' => $document->getTitle(),
|
2012-08-27 05:07:01 +02:00
|
|
|
'thumbnail_url' => $document->Icon($document->getExtension()),
|
2012-08-09 08:10:50 +02:00
|
|
|
'edit_url' => $this->getEditForm()->Fields()->fieldByName('Main.From your computer.AssetUploadField')->getItemHandler($document->ID)->EditLink(),
|
|
|
|
'size' => $document->getFileSizeFormatted(),
|
2012-08-10 02:05:34 +02:00
|
|
|
'buttons' => $buttonText,
|
2012-08-09 08:10:50 +02:00
|
|
|
'showeditform' => true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_encode($return);
|
2012-08-07 04:42:03 +02:00
|
|
|
}
|
2012-08-14 03:59:08 +02:00
|
|
|
|
|
|
|
public function documentlist() {
|
|
|
|
$page = DataObject::get_by_id('SiteTree', (int) $_GET['pageID']);
|
|
|
|
if($page && $page->Documents()) {
|
|
|
|
$list = '<ul>';
|
|
|
|
|
|
|
|
foreach($page->Documents() as $document) {
|
2012-08-14 04:12:56 +02:00
|
|
|
$list .= '<li><a class="add-document" data-document-id="' . $document->ID . '">';
|
|
|
|
$list .= $document->ID . ' - ' . Convert::raw2xml($document->Title);
|
|
|
|
$list .= '</a></li>';
|
2012-08-14 03:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$list .= '</ul>';
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
} else {
|
|
|
|
return '<p>There are no documents attached to the selected page.</p>';
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 04:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|