diff --git a/code/ManyManySortable.php b/code/ManyManySortable.php index 2bb4f65..bbaad61 100644 --- a/code/ManyManySortable.php +++ b/code/ManyManySortable.php @@ -23,11 +23,6 @@ class ManyManySortable extends DataObjectDecorator { static $many_many_sortable_relations = array(); - static $sort_dir = "ASC"; - - public static function set_sort_dir($dir) { - self::$sort_dir = $dir; - } /** * Used in _config to set up any many_many sortable relationships. @@ -61,13 +56,9 @@ public static function add_sortable_many_many_relation($ownerClass,$componentNam /** * Used in decorated classes to access the ManyManySorted objects. - * - * @param string either 'ASC' or 'DESC' - * */ - function ManyManySorted($sortdir = null) { - $sortDirection = ($sortdir) ? $sortdir : self::$sort_dir; + function ManyManySorted() { $functionname = self::$many_many_sortable_relations[$this->owner->ClassName]['relationName']; - return $this->owner->$functionname(null, 'ManyManySort '.$sortDirection); + return $this->owner->$functionname(null, 'ManyManySort ASC'); // Sort order must not be changed or MultipleFileAttachmentField will no longer work properly. } } \ No newline at end of file diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index f7dcc4f..7a0dade 100644 --- a/code/MultipleFileAttachmentField.php +++ b/code/MultipleFileAttachmentField.php @@ -8,8 +8,6 @@ */ class MultipleFileAttachmentField extends KickAssetField { - - /** * @var boolean A simple template variable that states whether this is a multiple @@ -28,7 +26,7 @@ class MultipleFileAttachmentField extends KickAssetField { * FieldHolder overriden here so we can add javascript required for manymanysortable */ public function FieldHolder() { - if ($this->getForm()->getRecord()->hasExtension('ManyManySortable')){ + if ($this->isSortable()){ Requirements::javascript(SAPPHIRE_DIR ."/thirdparty/jquery-ui/jquery-ui-1.8rc3.custom.js"); Requirements::javascript('kickassets/javascript/manymanysortable.js'); } @@ -105,12 +103,13 @@ public function Files() { if(is_array($val)) { $list = implode(',', $val); - if ($many_many_parent->hasExtension('ManyManySortable')) { + if ($many_many_parent->hasExtension('ManyManySortable') && method_exists($many_many_parent, "ManyManySorted")) { $files = $many_many_parent->ManyManySorted(); } else { $files = DataObject::get("File", "\"File\".\"ID\" IN (".Convert::raw2sql($list).")"); } + $files = DataObject::get("File", "\"File\".\"ID\" IN (".Convert::raw2sql($list).")"); if($files->Count() > 0) { $ret = new DataObjectSet(); foreach($files as $file) { @@ -148,19 +147,26 @@ public function saveInto(DataObject $record) { if(isset($_REQUEST[$this->name]) && is_array($_REQUEST[$this->name])) { if($relation_name = $this->getForeignRelationName($record)) { + // Assign all the new relations (may have already existed) $data = $_REQUEST; + for($count = 0; $count < count($data[$this->name]); ++$count) { $id = $data[$this->name][$count]; - $sort = $data['sort'][$count]; if($file = DataObject::get_by_id("File", $id)) { $new = ($file_class != "File") ? $file->newClassInstance($file_class) : $file; $new->write(); - $currentComponentSet->add($new, array('ManyManySort'=>$sort)); + if ($this->isSortable()){ + $sort = $data['sort'][$count]; + $currentComponentSet->add($new, array('ManyManySort'=>$sort)); + } + else { + $currentComponentSet->add($new); + } } } } - } + } } @@ -215,6 +221,10 @@ public function getFileClass (DataObject $record) { } return $file_class; } + + public function isSortable() { + return $this->getForm()->getRecord()->hasExtension('ManyManySortable'); + } } \ No newline at end of file diff --git a/templates/Includes/KickAssetFieldFiles_Multi.ss b/templates/Includes/KickAssetFieldFiles_Multi.ss index 29f8de5..45d0b55 100644 --- a/templates/Includes/KickAssetFieldFiles_Multi.ss +++ b/templates/Includes/KickAssetFieldFiles_Multi.ss @@ -5,24 +5,43 @@
+ <% if Files %> - + <% if isSortable %> + + <% else %> + + <% end_if %> <% else %> <% _t('FileAttachmentField.NOFILESATTACHED','No files attached') %> <% end_if %>