2012-07-31 04:24:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Field for uploading files into a DMSDocument. Replacing the existing file.
|
2012-08-06 20:53:34 +02:00
|
|
|
* Not ideally suited for the purpose, as the base implementation
|
|
|
|
* assumes to operate on a {@link File} record. We only use this as
|
|
|
|
* a temporary container, which gets deleted as soon as the actual
|
|
|
|
* {@link DMSDocument} is created.
|
2012-07-31 04:24:35 +02:00
|
|
|
*
|
|
|
|
* <b>NOTE: this Field will call write() on the supplied record</b>
|
|
|
|
*
|
|
|
|
* @author Julian Seidenberg
|
|
|
|
* @package dms
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
class DMSUploadField extends UploadField
|
|
|
|
{
|
|
|
|
private static $allowed_actions = array(
|
2013-06-25 18:57:36 +02:00
|
|
|
"upload",
|
2015-12-17 19:48:37 +01:00
|
|
|
);
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* The temporary folder name to store files in during upload
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
protected $folderName = 'DMSTemporaryUploads';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the default behaviour of the UploadField and take the uploaded file (uploaded to assets) and
|
|
|
|
* add it into the DMS storage, deleting the old/uploaded file.
|
|
|
|
* @param File
|
|
|
|
*/
|
|
|
|
protected function attachFile($file)
|
|
|
|
{
|
|
|
|
$dms = DMS::inst();
|
|
|
|
$record = $this->getRecord();
|
|
|
|
|
|
|
|
if ($record instanceof DMSDocument) {
|
|
|
|
// If the edited record is a document,
|
|
|
|
// assume we're replacing an existing file
|
|
|
|
$doc = $record;
|
|
|
|
$doc->ingestFile($file);
|
|
|
|
} else {
|
|
|
|
// Otherwise create it
|
|
|
|
$doc = $dms->storeDocument($file);
|
|
|
|
$file->delete();
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Relate to the underlying document set being edited.
|
|
|
|
// Not applicable when editing the document itself and replacing it, or uploading from the ModelAdmin
|
|
|
|
if ($record instanceof DMSDocumentSet) {
|
2017-05-17 00:58:51 +02:00
|
|
|
$record->Documents()->add($doc, array('ManuallyAdded' => 1));
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validate($validator)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to handle upload of a single file
|
2017-05-01 05:54:48 +02:00
|
|
|
*
|
2015-12-17 19:48:37 +01:00
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return string json
|
|
|
|
*/
|
|
|
|
public function upload(SS_HTTPRequest $request)
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
if ($recordId = $request->postVar('ID')) {
|
|
|
|
$this->setRecord(DMSDocumentSet::get()->byId($recordId));
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
if ($this->isDisabled() || $this->isReadonly()) {
|
|
|
|
return $this->httpError(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
$token = $this->getForm()->getSecurityToken();
|
|
|
|
if (!$token->checkRequest($request)) {
|
|
|
|
return $this->httpError(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->getName();
|
|
|
|
$tmpfile = $request->postVar($name);
|
|
|
|
$record = $this->getRecord();
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
// Check if the file has been uploaded into the temporary storage.
|
|
|
|
if (!$tmpfile) {
|
|
|
|
$return = array('error' => _t('UploadField.FIELDNOTSET', 'File information not found'));
|
|
|
|
} else {
|
|
|
|
$return = array(
|
|
|
|
'name' => $tmpfile['name'],
|
|
|
|
'size' => $tmpfile['size'],
|
|
|
|
'type' => $tmpfile['type'],
|
|
|
|
'error' => $tmpfile['error']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for constraints on the record to which the file will be attached.
|
|
|
|
if (!$return['error'] && $this->relationAutoSetting && $record && $record->exists()) {
|
|
|
|
$tooManyFiles = false;
|
|
|
|
// Some relationships allow many files to be attached.
|
2017-05-02 04:49:41 +02:00
|
|
|
if ($this->getConfig('allowedMaxFileNumber') && ($record->hasMany($name) || $record->manyMany($name))) {
|
2015-12-17 19:48:37 +01:00
|
|
|
if (!$record->isInDB()) {
|
|
|
|
$record->write();
|
|
|
|
}
|
|
|
|
$tooManyFiles = $record->{$name}()->count() >= $this->getConfig('allowedMaxFileNumber');
|
|
|
|
// has_one only allows one file at any given time.
|
2017-05-02 04:49:41 +02:00
|
|
|
} elseif ($record->hasOne($name)) {
|
2015-12-17 19:48:37 +01:00
|
|
|
$tooManyFiles = $record->{$name}() && $record->{$name}()->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report the constraint violation.
|
|
|
|
if ($tooManyFiles) {
|
|
|
|
if (!$this->getConfig('allowedMaxFileNumber')) {
|
|
|
|
$this->setConfig('allowedMaxFileNumber', 1);
|
|
|
|
}
|
|
|
|
$return['error'] = _t(
|
|
|
|
'UploadField.MAXNUMBEROFFILES',
|
|
|
|
'Max number of {count} file(s) exceeded.',
|
|
|
|
array('count' => $this->getConfig('allowedMaxFileNumber'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the uploaded file
|
|
|
|
if (!$return['error']) {
|
|
|
|
$fileObject = null;
|
|
|
|
|
|
|
|
if ($this->relationAutoSetting) {
|
|
|
|
// Search for relations that can hold the uploaded files.
|
|
|
|
if ($relationClass = $this->getRelationAutosetClass()) {
|
|
|
|
// Create new object explicitly. Otherwise rely on Upload::load to choose the class.
|
|
|
|
$fileObject = Object::create($relationClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the uploaded file into a new file object.
|
|
|
|
try {
|
2017-05-02 04:49:41 +02:00
|
|
|
$this->upload->loadIntoFile($tmpfile, $fileObject, $this->getFolderName());
|
2015-12-17 19:48:37 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// we shouldn't get an error here, but just in case
|
|
|
|
$return['error'] = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$return['error']) {
|
|
|
|
if ($this->upload->isError()) {
|
2017-05-02 04:49:41 +02:00
|
|
|
$return['error'] = implode(' ' . PHP_EOL, $this->upload->getErrors());
|
2015-12-17 19:48:37 +01:00
|
|
|
} else {
|
|
|
|
$file = $this->upload->getFile();
|
|
|
|
|
|
|
|
// CUSTOM Attach the file to the related record.
|
|
|
|
$document = $this->attachFile($file);
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
// Collect all output data.
|
|
|
|
$return = array_merge($return, array(
|
|
|
|
'id' => $document->ID,
|
|
|
|
'name' => $document->getTitle(),
|
|
|
|
'thumbnail_url' => $document->Icon($document->getExtension()),
|
|
|
|
'edit_url' => $this->getItemHandler($document->ID)->EditLink(),
|
|
|
|
'size' => $document->getFileSizeFormatted(),
|
|
|
|
'buttons' => (string) $document->renderWith($this->getTemplateFileButtons()),
|
|
|
|
'showeditform' => true
|
|
|
|
));
|
|
|
|
|
|
|
|
// CUSTOM END
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$response = new SS_HTTPResponse(Convert::raw2json(array($return)));
|
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Never directly display items uploaded
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
public function getItems()
|
|
|
|
{
|
|
|
|
return new ArrayList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Field($properties = array())
|
|
|
|
{
|
|
|
|
$fields = parent::Field($properties);
|
|
|
|
|
|
|
|
// Replace the download template with a new one only when access the upload field through a GridField.
|
|
|
|
// Needs to be enabled through setConfig('downloadTemplateName', 'ss-dmsuploadfield-downloadtemplate');
|
2017-05-02 04:49:41 +02:00
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DMSUploadField_downloadtemplate.js');
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
// In the add dialog, add the addtemplate into the set of file that load.
|
2017-05-02 04:49:41 +02:00
|
|
|
Requirements::javascript(DMS_DIR . '/javascript/DMSUploadField_addtemplate.js');
|
2015-12-17 19:48:37 +01:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $itemID
|
|
|
|
* @return UploadField_ItemHandler
|
|
|
|
*/
|
|
|
|
public function getItemHandler($itemID)
|
|
|
|
{
|
|
|
|
return DMSUploadField_ItemHandler::create($this, $itemID);
|
|
|
|
}
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $fields for the EditForm
|
|
|
|
* @example 'getCMSFields'
|
|
|
|
*
|
|
|
|
* @param File $file File context to generate fields for
|
|
|
|
* @return FieldList List of form fields
|
|
|
|
*/
|
2017-05-01 05:54:48 +02:00
|
|
|
public function getDMSFileEditFields($file)
|
|
|
|
{
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
// Empty actions, generate default
|
2017-05-01 05:54:48 +02:00
|
|
|
if (empty($this->fileEditFields)) {
|
2016-08-30 04:04:08 +02:00
|
|
|
$fields = $file->getCMSFields();
|
|
|
|
// Only display main tab, to avoid overly complex interface
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($fields->hasTabSet() && ($mainTab = $fields->findOrMakeTab('Root.Main'))) {
|
2016-08-30 04:04:08 +02:00
|
|
|
$fields = $mainTab->Fields();
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fields instance
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($this->fileEditFields instanceof FieldList) {
|
|
|
|
return $this->fileEditFields;
|
|
|
|
}
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
// Method to call on the given file
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($file->hasMethod($this->fileEditFields)) {
|
2016-08-30 04:04:08 +02:00
|
|
|
return $file->{$this->fileEditFields}();
|
|
|
|
}
|
|
|
|
|
|
|
|
user_error("Invalid value for UploadField::fileEditFields", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm
|
|
|
|
* @example 'getCMSActions'
|
|
|
|
*
|
|
|
|
* @param File $file File context to generate form actions for
|
|
|
|
* @return FieldList Field list containing FormAction
|
|
|
|
*/
|
2017-05-01 05:54:48 +02:00
|
|
|
public function getDMSFileEditActions($file)
|
|
|
|
{
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
// Empty actions, generate default
|
2017-05-01 05:54:48 +02:00
|
|
|
if (empty($this->fileEditActions)) {
|
2016-08-30 04:04:08 +02:00
|
|
|
$actions = new FieldList($saveAction = new FormAction('doEdit', _t('UploadField.DOEDIT', 'Save')));
|
|
|
|
$saveAction->addExtraClass('ss-ui-action-constructive icon-accept');
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions instance
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($this->fileEditActions instanceof FieldList) {
|
|
|
|
return $this->fileEditActions;
|
|
|
|
}
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
// Method to call on the given file
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($file->hasMethod($this->fileEditActions)) {
|
2016-08-30 04:04:08 +02:00
|
|
|
return $file->{$this->fileEditActions}();
|
|
|
|
}
|
|
|
|
|
|
|
|
user_error("Invalid value for UploadField::fileEditActions", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the validator to use for the edit form
|
|
|
|
* @example 'getCMSValidator'
|
|
|
|
*
|
|
|
|
* @param File $file File context to generate validator from
|
|
|
|
* @return Validator Validator object
|
|
|
|
*/
|
2017-05-01 05:54:48 +02:00
|
|
|
public function getDMSFileEditValidator($file)
|
|
|
|
{
|
2016-08-30 04:04:08 +02:00
|
|
|
// Empty validator
|
2017-05-01 05:54:48 +02:00
|
|
|
if (empty($this->fileEditValidator)) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
// Validator instance
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($this->fileEditValidator instanceof Validator) {
|
|
|
|
return $this->fileEditValidator;
|
|
|
|
}
|
2016-08-30 04:04:08 +02:00
|
|
|
|
|
|
|
// Method to call on the given file
|
2017-05-01 05:54:48 +02:00
|
|
|
if ($file->hasMethod($this->fileEditValidator)) {
|
2016-08-30 04:04:08 +02:00
|
|
|
return $file->{$this->fileEditValidator}();
|
|
|
|
}
|
|
|
|
|
|
|
|
user_error("Invalid value for UploadField::fileEditValidator", E_USER_ERROR);
|
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the folder name to store DMS files in
|
|
|
|
*
|
|
|
|
* @param string $folderName
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setFolderName($folderName)
|
|
|
|
{
|
|
|
|
$this->folderName = (string) $folderName;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the folder name for storing the document
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFolderName()
|
|
|
|
{
|
|
|
|
return $this->folderName;
|
|
|
|
}
|
2012-07-31 04:24:35 +02:00
|
|
|
}
|