diff --git a/administrator/components/com_k2/install.mysql.sql b/administrator/components/com_k2/install.mysql.sql index 96bdf4ef..93d89557 100644 --- a/administrator/components/com_k2/install.mysql.sql +++ b/administrator/components/com_k2/install.mysql.sql @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS `#__k2_attachments` ( `title` varchar(255) NOT NULL, `titleAttribute` text NOT NULL, `hits` int(11) NOT NULL, + `ordering` tinyint(1) UNSIGNED NOT NULL, PRIMARY KEY (`id`), KEY `idx_hits` (`hits`), KEY `idx_itemID` (`itemID`) diff --git a/administrator/components/com_k2/models/item.php b/administrator/components/com_k2/models/item.php index c672b952..c1e735c2 100644 --- a/administrator/components/com_k2/models/item.php +++ b/administrator/components/com_k2/models/item.php @@ -410,6 +410,16 @@ public function save($front = false) $attPost = JRequest::getVar('attachment', null, 'POST', 'array'); $attFiles = JRequest::getVar('attachment', null, 'FILES', 'array'); + $attOrderings = JRequest::getVar('attachmentordering', null, 'POST', 'array'); + $existingAtts = $this->getAttachments($row->id); + foreach ($existingAtts as $key => $attachment){ + $attOrder = array_search($attachment->id, $attOrderings); + $attachment->ordering = $attOrder; + $existingAtts[$key] = $attachment; + $db->setQuery("UPDATE #__k2_attachments SET `ordering` = " . $db->Quote($attOrder) . ' WHERE `id` = ' . $attachment->id)->execute(); // update attachment ordering on item save + } + + $attOrderingI = count($existingAtts); if (is_array($attPost) && count($attPost)) { foreach ($attPost as $key => $attachment) { /* Use the POST array's key as reference */ @@ -431,6 +441,7 @@ public function save($front = false) $attachmentToSave->filename = $filename; $attachmentToSave->title = (empty($attachment['title'])) ? $filename : $attachment['title']; $attachmentToSave->titleAttribute = (empty($attachment['title_attribute'])) ? $filename : $attachment['title_attribute']; + $attachmentToSave->ordering = $attOrderingI; $attachmentToSave->store(); } else { $handle = new Upload($attFiles['tmp_name'][$key]['upload']); @@ -449,12 +460,14 @@ public function save($front = false) $attachmentToSave->filename = $dstName; $attachmentToSave->title = (empty($attachment['title'])) ? $filename : $attachment['title']; $attachmentToSave->titleAttribute = (empty($attachment['title_attribute'])) ? $filename : $attachment['title_attribute']; + $attachmentToSave->ordering = $attOrderingI; $attachmentToSave->store(); } else { $app->enqueueMessage($handle->error, 'error'); $app->redirect('index.php?option=com_k2&view=items'); } } + $attOrderingI++; } } @@ -913,7 +926,7 @@ public function download() public function getAttachments($itemID) { $db = JFactory::getDbo(); - $query = "SELECT * FROM #__k2_attachments WHERE itemID=".(int)$itemID; + $query = "SELECT * FROM #__k2_attachments WHERE itemID=".(int)$itemID." ORDER BY ordering ASC"; $db->setQuery($query); $rows = $db->loadObjectList(); foreach ($rows as $row) { diff --git a/administrator/components/com_k2/views/item/tmpl/default.php b/administrator/components/com_k2/views/item/tmpl/default.php index 6ac5f25b..683dbb38 100644 --- a/administrator/components/com_k2/views/item/tmpl/default.php +++ b/administrator/components/com_k2/views/item/tmpl/default.php @@ -651,31 +651,40 @@
diff --git a/administrator/components/com_k2/views/item/view.html.php b/administrator/components/com_k2/views/item/view.html.php index 6e381f7a..42a483d9 100644 --- a/administrator/components/com_k2/views/item/view.html.php +++ b/administrator/components/com_k2/views/item/view.html.php @@ -355,6 +355,11 @@ public function display($tpl = null) $item->ratingSum = 0; $item->ratingCount = 0; } + $document->addScriptDeclaration(' + jQuery(function () { + jQuery(".itemAttachmentsTable tbody").sortable({handle: ".sortable-handler"}).disableSelection(); + }); +'); // Tags if ($user->gid < 24 && $params->get('lockTags')) { diff --git a/components/com_k2/models/item.php b/components/com_k2/models/item.php index 73ec5631..55e5a0ec 100644 --- a/components/com_k2/models/item.php +++ b/components/com_k2/models/item.php @@ -1413,7 +1413,7 @@ public function getItemAttachments($itemID) return $K2ItemAttachmentsInstances[$itemID]; } $db = JFactory::getDbo(); - $query = "SELECT * FROM #__k2_attachments WHERE itemID=".$itemID; + $query = "SELECT * FROM #__k2_attachments WHERE itemID=".$itemID." ORDER BY ordering ASC"; $db->setQuery($query); $rows = $db->loadObjectList(); foreach ($rows as $row) {