From cd45bb2f4f63bcb05d7e7c48ac8ba9c54e1fa97a Mon Sep 17 00:00:00 2001 From: zauberfisch Date: Thu, 24 Aug 2017 04:16:14 +0000 Subject: [PATCH] Added UploadField subclass to save into serialized DBField --- README.md | 1 + composer.json | 2 +- css/SortableUploadField.scss.css | 13 ++++++++ javascript/SortableUploadField.js | 35 ++++++++++++++++++++++ scss/SortableUploadField.scss | 13 ++++++++ src/Form/SortableUploadField.php | 15 ++++++++++ src/Form/UploadField.php | 49 +++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 css/SortableUploadField.scss.css create mode 100644 javascript/SortableUploadField.js create mode 100644 scss/SortableUploadField.scss create mode 100644 src/Form/SortableUploadField.php create mode 100644 src/Form/UploadField.php diff --git a/README.md b/README.md index cded11d..a869e79 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ The primary motivation for this module is to be able to save collections of data ## Requirements * silverstripe/framework >=3.6 +* zauberfisch/silverstripe-namespace-templates >=1 ## Installation diff --git a/composer.json b/composer.json index 8981995..585a19f 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,6 @@ ], "require": { "silverstripe/framework": "^3.6", - "zauberfisch/silverstripe-namespace-ssviewer": "~1.0" + "zauberfisch/silverstripe-namespace-templates": "^1.0" } } diff --git a/css/SortableUploadField.scss.css b/css/SortableUploadField.scss.css new file mode 100644 index 0000000..043a819 --- /dev/null +++ b/css/SortableUploadField.scss.css @@ -0,0 +1,13 @@ +.ui-sortable .preview img { + cursor: move; +} + +.zauberfisch\\serializeddataobject\\form\\sortableupload .ui-sortable-helper { + background-color: #fff !important; + border: 1px solid #b3b3b3 !important; + border-left: 0 !important; + border-right: 0 !important; + -webkit-box-shadow: 0px 9px 5px -5px rgba(0, 0, 0, 0.3); + -moz-box-shadow: 0px 9px 5px -5px rgba(0, 0, 0, 0.3); + box-shadow: 0px 9px 5px -5px rgba(0, 0, 0, 0.3); +} diff --git a/javascript/SortableUploadField.js b/javascript/SortableUploadField.js new file mode 100644 index 0000000..369fa1b --- /dev/null +++ b/javascript/SortableUploadField.js @@ -0,0 +1,35 @@ +;(function($) { + $(function(){ + $.entwine('ss', function($) { + $(".zauberfisch\\\\serializeddataobject\\\\form\\\\sortableupload.ss-uploadfield ul.ss-uploadfield-files").entwine({ + onmatch: function() { + // enable sorting functionality + var self = this, + rootForm = this.closest('form'); + self.sortable({ + handle: ".ss-uploadfield-item-preview", + axis: "y", + start: function(event, ui){ + // remove overflow on container + ui.item.data("oldPosition", ui.item.index()); + self.css("overflow", "hidden"); + }, + stop: function(event, ui){ + // restore overflow + self.css("overflow", "auto"); + //rootForm.addClass('changed'); + } + }); + this._super(); + }, + onunmatch: function(){ + // clean up + try { + $(this).sortable("destroy"); + } catch(e){} + this._super(); + } + }); + }); + }); +}(jQuery)); diff --git a/scss/SortableUploadField.scss b/scss/SortableUploadField.scss new file mode 100644 index 0000000..043a819 --- /dev/null +++ b/scss/SortableUploadField.scss @@ -0,0 +1,13 @@ +.ui-sortable .preview img { + cursor: move; +} + +.zauberfisch\\serializeddataobject\\form\\sortableupload .ui-sortable-helper { + background-color: #fff !important; + border: 1px solid #b3b3b3 !important; + border-left: 0 !important; + border-right: 0 !important; + -webkit-box-shadow: 0px 9px 5px -5px rgba(0, 0, 0, 0.3); + -moz-box-shadow: 0px 9px 5px -5px rgba(0, 0, 0, 0.3); + box-shadow: 0px 9px 5px -5px rgba(0, 0, 0, 0.3); +} diff --git a/src/Form/SortableUploadField.php b/src/Form/SortableUploadField.php new file mode 100644 index 0000000..32c38c1 --- /dev/null +++ b/src/Form/SortableUploadField.php @@ -0,0 +1,15 @@ +byIDs($ids)->toArray()); + } + + /** + * @param \DataObjectInterface|\DataObject $record + * @return $this + */ + public function saveInto(\DataObjectInterface $record) { + $fieldName = $this->getName(); + if (!$fieldName) { + return $this; + } + if ($record->hasField($fieldName)) { + $info = $record->db($fieldName); + if ($info == DataListField::class) { + // Get details to save + $value = $this->getSerializableList($this->getItemIDs()); + $dbValue = new DataListField(); + $dbValue->setValue($value, null, true); + $record->setField($fieldName, $dbValue); + } + } else { + parent::saveInto($record); + } + return $this; + } + + public function setValue($value, $record = null) { + if (is_string($value) && $value) { + $dbField = new DataListField(); + $dbField->setValue($value, null, true); + $value = $dbField->getValue(); + return parent::setValue(null, $value); + } + return parent::setValue($value, $record); + } +}