diff --git a/config/settings/acl.global.php b/config/settings/acl.global.php index 3d50756b7b213c5f00c86f650c6fdd200f7bb803..b56769713dc9b1e3c1e6676535724cae23e9e3fe 100644 --- a/config/settings/acl.global.php +++ b/config/settings/acl.global.php @@ -24,7 +24,7 @@ return [ ['/', ['guest', 'user'], ['get']], ['/about', ['guest', 'user'], ['get']], ['/school', ['school'], ['get']], - ['/school/labs', ['school'], ['get', 'post']], + ['/school/labs', ['school'], ['get', 'post', 'delete']], ['/school/staff', ['school'], ['get', 'post', 'delete']], ['/school/assets', ['school'], ['get', 'post', 'delete']], ['/school/labs/attachment', ['school'], ['get', 'delete']], diff --git a/module/schools/bootstrap.php b/module/schools/bootstrap.php index 3428a2024be3392b61bd73b4848e387b2de3dcb3..1f0f8de8c57e947aae6374609f374dd5c4479620 100644 --- a/module/schools/bootstrap.php +++ b/module/schools/bootstrap.php @@ -66,6 +66,12 @@ return function (Slim\App $app) { ); }; + $container[Action\Lab\DeleteLab::class] = function ($c) { + return new Action\Lab\DeleteLab( + $c->get(Service\LabServiceInterface::class) + ); + }; + $container[Action\Lab\DownloadAttachment::class] = function ($c) { $settings = $c->get('settings'); $uploadTargetPath = $settings['schools']['file_upload']['target_path']; @@ -210,6 +216,7 @@ return function (Slim\App $app) { $this->get('/labs', Action\Lab\ListAll::class)->setName('school.labs'); $this->post('/labs', Action\Lab\PersistLab::class) ->add(Middleware\InputFilterLab::class); + $this->delete('/labs', Action\Lab\DeleteLab::class); $this->get('/labs/attachment', Action\Lab\DownloadAttachment::class) ->setName('school.labs.attachment'); $this->delete('/labs/attachment', Action\Lab\RemoveAttachment::class); diff --git a/module/schools/public/js/schools/labs.js b/module/schools/public/js/schools/labs.js index bce67d9afcd384a7b350f57f9f2818461933c210..bd74dcddcbe4963621fe6916ad325068af186b2a 100644 --- a/module/schools/public/js/schools/labs.js +++ b/module/schools/public/js/schools/labs.js @@ -202,7 +202,7 @@ id: that.lab.get('id') } }).done(function () { - that.model.remove(that.asset.get('id')); + that.model.remove(that.lab.get('id')); that.hide(); }).fail(function (xhr, err){ alert('Δεν ήταν δυνατή η διαγραφή του χώρου'); diff --git a/module/schools/src/Action/Lab/DeleteLab.php b/module/schools/src/Action/Lab/DeleteLab.php new file mode 100644 index 0000000000000000000000000000000000000000..45f1e6b3832675744df1ccf4e589c8d59096b14e --- /dev/null +++ b/module/schools/src/Action/Lab/DeleteLab.php @@ -0,0 +1,54 @@ +labService = $labService; + } + + public function __invoke(Request $req, Response $res) + { + $school = $req->getAttribute('school', false); + if (!$school && !$school->id) { + return $res->withStatus(403, 'No school'); + } + + $id = $req->getParam('id', false); + + if (!$id) { + return $res->withStatus(404); + } + + try { + $this->labService->removeLab($id, $school->id); + $res = $res->withStatus(204); + } catch (Exception $ex) { + $res = $res->withStatus(500, $ex->getMessage()); + } + + return $res; + } +} diff --git a/module/schools/src/Service/LabService.php b/module/schools/src/Service/LabService.php index c41c8a094b9e2e28b52ebee7a9e0a8bbed8ba938..56d6ac32250259084a20543bc5fc391eb9cf708b 100644 --- a/module/schools/src/Service/LabService.php +++ b/module/schools/src/Service/LabService.php @@ -197,4 +197,21 @@ class LabService implements LabServiceInterface $lab->attachment_mime = null; R::store($lab); } + + public function removeLab($id, $school_id = null) + { + $sql = ' id = ? '; + $bindings = [(int) $id]; + if (null !== $school_id) { + $sql .= ' AND school_id = ? '; + $bindings[] = (int) $school_id; + } + $lab = R::findOne('lab', $sql, $bindings); + if (null !== $lab) { + if ($lab->attachment && is_writable($this->filesPath . '/' . $lab->attachment)) { + unlink($this->filesPath . '/' . $lab->attachment); + } + R::trash($lab); + } + } } diff --git a/module/schools/src/Service/LabServiceInterface.php b/module/schools/src/Service/LabServiceInterface.php index ea27126081719ffe00a73912e287eecaaf57cf61..7afd354466a0a6a51dc7dde01e8b052be534e306 100644 --- a/module/schools/src/Service/LabServiceInterface.php +++ b/module/schools/src/Service/LabServiceInterface.php @@ -16,6 +16,7 @@ interface LabServiceInterface public function getLabById($id); public function getLabsBySchoolId($id); public function getLabForSchool($school_id, $id); + public function removeLab($id, $school_id = null); public function removeLabAttachment($lab_id); diff --git a/module/schools/templates/schools/labs.twig b/module/schools/templates/schools/labs.twig index bb54daea0a9cefff2afa8c1edffe59025ef7e959..6c148981020a9c0c60255814c747a4cdc88e194b 100644 --- a/module/schools/templates/schools/labs.twig +++ b/module/schools/templates/schools/labs.twig @@ -90,6 +90,7 @@