Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering for K2 item attachments #511

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions administrator/components/com_k2/install.mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
15 changes: 14 additions & 1 deletion administrator/components/com_k2/models/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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']);
Expand All @@ -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++;
}
}

Expand Down Expand Up @@ -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) {
Expand Down
55 changes: 32 additions & 23 deletions administrator/components/com_k2/views/item/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,31 +651,40 @@
<div class="itemAttachments">
<?php if ($this->row->attachments && count($this->row->attachments)): ?>
<table class="itemAttachmentsTable">
<tr>
<th><?php echo JText::_('K2_FILENAME'); ?></th>
<th><?php echo JText::_('K2_TITLE'); ?></th>
<th><?php echo JText::_('K2_TITLE_ATTRIBUTE'); ?></th>
<th><?php echo JText::_('K2_DOWNLOADS'); ?></th>
<th class="k2Center"><?php echo JText::_('K2_OPERATIONS'); ?></th>
</tr>
<thead>
<tr>
<th class="k2ui-center k2ui-hide-on-mobile" width="1%"><i class="icon-menu-2"></i></th>
<th><?php echo JText::_('K2_FILENAME'); ?></th>
<th><?php echo JText::_('K2_TITLE'); ?></th>
<th><?php echo JText::_('K2_TITLE_ATTRIBUTE'); ?></th>
<th><?php echo JText::_('K2_DOWNLOADS'); ?></th>
<th class="k2Center"><?php echo JText::_('K2_OPERATIONS'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach($this->row->attachments as $attachment): ?>
<tr>
<td class="attachment_entry"><?php echo $attachment->filename; ?></td>
<td><?php echo $attachment->title; ?></td>
<td><?php echo $attachment->titleAttribute; ?></td>
<td><?php echo $attachment->hits; ?></td>
<td class="k2Center">
<a class="downloadAttachmentButton" href="<?php echo $attachment->link; ?>" title="<?php echo JText::_('K2_DOWNLOAD'); ?>">
<i class="fa fa-download"></i>
<span class="hidden"><?php echo JText::_('K2_DOWNLOAD'); ?></span>
</a>
<a class="deleteAttachmentButton" title="<?php echo JText::_('K2_DELETE'); ?>" href="<?php echo JURI::base(true); ?>/index.php?option=com_k2&amp;view=item&amp;task=deleteAttachment&amp;id=<?php echo $attachment->id?>&amp;cid=<?php echo $this->row->id; ?>">
<i class="fa fa-remove"></i>
<span class="hidden"><?php echo JText::_('K2_DELETE'); ?></span>
</a>
</td>
</tr>
<tr>
<td class="k2ui-center k2ui-hide-on-mobile">
<span class="sortable-handler"><i class="icon-menu"></i></span>
<input type="hidden" name="attachmentordering[]" value="<?php echo $attachment->id; ?>" />
</td>
<td class="attachment_entry"><?php echo $attachment->filename; ?></td>
<td><?php echo $attachment->title; ?></td>
<td><?php echo $attachment->titleAttribute; ?></td>
<td><?php echo $attachment->hits; ?></td>
<td class="k2Center">
<a class="downloadAttachmentButton" href="<?php echo $attachment->link; ?>" title="<?php echo JText::_('K2_DOWNLOAD'); ?>">
<i class="fa fa-download"></i>
<span class="hidden"><?php echo JText::_('K2_DOWNLOAD'); ?></span>
</a>
<a class="deleteAttachmentButton" title="<?php echo JText::_('K2_DELETE'); ?>" href="<?php echo JURI::base(true); ?>/index.php?option=com_k2&amp;view=item&amp;task=deleteAttachment&amp;id=<?php echo $attachment->id?>&amp;cid=<?php echo $this->row->id; ?>">
<i class="fa fa-remove"></i>
<span class="hidden"><?php echo JText::_('K2_DELETE'); ?></span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
Expand Down
5 changes: 5 additions & 0 deletions administrator/components/com_k2/views/item/view.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion components/com_k2/models/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down