Commit 9acdb2aa authored by Tino Goratsch's avatar Tino Goratsch

added the first parts of making it possible to use a customized order of the downloads

parent 0d54cb4b
......@@ -63,6 +63,8 @@ class Index extends Core\Controller\AbstractAdminAction
*/
protected function addDataGridColumns(Core\Helpers\DataGrid $dataGrid)
{
$settings = $this->config->getSettings(Files\Installer\Schema::MODULE_NAME);
$dataGrid
->addColumn([
'label' => $this->translator->t('files', 'active'),
......@@ -77,7 +79,7 @@ class Index extends Core\Controller\AbstractAdminAction
'label' => $this->translator->t('system', 'publication_period'),
'type' => Core\Helpers\DataGrid\ColumnRenderer\DateColumnRenderer::class,
'fields' => ['start', 'end'],
'default_sort' => true,
'default_sort' => $settings['order_by'] === 'date',
'default_sort_direction' => 'desc'
], 50)
->addColumn([
......@@ -104,5 +106,19 @@ class Index extends Core\Controller\AbstractAdminAction
'fields' => ['id'],
'primary' => true
], 10);
if ($this->acl->hasPermission('admin/files/index/sort') && $settings['order_by'] === 'custom') {
$dataGrid
->addColumn([
'label' => $this->translator->t('system', 'order'),
'type' => Core\Helpers\DataGrid\ColumnRenderer\SortColumnRenderer::class,
'fields' => ['sort'],
'default_sort' => $settings['order_by'] === 'custom',
'custom' => [
'route_sort_down' => 'acp/files/index/sort/id_%d/action_down',
'route_sort_up' => 'acp/files/index/sort/id_%d/action_up',
]
], 15);
}
}
}
......@@ -84,7 +84,13 @@ class Settings extends Core\Controller\AbstractAdminAction
);
}
$orderBy = [
'date' => $this->translator->t('files', 'order_by_date_descending'),
'custom' => $this->translator->t('files', 'order_by_custom')
];
return [
'order_by' => $this->formsHelper->choicesGenerator('order_by', $orderBy, $settings['order_by']),
'dateformat' => $this->get('core.helpers.date')->dateFormatDropdown($settings['dateformat']),
'sidebar_entries' => $this->formsHelper->recordsPerPage((int)$settings['sidebar'], 1, 10, 'sidebar'),
'form_token' => $this->formTokenHelper->renderFormToken()
......@@ -103,7 +109,8 @@ class Settings extends Core\Controller\AbstractAdminAction
$data = [
'dateformat' => $this->get('core.helpers.secure')->strEncode($formData['dateformat']),
'sidebar' => (int)$formData['sidebar']
'sidebar' => (int)$formData['sidebar'],
'order_by' => $formData['order_by']
];
if ($this->commentsHelpers) {
......
<?php
/**
* Copyright (c) by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Files\Controller\Admin\Index;
use ACP3\Core;
use ACP3\Core\Controller\AbstractAdminAction;
use ACP3\Core\Controller\Context\FrontendContext;
use ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository;
class Sort extends AbstractAdminAction
{
/**
* @var FilesRepository
*/
private $filesRepository;
/**
* @var \ACP3\Core\Helpers\Sort
*/
private $sortHelper;
/**
* Sort constructor.
* @param FrontendContext $context
* @param FilesRepository $filesRepository
* @param \ACP3\Core\Helpers\Sort $sortHelper
*/
public function __construct(FrontendContext $context, FilesRepository $filesRepository, Core\Helpers\Sort $sortHelper)
{
parent::__construct($context);
$this->filesRepository = $filesRepository;
$this->sortHelper = $sortHelper;
}
/**
* @param int $id
* @param string $action
* @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
* @throws Core\Controller\Exception\ResultNotExistsException
*/
public function execute($id, $action)
{
if (($action === 'up' || $action === 'down') && $this->filesRepository->resultExists($id) === true) {
if ($action === 'up') {
$this->sortHelper->up(FilesRepository::TABLE_NAME, 'id', 'sort', $id);
} else {
$this->sortHelper->down(FilesRepository::TABLE_NAME, 'id', 'sort', $id);
}
Core\Cache\Purge::doPurge($this->appPath->getCacheDir() . 'http');
return $this->redirect()->temporary('acp/files');
}
throw new Core\Controller\Exception\ResultNotExistsException();
}
}
......@@ -12,8 +12,6 @@ class Migration extends Modules\Installer\AbstractMigration
{
/**
* @inheritdoc
*
* @return array
*/
public function schemaUpdates()
{
......@@ -70,14 +68,31 @@ class Migration extends Modules\Installer\AbstractMigration
"ALTER TABLE `{pre}files` ADD COLUMN `active` TINYINT(1) UNSIGNED NOT NULL AFTER `id`;",
"ALTER TABLE `{pre}files` ADD INDEX (`active`)",
"UPDATE `{pre}files` SET `active` = 1;"
],
45 => [
"ALTER TABLE `{pre}files` ADD COLUMN `sort` INT(10) UNSIGNED NOT NULL AFTER `text`;",
"ALTER TABLE `{pre}files` ADD INDEX (`sort`)",
function() {
$repository = $this->schemaHelper->getContainer()->get('files.model.filesrepository');
$files = $repository->getAll();
$i = 1;
foreach ($files as $file) {
$repository->update(['sort' => $i], $file['id']);
++$i;
}
}
],
46 => [
"INSERT INTO `{pre}settings` (`id`, `module_id`, `name`, `value`) VALUES ('', '{moduleId}', 'order_by', 'date');",
"INSERT INTO `{pre}acl_resources` (`id`, `module_id`, `area`, `controller`, `page`, `params`, `privilege_id`) VALUES('', '{moduleId}', 'admin', 'index', 'sort', '', 4);",
]
];
}
/**
* @inheritdoc
*
* @return array
*/
public function renameModule()
{
......
......@@ -26,7 +26,8 @@ class Schema implements Modules\Installer\SchemaInterface
'duplicate' => PrivilegeEnum::ADMIN_CREATE,
'edit' => PrivilegeEnum::ADMIN_EDIT,
'index' => PrivilegeEnum::ADMIN_VIEW,
'settings' => PrivilegeEnum::ADMIN_SETTINGS
'settings' => PrivilegeEnum::ADMIN_SETTINGS,
'sort' => PrivilegeEnum::ADMIN_CREATE,
]
],
'frontend' => [
......@@ -58,7 +59,7 @@ class Schema implements Modules\Installer\SchemaInterface
*/
public function getSchemaVersion()
{
return 44;
return 46;
}
/**
......@@ -78,12 +79,14 @@ class Schema implements Modules\Installer\SchemaInterface
`size` VARCHAR(20) NOT NULL,
`title` VARCHAR(255) NOT NULL,
`text` TEXT NOT NULL,
`sort` INT(10) UNSIGNED NOT NULL,
`comments` TINYINT(1) UNSIGNED NOT NULL,
`user_id` INT UNSIGNED,
PRIMARY KEY (`id`),
FULLTEXT KEY `fulltext_index` (`title`, `file`, `text`),
INDEX `foreign_category_id` (`category_id`),
INDEX (`user_id`),
INDEX (`sort`),
FOREIGN KEY (`category_id`) REFERENCES `{pre}categories` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `{pre}users` (`id`) ON DELETE SET NULL
) {ENGINE} {CHARSET};"
......@@ -109,6 +112,7 @@ class Schema implements Modules\Installer\SchemaInterface
'comments' => 1,
'dateformat' => 'long',
'sidebar' => 5,
'order_by' => 'date'
];
}
}
......@@ -10,6 +10,7 @@ use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Core\Model\DuplicationAwareTrait;
use ACP3\Modules\ACP3\Files\Installer\Schema;
use ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository;
class FilesModel extends AbstractModel
{
......@@ -17,6 +18,11 @@ class FilesModel extends AbstractModel
const EVENT_PREFIX = Schema::MODULE_NAME;
/**
* @var FilesRepository
*/
protected $repository;
/**
* @inheritdoc
*/
......@@ -31,6 +37,10 @@ class FilesModel extends AbstractModel
$data['size'] = $data['filesize'];
}
if ($entryId === null) {
$data['sort'] = $this->repository->getMaxSort() + 1;
}
return parent::save($data, $entryId);
}
......@@ -50,7 +60,8 @@ class FilesModel extends AbstractModel
'comments' => DataProcessor\ColumnTypes::COLUMN_TYPE_INT,
'user_id' => DataProcessor\ColumnTypes::COLUMN_TYPE_INT,
'file' => DataProcessor\ColumnTypes::COLUMN_TYPE_RAW,
'size' => DataProcessor\ColumnTypes::COLUMN_TYPE_RAW
'size' => DataProcessor\ColumnTypes::COLUMN_TYPE_RAW,
'sort' => DataProcessor\ColumnTypes::COLUMN_TYPE_INT
];
}
......
......@@ -25,7 +25,9 @@ class DataGridRepository extends \ACP3\Core\Model\Repository\DataGridRepository
{
return [
'main.*',
'c.title AS cat'
'c.title AS cat',
"(SELECT MIN(`sort`) FROM {$this->getTableName()}) AS `first`",
"(SELECT MAX(`sort`) FROM {$this->getTableName()}) AS `last`"
];
}
......
......@@ -107,4 +107,12 @@ class FilesRepository extends Core\Model\Repository\AbstractRepository
['time' => $time, 'active' => 1]
);
}
/**
* @return int
*/
public function getMaxSort()
{
return (int)$this->db->fetchColumn("SELECT MAX(`sort`) FROM {$this->getTableName()};");
}
}
......@@ -3,6 +3,7 @@
{block CONTENT_AJAX_FORM}
{include file="asset:System/Partials/form_group.select.tpl" options=$dateformat required=true label={lang t="system|date_format"}}
{include file="asset:System/Partials/form_group.select.tpl" options=$sidebar_entries required=true label={lang t="system|sidebar_entries_to_display"}}
{include file="asset:System/Partials/form_group.select.tpl" options=$order_by required=true label={lang t="files|order_by"}}
{if isset($comments)}
{include file="asset:System/Partials/form_group.button_group.tpl" options=$comments required=true label={lang t="system|allow_comments"}}
{/if}
......
......@@ -49,6 +49,13 @@ services:
calls:
- ['setCommentsHelpers', ['@?comments.helpers']]
files.controller.admin.index.sort:
class: ACP3\Modules\ACP3\Files\Controller\Admin\Index\Sort
arguments:
- '@core.context.frontend'
- '@files.model.filesrepository'
- '@core.helpers.sort'
files.controller.frontend.index.details:
class: ACP3\Modules\ACP3\Files\Controller\Frontend\Index\Details
arguments:
......
......@@ -24,6 +24,10 @@
<item key="select_allow_comments">Bitte wählen Sie aus, ob Kommentare unter Downloads zugelassen werden sollen oder nicht.</item>
<item key="select_category">Bitte wählen Sie die Kategorie aus, in der sich der Download befinden soll.</item>
<item key="select_internal_resource">Bitte geben Sie den Pfad zur hochzuladenden Datei an.</item>
<item key="select_order_by">Bitte wählen Sie aus, wie die Downloads sortiert sein sollen.</item>
<item key="order_by">Reihenfolge der Datensätze</item>
<item key="order_by_date_descending">Nach Datum (absteigend)</item>
<item key="order_by_custom">Benutzerdefinierte Reihenfolge</item>
<item key="title">Titel</item>
<item key="type_in_external_resource">Bitte geben Sie den Pfad zur Datei und die Dateigröße ein.</item>
<item key="type_in_title">Der Titel darf nicht leer sein.</item>
......
......@@ -24,6 +24,10 @@
<item key="select_allow_comments">Please select whether you want to allow comments under downloads or not.</item>
<item key="select_category">Please select a category where do you want to find the download.</item>
<item key="select_internal_resource">Please select the path to the file you want to upload.</item>
<item key="select_order_by">Please select in which order you want to display the downloads.</item>
<item key="order_by">Order of results</item>
<item key="order_by_date_descending">By date (descending)</item>
<item key="order_by_custom">Custom order</item>
<item key="title">Title</item>
<item key="type_in_external_resource">Please type in the external resource for the file.</item>
<item key="type_in_title">The title should not be empty.</item>
......
......@@ -55,6 +55,16 @@ class AdminSettingsFormValidation extends AbstractFormValidation
'data' => $formData,
'field' => 'sidebar',
'message' => $this->translator->t('system', 'select_sidebar_entries')
])
->addConstraint(
Core\Validation\ValidationRules\InArrayValidationRule::class,
[
'data' => $formData,
'field' => 'order_by',
'message' => $this->translator->t('files', 'select_order_by'),
'extra' => [
'haystack' => ['date', 'custom']
]
]);
if ($this->modules->isActive('comments')) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment