remove lab action

parent c633655a
......@@ -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']],
......
......@@ -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);
......
......@@ -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('Δεν ήταν δυνατή η διαγραφή του χώρου');
......
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\Action\Lab;
use Exception;
use GrEduLabs\Schools\Service\LabServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class DeleteLab
{
/**
*
* @var LabServiceInterface
*/
protected $labService;
public function __construct(LabServiceInterface $labService)
{
$this->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;
}
}
......@@ -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);
}
}
}
......@@ -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);
......
......@@ -90,6 +90,7 @@
<input type="hidden" name="id" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger pull-left remove">Διαγραφή</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Κλείσιμο</button>
<button name="submit" value="submit" type="submit" class="btn btn-primary">Αποθήκευση</button>
</div>
......
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