1. <?php
  2. class FrontendUploadField extends UploadField {
  3. public function __construct($name, $title = null, SS_List $items = null) {
  4. parent::__construct($name, $title, $items);
  5. $this->setConfig('autoUpload', true)
  6. ->setConfig('canAttachExisting', false)
  7. ->setConfig('canPreviewFolder', false)
  8. ->setConfig('changeDetection', false)
  9. ->setTemplateFileButtons('FrontendUploadField_FileButtons');
  10. $this->getUpload()->setReplaceFile(false);
  11. }
  12. protected function saveTemporaryFile($tmpFile, &$error = null) {
  13. // Determine container object
  14. $error = null;
  15. $fileObject = new ImageExtended();
  16. if (empty($tmpFile)) {
  17. $error = _t('UploadField.FIELDNOTSET', 'File information not found');
  18. return null;
  19. }
  20. if($tmpFile['error']) {
  21. $error = $tmpFile['error'];
  22. return null;
  23. }
  24. // prefix with unique id to avoid file name conflicts
  25. $title = $tmpFile['name'];
  26. $tmpFile['name'] = uniqid('', true) . $tmpFile['name'];
  27. // Allow replacing files (rather than renaming a duplicate) when warning about overwrites
  28. if($this->getConfig('overwriteWarning')) {
  29. $this->upload->setReplaceFile(true);
  30. }
  31. // Get the uploaded file into a new file object.
  32. try {
  33. $this->upload->loadIntoFile($tmpFile, $fileObject, $this->getFolderName());
  34. } catch (Exception $e) {
  35. // we shouldn't get an error here, but just in case
  36. $error = $e->getMessage();
  37. return null;
  38. }
  39. // Check if upload field has an error
  40. if ($this->upload->isError()) {
  41. $error = implode(' ' . PHP_EOL, $this->upload->getErrors());
  42. return null;
  43. }
  44. // return file
  45. $fileObject = $this->upload->getFile();
  46. // reset the title to the original title (otherwise it would contain the uid we have prepended the filename earlier)
  47. $fileObject->Title = $title;
  48. $fileObject->write();
  49. return $fileObject;
  50. }
  51. public function attach(SS_HTTPRequest $request) {
  52. return $this->httpError(403);
  53. }
  54. public function handleItem(SS_HTTPRequest $request) {
  55. return $this->httpError(403);
  56. }
  57. public function handleSelect(SS_HTTPRequest $request) {
  58. return $this->httpError(403);
  59. }
  60. }