diff --git a/README.md b/README.md index d272ae7..c71b997 100644 --- a/README.md +++ b/README.md @@ -35,16 +35,20 @@ The Dropzone module provides `FileAttachmentField`, a robust HTML5 uploading int ## Usage -The field instantiates similarly to `UploadField`, taking the name of the file relationship and a label, as the first two arguments. Once instantiated, there are many ways to configure the UI. +The field instantiates similarly to `UploadField`, taking the name of the file relationship and a label, as the first +two arguments. Once instantiated, there are many ways to configure the UI. ```php FileAttachmentField::create('MyFile', 'Upload a file') ->setView('grid') ``` -If the form holding the upload field is bound to a record, (i.e. with `loadDataFrom()`), the upload field will automatically allow multiple files if the relation is a `has_many` or `many_many`. If the form is not bound to a record, you can use `setMultiple(true)`. +If the form holding the upload field is bound to a record, (i.e. with `loadDataFrom()`), the upload field will +automatically allow multiple files if the relation is a `has_many` or `many_many`. If the form is not bound to a record, +you can use `setMultiple(true)`. -Image-only uploads can be forced using the `imagesOnly()` method. If the form is bound to a record, and the relation points to an `Image` class, this will be automatically set. +Image-only uploads can be forced using the `imagesOnly()` method. If the form is bound to a record, and the relation +points to an `Image` class, this will be automatically set. ### More advanced options diff --git a/_config.php b/_config.php deleted file mode 100644 index 0321591..0000000 --- a/_config.php +++ /dev/null @@ -1,3 +0,0 @@ -permissions['upload'] = true; $this->permissions['detach'] = true; $this->permissions['delete'] = function () use ($instance) { - return Injector::inst()->get('File')->canDelete() && $instance->isCMS(); + return Injector::inst()->get(File::class)->canDelete() && $instance->isCMS(); }; $this->permissions['attach'] = function () use ($instance) { return $instance->isCMS(); @@ -175,12 +209,12 @@ public function SmallFieldHolder($attributes = array ()) { * Define some requirements and settings just before rendering the Field Holder. */ protected function defineFieldHolderRequirements() { - Requirements::javascript(DROPZONE_DIR.'/javascript/dropzone.js'); - Requirements::javascript(DROPZONE_DIR.'/javascript/file_attachment_field.js'); + Requirements::javascript('unclecheese/dropzone:/javascript/dropzone.js'); + Requirements::javascript('unclecheese/dropzone:/javascript/file_attachment_field.js'); if($this->isCMS()) { - Requirements::javascript(DROPZONE_DIR.'/javascript/file_attachment_field_backend.js'); + Requirements::javascript('unclecheese/dropzone:/javascript/file_attachment_field_backend.js'); } - Requirements::css(DROPZONE_DIR.'/css/file_attachment_field.css'); + Requirements::css('unclecheese/dropzone:/css/file_attachment_field.css'); if(!$this->getSetting('url')) { $this->settings['url'] = $this->Link('upload'); @@ -365,14 +399,17 @@ public function setMaxThumbnailFilesize($num) { * @return void */ public function addValidFileIDs(array $ids) { - $validIDs = Session::get('FileAttachmentField.validFileIDs'); + $request = Injector::inst()->get(HTTPRequest::class); + $session = $request->getSession(); + + $validIDs = $session->get('FileAttachmentField.validFileIDs'); if (!$validIDs) { $validIDs = array(); } foreach ($ids as $id) { $validIDs[$id] = $id; } - Session::set('FileAttachmentField.validFileIDs', $validIDs); + $session->set('FileAttachmentField.validFileIDs', $validIDs); } /** @@ -382,7 +419,9 @@ public function addValidFileIDs(array $ids) { * @return array */ public function getValidFileIDs() { - $validIDs = Session::get('FileAttachmentField.validFileIDs'); + $request = Injector::inst()->get(HTTPRequest::class); + $session = $request->getSession(); + $validIDs = $session->get('FileAttachmentField.validFileIDs'); if ($validIDs && is_array($validIDs)) { return $validIDs; } @@ -788,7 +827,7 @@ private function getUploadUserError($code) { * @return SS_HTTPResponse * @return SS_HTTPResponse */ - public function upload(SS_HTTPRequest $request) { + public function upload(HTTPRequest $request) { $name = $this->getSetting('paramName'); $files = (!empty($_FILES[$name]) ? $_FILES[$name] : array()); @@ -840,7 +879,7 @@ public function upload(SS_HTTPRequest $request) { return $this->httpError(400, $user_message); } if($relationClass = $this->getFileClass($tmpFile['name'])) { - $fileObject = Object::create($relationClass); + $fileObject = new $relationClass(); } try { @@ -867,7 +906,7 @@ public function upload(SS_HTTPRequest $request) { if (!$formController instanceof LeftAndMain) { $trackFile->setRecord($formController->getRecord()); } - } else if ($formClass !== 'Form') { + } else if ($formClass !== Form::class) { $trackFile->ControllerClass = $formClass; } else { // If using generic 'Form' instance, get controller @@ -879,7 +918,8 @@ public function upload(SS_HTTPRequest $request) { } $this->addValidFileIDs($ids); - return new SS_HTTPResponse(implode(',', $ids), 200); + $this->extend('onAfterUploadFiles', $ids); + return new HTTPResponse(implode(',', $ids), 200); } @@ -887,7 +927,7 @@ public function upload(SS_HTTPRequest $request) { * @param SS_HTTPRequest $request * @return UploadField_ItemHandler */ - public function handleSelect(SS_HTTPRequest $request) { + public function handleSelect(HTTPRequest $request) { if($this->isDisabled() || $this->isReadonly() || !$this->CanAttach()) { return $this->httpError(403); } @@ -933,7 +973,7 @@ public function IsMultiple() { } if($record = $this->getRecord()) { - return ($record->many_many($this->getName()) || $record->has_many($this->getName())); + return ($record->manyMany($this->getName()) || $record->hasMany($this->getName())); } return false; @@ -994,7 +1034,7 @@ public function AttachedFiles() { * @return string */ public function RootThumbnailsDir() { - return $this->getSetting('thumbnailsDir') ?: DROPZONE_DIR.'/images/file-icons'; + return $this->getSetting('thumbnailsDir') ?: 'unclecheese/dropzone:/images/file-icons'; } /** @@ -1142,15 +1182,15 @@ public function getFileClass($filename = null) { if($record) { $class = $record->getRelationClass($name); - if(!$class) $class = "File"; + if(!$class) $class = File::class; } if($filename) { - if($defaultClass == "Image" && + if($defaultClass == Image::class && $this->config()->upgrade_images && !Injector::inst()->get($class) instanceof Image ) { - $class = "Image"; + $class = Image::class; } } @@ -1166,7 +1206,7 @@ public function getRecord() { if (($record = $this->form->getRecord()) && ($record instanceof DataObject)) { $this->record = $record; } - elseif (($controller = $this->form->Controller()) + elseif (($controller = $this->form->getController()) && $controller->hasMethod('data') && ($record = $controller->data()) && ($record instanceof DataObject) @@ -1221,9 +1261,9 @@ protected function getSetting($setting) { * @return array */ protected function getDefaults() { - $file_path = BASE_PATH.'/'.DROPZONE_DIR.'/'.$this->config()->default_config_path; + $file_path = BASE_PATH.'/'.'dropzone/'.$this->config()->default_config_path; if(!file_exists($file_path)) { - throw new Exception("FileAttachmentField::getDefaults() - There is no config json file at $file_path"); + throw new \Exception("FileAttachmentField::getDefaults() - There is no config json file at $file_path"); } return Convert::json2array(file_get_contents($file_path)); @@ -1300,76 +1340,4 @@ public function getConfigJSON() { } } -class FileAttachmentField_SelectHandler extends UploadField_SelectHandler { - - private static $allowed_actions = array ( - 'filesbyid', - ); - - /** - * @param $folderID The ID of the folder to display. - * @return FormField - */ - protected function getListField($folderID) { - // Generate the folder selection field. - $folderField = new TreeDropdownField('ParentID', _t('HtmlEditorField.FOLDER', 'Folder'), 'Folder'); - $folderField->setValue($folderID); - - // Generate the file list field. - $config = GridFieldConfig::create(); - $config->addComponent(new GridFieldSortableHeader()); - $config->addComponent(new GridFieldFilterHeader()); - $config->addComponent($columns = new GridFieldDataColumns()); - $columns->setDisplayFields(array( - 'StripThumbnail' => '', - 'Name' => 'Name', - 'Title' => 'Title' - )); - $config->addComponent(new GridFieldPaginator(8)); - - // If relation is to be autoset, we need to make sure we only list compatible objects. - $baseClass = $this->parent->getFileClass(); - - // Create the data source for the list of files within the current directory. - $files = DataList::create($baseClass)->filter('ParentID', $folderID); - - $fileField = new GridField('Files', false, $files, $config); - $fileField->setAttribute('data-selectable', true); - if($this->parent->IsMultiple()) { - $fileField->setAttribute('data-multiselect', true); - } - $selectComposite = new CompositeField( - $folderField, - $fileField - ); - - return $selectComposite; - } - - - public function filesbyid(SS_HTTPRequest $r) { - $ids = $r->getVar('ids'); - $files = File::get()->byIDs(explode(',',$ids)); - - $validIDs = array(); - $json = array (); - foreach($files as $file) { - $template = new SSViewer('FileAttachmentField_attachments'); - $html = $template->process(ArrayData::create(array( - 'File' => $file, - 'Scope' => $this->parent - ))); - - $validIDs[$file->ID] = $file->ID; - $json[] = array ( - 'id' => $file->ID, - 'html' => $html->forTemplate() - ); - } - - $this->parent->addValidFileIDs($validIDs); - return Convert::array2json($json); - } - -} diff --git a/code/FileAttachmentFieldCleanTask.php b/code/FileAttachmentFieldCleanTask.php index df6983a..fc3fa77 100644 --- a/code/FileAttachmentFieldCleanTask.php +++ b/code/FileAttachmentFieldCleanTask.php @@ -1,4 +1,8 @@ 'File', + 'File' => File::class, ); public static function untrack($fileIDs) { diff --git a/code/FileAttachmentField_SelectHandler.php b/code/FileAttachmentField_SelectHandler.php new file mode 100644 index 0000000..2f622cb --- /dev/null +++ b/code/FileAttachmentField_SelectHandler.php @@ -0,0 +1,84 @@ +setValue($folderID); + + // Generate the file list field. + $config = GridFieldConfig::create(); + $config->addComponent(new GridFieldSortableHeader()); + $config->addComponent(new GridFieldFilterHeader()); + $config->addComponent($columns = new GridFieldDataColumns()); + $columns->setDisplayFields(array( + 'StripThumbnail' => '', + 'Name' => 'Name', + 'Title' => 'Title' + )); + $config->addComponent(new GridFieldPaginator(8)); + + // If relation is to be autoset, we need to make sure we only list compatible objects. + $baseClass = $this->parent->getFileClass(); + + // Create the data source for the list of files within the current directory. + $files = DataList::create($baseClass)->filter('ParentID', $folderID); + + $fileField = new GridField('Files', false, $files, $config); + $fileField->setAttribute('data-selectable', true); + if($this->parent->IsMultiple()) { + $fileField->setAttribute('data-multiselect', true); + } + + $selectComposite = new CompositeField( + $folderField, + $fileField + ); + + return $selectComposite; + } + + + public function filesbyid(HTTPRequest $r) { + $ids = $r->getVar('ids'); + $files = File::get()->byIDs(explode(',',$ids)); + + $validIDs = array(); + $json = array (); + foreach($files as $file) { + $template = new SSViewer('FileAttachmentField_attachments'); + $html = $template->process(ArrayData::create(array( + 'File' => $file, + 'Scope' => $this->parent + ))); + + $validIDs[$file->ID] = $file->ID; + $json[] = array ( + 'id' => $file->ID, + 'html' => $html->forTemplate() + ); + } + + $this->parent->addValidFileIDs($validIDs); + return Convert::array2json($json); + } + +} diff --git a/composer.json b/composer.json index 1341e71..1e790ea 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,21 @@ ], "require": { - "silverstripe/framework": "3.*" + "silverstripe/framework": "4.*" }, + "autoload": { + "psr-4": { + "UncleCheese\\DropZone\\": "code/" + } + }, + "minimum-stability": "dev", + "prefer-stable": true, "extra": { - "installer-name": "dropzone" + "installer-name": "dropzone", + "expose": [ + "css", + "javascript", + "images" + ] } } diff --git a/templates/forms/FileAttachmentField_holder.ss b/templates/UncleCheese/DropZone/FileAttachmentField_holder.ss similarity index 100% rename from templates/forms/FileAttachmentField_holder.ss rename to templates/UncleCheese/DropZone/FileAttachmentField_holder.ss diff --git a/templates/forms/FileAttachmentField_holder_small.ss b/templates/UncleCheese/DropZone/FileAttachmentField_holder_small.ss similarity index 100% rename from templates/forms/FileAttachmentField_holder_small.ss rename to templates/UncleCheese/DropZone/FileAttachmentField_holder_small.ss diff --git a/templates/Includes/FileAttachmentField_attachments.ss b/templates/UncleCheese/DropZone/Includes/FileAttachmentField_attachments.ss similarity index 89% rename from templates/Includes/FileAttachmentField_attachments.ss rename to templates/UncleCheese/DropZone/Includes/FileAttachmentField_attachments.ss index da0795a..023c620 100644 --- a/templates/Includes/FileAttachmentField_attachments.ss +++ b/templates/UncleCheese/DropZone/Includes/FileAttachmentField_attachments.ss @@ -28,13 +28,13 @@ <% if $Scope.CanDetach %> <%t Dropzone.DETACHFILE 'remove' %> - + <% end_if %> <% if $Scope.CanDelete %> <%t Dropzone.MARKFORDELETION 'delete' %> - + <% end_if %> @@ -44,7 +44,7 @@
<%t Dropzone.REMOVED 'removed' %>
<%t Dropzone.CHANGEAFTERSAVE 'The change will take effect after you save.' %> - +
<% end_if %> @@ -53,7 +53,7 @@
<%t Dropzone.DELETED 'deleted' %>
<%t Dropzone.CHANGEAFTERSAVE 'The change will take effect after you save.' %> - +
<% end_if %> diff --git a/templates/Includes/FileAttachmentField_preview.ss b/templates/UncleCheese/DropZone/Includes/FileAttachmentField_preview.ss similarity index 83% rename from templates/Includes/FileAttachmentField_preview.ss rename to templates/UncleCheese/DropZone/Includes/FileAttachmentField_preview.ss index f4d9930..3f66c31 100644 --- a/templates/Includes/FileAttachmentField_preview.ss +++ b/templates/UncleCheese/DropZone/Includes/FileAttachmentField_preview.ss @@ -1,7 +1,7 @@
  • - + @@ -20,13 +20,13 @@ - +
    <%t Dropzone.ERROR 'Oh no!' %>
    - +