From 407824ec82ef8db7b915fbda828029c1112bf61d Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Fri, 18 Nov 2011 15:03:50 -0800 Subject: [PATCH 1/8] Update to allow MultipleFileAttachmentField to work for objects that have not been extended with "ManyManySortable". --- code/MultipleFileAttachmentField.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index f7dcc4f..c5eaba3 100644 --- a/code/MultipleFileAttachmentField.php +++ b/code/MultipleFileAttachmentField.php @@ -28,7 +28,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 +105,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) { @@ -152,15 +153,17 @@ public function saveInto(DataObject $record) { $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)); + } } } } - } + } } @@ -215,6 +218,10 @@ public function getFileClass (DataObject $record) { } return $file_class; } + + public function isSortable() { + return $this->getForm()->getRecord()->hasExtension('ManyManySortable'); + } } \ No newline at end of file From 5764acce5f44798e64b1ea53a4b050117a108d0d Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Fri, 18 Nov 2011 15:06:33 -0800 Subject: [PATCH 2/8] Update to this template so that it is aware of whether or not the DataObject has been extended with "ManyManySortable". Elements for changing sort order are not displayed and specific hidden fields are not sent to the browser. --- .../Includes/KickAssetFieldFiles_Multi.ss | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) 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 %> -
    - <% control Files %> -
  • -
    - Drag to rearrange order of fields - $Name ($Size) - - - - - - - -
    -
  • - <% end_control %> -
+ <% if isSortable %> +
    + <% control Files %> +
  • +
    + Drag to rearrange order of fields + $Name ($Size) + + + + + + + +
    +
  • + <% end_control %> +
+ <% else %> +
    + <% control Files %> +
  • +
    + $Name ($Size) + + + + + + +
    +
  • + <% end_control %> +
+ <% end_if %> <% else %> <% _t('FileAttachmentField.NOFILESATTACHED','No files attached') %> <% end_if %> From 3e2c7fbeb875a97023dd72bb0606edf77477cac5 Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Fri, 18 Nov 2011 17:14:04 -0800 Subject: [PATCH 3/8] Update to fix sort order problems. ManyManySorted() must always be 'ManyManySort ASC' so I removed the ability to set or change the sort order of the function return. --- code/ManyManySortable.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) 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 From 393d8a5d66fd1a75b91a275c6292fc50490d3afd Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Fri, 18 Nov 2011 17:14:26 -0800 Subject: [PATCH 4/8] Update code/MultipleFileAttachmentField.php --- code/MultipleFileAttachmentField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index c5eaba3..af771f9 100644 --- a/code/MultipleFileAttachmentField.php +++ b/code/MultipleFileAttachmentField.php @@ -105,13 +105,12 @@ public function Files() { if(is_array($val)) { $list = implode(',', $val); - if ($many_many_parent->hasExtension('ManyManySortable') && method_exists($many_many_parent, "ManyManySorted")) { + if ($this->isSortable()) { $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) { @@ -151,15 +150,16 @@ public function saveInto(DataObject $record) { if($relation_name = $this->getForeignRelationName($record)) { // Assign all the new relations (may have already existed) $data = $_REQUEST; + //Debug::show($data); for($count = 0; $count < count($data[$this->name]); ++$count) { $id = $data[$this->name][$count]; if($file = DataObject::get_by_id("File", $id)) { $new = ($file_class != "File") ? $file->newClassInstance($file_class) : $file; - $new->write(); if ($this->isSortable()){ $sort = $data['sort'][$count]; $currentComponentSet->add($new, array('ManyManySort'=>$sort)); } + $new->write(); } } } From e323d6620f2e6ed430a23d6add58597ae9f84675 Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Mon, 21 Nov 2011 11:25:52 -0800 Subject: [PATCH 5/8] Update code/MultipleFileAttachmentField.php --- code/MultipleFileAttachmentField.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index af771f9..8d2161a 100644 --- a/code/MultipleFileAttachmentField.php +++ b/code/MultipleFileAttachmentField.php @@ -3,6 +3,9 @@ * Provides an interface for attaching multiple files associated with * a Page or DataObject. Files can be chosen from exting assets in {@link KickAssetAdmin} * + * To allow files managed using this interface to be sorted you must add the ManyManySortable Decorator to + * the DataObject that the relation ship is set on. See ManyManySortable.php for more information on how to do this. + * * @package KickAssets * @author UncleCheese */ From 3681bf008388318f62c0a89c426404ec11abb202 Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Mon, 21 Nov 2011 17:36:14 -0800 Subject: [PATCH 6/8] Update code/MultipleFileAttachmentField.php --- code/MultipleFileAttachmentField.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index 8d2161a..4950c8a 100644 --- a/code/MultipleFileAttachmentField.php +++ b/code/MultipleFileAttachmentField.php @@ -3,9 +3,6 @@ * Provides an interface for attaching multiple files associated with * a Page or DataObject. Files can be chosen from exting assets in {@link KickAssetAdmin} * - * To allow files managed using this interface to be sorted you must add the ManyManySortable Decorator to - * the DataObject that the relation ship is set on. See ManyManySortable.php for more information on how to do this. - * * @package KickAssets * @author UncleCheese */ @@ -153,16 +150,19 @@ public function saveInto(DataObject $record) { if($relation_name = $this->getForeignRelationName($record)) { // Assign all the new relations (may have already existed) $data = $_REQUEST; - //Debug::show($data); for($count = 0; $count < count($data[$this->name]); ++$count) { $id = $data[$this->name][$count]; if($file = DataObject::get_by_id("File", $id)) { $new = ($file_class != "File") ? $file->newClassInstance($file_class) : $file; + $new->write(); if ($this->isSortable()){ $sort = $data['sort'][$count]; $currentComponentSet->add($new, array('ManyManySort'=>$sort)); } - $new->write(); + else { + $currentComponentSet->add($new); + } + } } } From 62effdae3e9451f6c329e576aaff2ed55382ca03 Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Thu, 19 Jul 2012 12:15:54 -0700 Subject: [PATCH 7/8] Update code/MultipleFileAttachmentField.php --- code/MultipleFileAttachmentField.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index 4950c8a..d4810d2 100644 --- a/code/MultipleFileAttachmentField.php +++ b/code/MultipleFileAttachmentField.php @@ -105,12 +105,13 @@ public function Files() { if(is_array($val)) { $list = implode(',', $val); - if ($this->isSortable()) { + 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,8 +149,10 @@ 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]; if($file = DataObject::get_by_id("File", $id)) { @@ -162,7 +165,6 @@ public function saveInto(DataObject $record) { else { $currentComponentSet->add($new); } - } } } From e8f8e8e1fd008d605ffe9c27c343499238e0cae4 Mon Sep 17 00:00:00 2001 From: Micah Sheets Date: Thu, 19 Jul 2012 12:17:35 -0700 Subject: [PATCH 8/8] Update SaveInto function so that non sortable object relationships are saved. Without this attached files using MultiFileAttachementField would upload but would not attach to the object. --- code/MultipleFileAttachmentField.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/code/MultipleFileAttachmentField.php b/code/MultipleFileAttachmentField.php index d4810d2..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