ManageAssets: allow batch deletion of assets

This commit is contained in:
Aaron van Geffen 2023-03-11 21:24:55 +01:00
parent 5b8551a726
commit a4cc528951
3 changed files with 100 additions and 5 deletions

View File

@ -14,10 +14,37 @@ class ManageAssets extends HTMLController
if (!Registry::get('user')->isAdmin())
throw new NotAllowedException();
if (isset($_POST['deleteChecked'], $_POST['delete']) && Session::validateSession())
$this->handleAssetDeletion();
Session::resetSessionToken();
$options = [
'form' => [
'action' => BASEURL . '/manageassets/?' . Session::getSessionTokenKey() . '=' . Session::getSessionToken(),
'method' => 'post',
'class' => 'col-md-6 text-end',
'is_embed' => true,
'buttons' => [
'deleteChecked' => [
'type' => 'submit',
'caption' => 'Delete checked',
'class' => 'btn-danger',
'onclick' => 'return confirm(\'Are you sure you want to delete these items?\')',
],
],
],
'columns' => [
'checkbox' => [
'header' => '<input type="checkbox" id="selectall">',
'is_sortable' => false,
'parse' => [
'type' => 'function',
'data' => function($row) {
return '<input type="checkbox" class="asset_select" name="delete[]" value="' . $row['id_asset'] . '">';
},
],
],
'id_asset' => [
'value' => 'id_asset',
'header' => 'ID',
@ -71,6 +98,7 @@ class ManageAssets extends HTMLController
'title' => 'Manage assets',
'no_items_label' => 'No assets meet the requirements of the current filter.',
'items_per_page' => 30,
'index_class' => 'col-md-6',
'base_url' => BASEURL . '/manageassets/',
'get_data' => function($offset = 0, $limit = 30, $order = '', $direction = 'down') {
if (!in_array($order, ['id_asset', 'id_user_uploaded', 'title', 'subdir', 'filename']))
@ -100,7 +128,25 @@ class ManageAssets extends HTMLController
];
$table = new GenericTable($options);
parent::__construct('Asset management - Page ' . $table->getCurrentPage() .'');
$this->page->adopt(new TabularData($table));
parent::__construct('Asset management - Page ' . $table->getCurrentPage());
$wrapper = new AssetManagementWrapper();
$this->page->adopt($wrapper);
$wrapper->adopt(new TabularData($table));
}
private function handleAssetDeletion()
{
if (!isset($_POST['delete']) || !is_array($_POST['delete']))
throw new UnexpectedValueException();
foreach ($_POST['delete'] as $id_asset)
{
$asset = Asset::fromId($id_asset);
$asset->delete();
}
header('Location: ' . BASEURL . '/manageassets/');
exit;
}
}

View File

@ -0,0 +1,36 @@
<?php
/*****************************************************************************
* AssetManagementWrapper.php
* Defines asset management wrapper template.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class AssetManagementWrapper extends Template
{
public function html_main()
{
echo '
<form action="" method="post">';
foreach ($this->_subtemplates as $template)
$template->html_main();
echo '
</form>
<script type="text/javascript" defer="defer">
const allAreSelected = () => {
return document.querySelectorAll(".asset_select").length ===
document.querySelectorAll(".asset_select:checked").length;
};
const selectAll = document.getElementById("selectall");
selectAll.addEventListener("change", event => {
const newSelectedState = !allAreSelected();
document.querySelectorAll(".asset_select").forEach(el => {
el.checked = newSelectedState;
});
});
</script>';
}
}

View File

@ -145,8 +145,12 @@ class TabularData extends SubTemplate
protected function showForm($form)
{
echo '
if (!isset($form['is_embed']))
echo '
<form action="', $form['action'], '" method="', $form['method'], '" class="', $form['class'], '">';
else
echo '
<div class="', $form['class'], '">';
if (!empty($form['is_group']))
echo '
@ -206,7 +210,12 @@ class TabularData extends SubTemplate
foreach ($form['buttons'] as $name => $button)
{
echo '
<button class="btn ', isset($button['class']) ? $button['class'] : 'btn-primary', '" type="', $button['type'], '" name="', $name, '">', $button['caption'], '</button>';
<button class="btn ', isset($button['class']) ? $button['class'] : 'btn-primary', '" type="', $button['type'], '" name="', $name, '"';
if (isset($button['onclick']))
echo ' onclick="', $button['onclick'], '"';
echo '>', $button['caption'], '</button>';
if (isset($button['html_after']))
echo $button['html_after'];
@ -216,7 +225,11 @@ class TabularData extends SubTemplate
echo '
</div>';
echo '
if (!isset($form['is_embed']))
echo '
</form>';
else
echo '
</div>';
}
}