- <?php
- class FrontendUploadField extends UploadField {
- public function __construct($name, $title = null, SS_List $items = null) {
- parent::__construct($name, $title, $items);
- $this->setConfig('autoUpload', true)
- ->setConfig('canAttachExisting', false)
- ->setConfig('canPreviewFolder', false)
- ->setConfig('changeDetection', false)
- ->setTemplateFileButtons('FrontendUploadField_FileButtons');
- $this->getUpload()->setReplaceFile(false);
- }
- protected function saveTemporaryFile($tmpFile, &$error = null) {
- // Determine container object
- $error = null;
- $fileObject = new ImageExtended();
- if (empty($tmpFile)) {
- $error = _t('UploadField.FIELDNOTSET', 'File information not found');
- return null;
- }
- if($tmpFile['error']) {
- $error = $tmpFile['error'];
- return null;
- }
- // prefix with unique id to avoid file name conflicts
- $title = $tmpFile['name'];
- $tmpFile['name'] = uniqid('', true) . $tmpFile['name'];
- // Allow replacing files (rather than renaming a duplicate) when warning about overwrites
- if($this->getConfig('overwriteWarning')) {
- $this->upload->setReplaceFile(true);
- }
- // Get the uploaded file into a new file object.
- try {
- $this->upload->loadIntoFile($tmpFile, $fileObject, $this->getFolderName());
- } catch (Exception $e) {
- // we shouldn't get an error here, but just in case
- $error = $e->getMessage();
- return null;
- }
- // Check if upload field has an error
- if ($this->upload->isError()) {
- $error = implode(' ' . PHP_EOL, $this->upload->getErrors());
- return null;
- }
- // return file
- $fileObject = $this->upload->getFile();
- // reset the title to the original title (otherwise it would contain the uid we have prepended the filename earlier)
- $fileObject->Title = $title;
- $fileObject->write();
- return $fileObject;
- }
- public function attach(SS_HTTPRequest $request) {
- return $this->httpError(403);
- }
- public function handleItem(SS_HTTPRequest $request) {
- return $this->httpError(403);
- }
- public function handleSelect(SS_HTTPRequest $request) {
- return $this->httpError(403);
- }
- }