Skip to content

Commit

Permalink
Added UploadField subclass to save into serialized DBField
Browse files Browse the repository at this point in the history
  • Loading branch information
Zauberfisch committed Aug 24, 2017
1 parent bcc1afa commit cd45bb2
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
],
"require": {
"silverstripe/framework": "^3.6",
"zauberfisch/silverstripe-namespace-ssviewer": "~1.0"
"zauberfisch/silverstripe-namespace-templates": "^1.0"
}
}
13 changes: 13 additions & 0 deletions css/SortableUploadField.scss.css
Original file line number Diff line number Diff line change
@@ -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);
}
35 changes: 35 additions & 0 deletions javascript/SortableUploadField.js
Original file line number Diff line number Diff line change
@@ -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));
13 changes: 13 additions & 0 deletions scss/SortableUploadField.scss
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 15 additions & 0 deletions src/Form/SortableUploadField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace zauberfisch\SerializedDataObject\Form;

/**
* @author Zauberfisch
*/
class SortableUploadField extends UploadField {
public function Field($properties = []) {
\Requirements::javascript(SERIALIZED_DATAOBJECT_DIR . '/javascript/SortableUploadField.js');
//\Requirements::css(SERIALIZED_DATAOBJECT_DIR . '/scss/SortableUploadField.scss');
\Requirements::css(SERIALIZED_DATAOBJECT_DIR . '/css/SortableUploadField.scss.css');
return parent::Field($properties);
}
}
49 changes: 49 additions & 0 deletions src/Form/UploadField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace zauberfisch\SerializedDataObject\Form;

use zauberfisch\SerializedDataObject\DataList;
use zauberfisch\SerializedDataObject\DBField\DataListField;

/**
* @author Zauberfisch
*/
class UploadField extends \UploadField {
protected function getSerializableList($ids) {
return new DataList(\File::get()->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);
}
}

0 comments on commit cd45bb2

Please sign in to comment.