diff --git a/controllers/ManageAssets.php b/controllers/ManageAssets.php index 4598c8e..499b3c1 100644 --- a/controllers/ManageAssets.php +++ b/controllers/ManageAssets.php @@ -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' => '', + 'is_sortable' => false, + 'parse' => [ + 'type' => 'function', + 'data' => function($row) { + return ''; + }, + ], + ], '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; } } diff --git a/templates/AssetManagementWrapper.php b/templates/AssetManagementWrapper.php new file mode 100644 index 0000000..545f7c4 --- /dev/null +++ b/templates/AssetManagementWrapper.php @@ -0,0 +1,36 @@ +'; + + foreach ($this->_subtemplates as $template) + $template->html_main(); + + echo ' + + '; + } +} diff --git a/templates/TabularData.php b/templates/TabularData.php index 86f17de..fdcfed3 100644 --- a/templates/TabularData.php +++ b/templates/TabularData.php @@ -145,8 +145,12 @@ class TabularData extends SubTemplate protected function showForm($form) { - echo ' + if (!isset($form['is_embed'])) + echo '
'; + else + echo ' + '; } }