Skip to content
13 changes: 2 additions & 11 deletions code/ManyManySortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
}
}
24 changes: 17 additions & 7 deletions code/MultipleFileAttachmentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/

class MultipleFileAttachmentField extends KickAssetField {



/**
* @var boolean A simple template variable that states whether this is a multiple
Expand All @@ -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');
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
}
}
}
}


Expand Down Expand Up @@ -215,6 +221,10 @@ public function getFileClass (DataObject $record) {
}
return $file_class;
}

public function isSortable() {
return $this->getForm()->getRecord()->hasExtension('ManyManySortable');
}


}
53 changes: 36 additions & 17 deletions templates/Includes/KickAssetFieldFiles_Multi.ss
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,43 @@
<div class="file_info">

<div class="file_name">

<% if Files %>
<ul id="sortable">
<% control Files %>
<li class="sortableli" style="cursor: default;">
<div class="file_block">
<img class="fieldHandler" alt="Drag to rearrange order of fields" src="sapphire/images/drag.gif" style="cursor: move;">
<span class="thumb"><img src="$Thumb" height="24" /></span> <span class="name">$Name</span> <span class="size">($Size)</span>
<span class="multi_actions">
<a href="$EditLink" class="file_attach_btn" data-id="$ID" title="<% _t('FileAttachmentField.EDIT','Edit') %>"><img src="kickassets/images/edit.png" height="14" /></a>
<a href="javascript:void(0);" class="detach_btn" data-id="$ID" title="<% _t('FileAttachmentField.DETACH','Detach') %>"><img src="kickassets/images/remove.png" height="14" /></a>
<a href="$RemoveLink" class="delete_btn" data-confirmtext="<% _t('FileAttachmentField.AREYOUSURE','Are you sure you want to delete this file permanently?') %>" data-id="$ID" title="<% _t('FileAttachmentField.DELETEPERMANENTLY','Delete permanently') %>"><img src="kickassets/images/delete.png" height="14" /></a>
</span>
<input type="hidden" name="{$Top.Name}[]" value="$ID" />
<input type="hidden" class="sortHidden" name="sort[]" value="$POS" />
</div>
</li>
<% end_control %>
</ul>
<% if isSortable %>
<ul id="sortable">
<% control Files %>
<li class="sortableli" style="cursor: default;">
<div class="file_block">
<img class="fieldHandler" alt="Drag to rearrange order of fields" src="sapphire/images/drag.gif" style="cursor: move;">
<span class="thumb"><img src="$Thumb" height="24" /></span> <span class="name">$Name</span> <span class="size">($Size)</span>
<span class="multi_actions">
<a href="$EditLink" class="file_attach_btn" data-id="$ID" title="<% _t('FileAttachmentField.EDIT','Edit') %>"><img src="kickassets/images/edit.png" height="14" /></a>
<a href="javascript:void(0);" class="detach_btn" data-id="$ID" title="<% _t('FileAttachmentField.DETACH','Detach') %>"><img src="kickassets/images/remove.png" height="14" /></a>
<a href="$RemoveLink" class="delete_btn" data-confirmtext="<% _t('FileAttachmentField.AREYOUSURE','Are you sure you want to delete this file permanently?') %>" data-id="$ID" title="<% _t('FileAttachmentField.DELETEPERMANENTLY','Delete permanently') %>"><img src="kickassets/images/delete.png" height="14" /></a>
</span>
<input type="hidden" name="{$Top.Name}[]" value="$ID" />
<input type="hidden" class="sortHidden" name="sort[]" value="$POS" />
</div>
</li>
<% end_control %>
</ul>
<% else %>
<ul>
<% control Files %>
<li>
<div class="file_block">
<span class="thumb"><img src="$Thumb" height="24" /></span> <span class="name">$Name</span> <span class="size">($Size)</span>
<span class="multi_actions">
<a href="$EditLink" class="file_attach_btn" data-id="$ID" title="<% _t('FileAttachmentField.EDIT','Edit') %>"><img src="kickassets/images/edit.png" height="14" /></a>
<a href="javascript:void(0);" class="detach_btn" data-id="$ID" title="<% _t('FileAttachmentField.DETACH','Detach') %>"><img src="kickassets/images/remove.png" height="14" /></a>
<a href="$RemoveLink" class="delete_btn" data-confirmtext="<% _t('FileAttachmentField.AREYOUSURE','Are you sure you want to delete this file permanently?') %>" data-id="$ID" title="<% _t('FileAttachmentField.DELETEPERMANENTLY','Delete permanently') %>"><img src="kickassets/images/delete.png" height="14" /></a>
</span>
<input type="hidden" name="{$Top.Name}[]" value="$ID" />
</div>
</li>
<% end_control %>
</ul>
<% end_if %>
<% else %>
<% _t('FileAttachmentField.NOFILESATTACHED','No files attached') %>
<% end_if %>
Expand Down