labs; need to fix ajax file upload

parent 0e973a68
......@@ -18,4 +18,9 @@ return [
],
],
],
'schools' => [
'file_upload' => [
'target_path' => 'data/uploads',
],
],
];
......@@ -28,7 +28,8 @@ return function (Slim\App $app) {
$container['settings']['db']['user'],
$container['settings']['db']['pass']
);
RedBeanPHP\R::freeze();
// RedBeanPHP\R::freeze();
// RedBeanPHP\R::debug( true );
$container['view'] = function ($c) {
$settings = $c['settings'];
......
......@@ -20,6 +20,7 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
{% block htmlHead %}{% endblock %}
</head>
<body class="{% block bodyClass %}{% endblock %}">
{% include 'navigation/main.twig' %}
......
......@@ -110,10 +110,7 @@ return function (Slim\App $app) {
};
$container[Service\LabServiceInterface::class] = function ($c) {
return new Service\LabService(
$c->get(Service\SchoolServiceInterface::class),
$c->get(Service\StaffServiceInterface::class)
);
return new Service\LabService();
};
$container[Service\AssetServiceInterface::class] = function ($c) {
......@@ -168,7 +165,12 @@ return function (Slim\App $app) {
};
$container[InputFilter\Lab::class] = function ($c) {
return new InputFilter\Lab();
$settings = $c->get('settings');
$fileUploadSettings = $settings['schools']['file_upload'];
return new InputFilter\Lab(
$fileUploadSettings,
$c->get(Service\LabServiceInterface::class)
);
};
});
......
......@@ -270,7 +270,7 @@ CREATE TABLE `lab` (
`use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`attachment` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_network` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_server` tinyint(1) unsigned DEFAULT NULL,
`has_server` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`school_id` int(11) unsigned DEFAULT NULL,
`teacher_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
......
This diff is collapsed.
......@@ -12,23 +12,38 @@ namespace GrEduLabs\Schools\Action\Lab;
use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\StaffServiceInterface;
use RedBeanPHP\R;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
class ListAll
{
/**
*
* @var Twig
*/
protected $view;
/**
*
* @var LabServiceInterface
*/
protected $labService;
/**
*
* @var StaffServiceInterface
*/
protected $staffService;
public function __construct(
Twig $view,
LabServiceInterface $labservice,
StaffServiceInterface $staffservice
) {
$this->view = $view;
$this->labservice = $labservice;
$this->staffservice = $staffservice;
$this->labService = $labservice;
$this->staffService = $staffservice;
}
public function __invoke(Request $req, Response $res, array $args = [])
......@@ -38,44 +53,25 @@ class ListAll
return $res->withStatus(403, 'No school');
}
$labs = $this->labservice->getLabsBySchoolId($school->id);
$staff = $this->staffservice->getTeachersBySchoolId($school->id);
$clean_staff = [];
foreach ($staff as $obj) {
$clean_staff[] = [
'value' => $obj['id'],
'label' => $obj['name'] . " " . $obj['surname'],
];
}
$lessons = $this->labservice->getLessons();
$lessons_formatted = [];
foreach ($lessons as $lesson) {
$lessons_formatted[] = ['value' => $lesson->id, 'label' => $lesson->name];
}
$labs_formatted = [];
foreach($labs as $lab) {
$lab['responsible'] = $lab['teacher_id'];
$labs_formatted[] = $lab;
}
$labs = $this->labService->getLabsBySchoolId($school->id);
return $this->view->render($res, 'schools/labs.twig', [
'labs' => $labs_formatted,
'staff' => $clean_staff,
'lab_types' => [
[
'value' => 1,
'label' => 'ΕΡΓΑΣΤΗΡΙΟ',
],
[
'value' => 2,
'label' => 'ΑΙΘΟΥΣΑ',
],
[
'value' => 3,
'label' => 'ΓΡΑΦΕΙΟ',
],
],
'lessons' => $lessons_formatted,
'labs' => $labs,
'staff' => array_map(function ($teacher) {
return ['value' => $teacher['id'], 'label' => $teacher['fullname']];
}, $this->staffService->getTeachersBySchoolId($school->id)),
'network_options' => array_map(function ($option) {
return ['value' => $option, 'label' => $option];
}, $this->labService->getHasNetworkValues()),
'server_options' => array_map(function ($option) {
return ['value' => $option, 'label' => $option];
}, $this->labService->getHasServerValues()),
'lab_types' => array_map(function ($type) {
return ['value' => $type['id'], 'label' => $type['name']];
}, $this->labService->getLabTypes()),
'lessons_options' => array_map(function ($lesson) {
return ['value' => $lesson['id'], 'label' => $lesson['name']];
}, $this->labService->getLessons()),
]);
}
}
......@@ -33,26 +33,21 @@ class PersistLab
$params = $req->getParams();
$id = $params['id'];
$params['school_id'] = $school->id;
$params['lessons'] = [1,2];
unset($params['id']);
try {
if ($id > 0) {
$id = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id);
if ($id) {
$lab = $this->labservice->updateLab($params, $id);
$res = $res->withStatus(200);
} else {
$id = $this->labservice->createLab($params);
if ($id > 0) {
$lab = $this->labservice->getLabById($id);
}
}
if (isset($lab)) {
return $res->withJson($lab->export())->withStatus(201);
$lab = $this->labservice->createLab($params);
$res = $res->withStatus(201);
}
$res = $res->withJson($lab);
} catch (Exception $ex) {
$res = $res->withStatus(500, $ex->getMessage());
}
return $res->withStatus(400);
return $res;
}
}
......@@ -17,7 +17,7 @@ trait InputFilterTrait
{
$this->inputFilter->setData($data);
$isValid = $this->inputFilter->isValid();
return [
'is_valid' => $isValid,
'values' => $isValid ? $this->inputFilter->getValues() : [],
......
......@@ -9,7 +9,9 @@
namespace GrEduLabs\Schools\InputFilter;
use GrEduLabs\Schools\Service\LabServiceInterface;
use Zend\Filter;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
......@@ -17,9 +19,11 @@ use Zend\Validator;
class Lab
{
use InputFilterTrait;
public function __construct()
{
public function __construct(
array $fileUploadSettings,
LabServiceInterface $labService
) {
$id = new Input('id');
$id->setRequired(false)
->getValidatorChain()
......@@ -33,13 +37,13 @@ class Lab
->attach(new Validator\NotEmpty())
->attach(new Validator\StringLength(['min' => 3]));
$type = new Input('type');
$type->setRequired(true);
$type->getValidatorChain()
$labTypeId = new Input('labtype_id');
$labTypeId->setRequired(true);
$labTypeId->getValidatorChain()
->attach(new Validator\NotEmpty());
$responsible = new Input('responsible');
$responsible->setRequired(false)
$responsibleId = new Input('responsible_id');
$responsibleId->setRequired(false)
->getValidatorChain()
->attach(new Validator\Digits());
......@@ -50,32 +54,48 @@ class Lab
->attach(new Validator\Digits());
$lessons = new Input('lessons');
$lessons->setRequired(true)
->getValidatorChain()
$lessons->setRequired(false);
$lessons->getValidatorChain()
->attach(new Validator\NotEmpty());
$attachment = new Input('attachment');
$attachment->setRequired(false);
$attachment = new FileInput('attachment');
$attachment->setRequired(false)
->getFilterChain()
->attach(new Filter\File\RenameUpload([
'target' => $fileUploadSettings['target_path'],
'randomize' => true,
]));
$attachment->getValidatorChain()
->attach(new Validator\File\UploadFile());
$use_ext_program= new Input('use_ext_program');
$use_ext_program->setRequired(false);
$use_in_program = new Input('use_in_program');
$use_in_program->setRequired(false);
$has_server = new Input('has_server');
$has_server->setRequired(false);
$has_network = new Input('has_network');
$has_network->setRequired(false);
$has_network->getValidatorChain()
->attach(new Validator\NotEmpty())->attach(new Validator\InArray([
'haystack' => $labService->getHasNetworkValues(),
]));
$has_server = new Input('has_server');
$has_server->setRequired(false);
$has_server->getValidatorChain()
->attach(new Validator\NotEmpty())
->attach(new Validator\InArray([
'haystack' => $labService->getHasServerValues(),
]));
$this->inputFilter = new InputFilter();
$this->inputFilter
->add($id)
->add($name)
->add($type)
->add($responsible)
->add($labTypeId)
->add($responsibleId)
->add($area)
->add($lessons)
->add($attachment)
......
......@@ -10,12 +10,36 @@
namespace GrEduLabs\Schools\Middleware;
use Slim\Http\Request;
use Slim\Http\Response;
class InputFilterLab
{
use InputFilterTrait;
private $inputFilter;
public function __construct(callable $inputFilter)
{
$this->inputFilter = $inputFilter;
}
public function __invoke(Request $req, Response $res, callable $next)
{
$data = array_merge_recursive($req->getParams(), $req->getUploadedFiles());
$inputFilter = $this->inputFilter;
$result = $inputFilter($data);
var_dump($req->getParams(), $req->getUploadedFiles(), $data);
die();
if (!$result['is_valid']) {
$res = $res->withStatus(422, 'validation error');
$res->withJson($result);
return $res;
}
$req = $req->withParsedBody($result['values']);
return $next($req, $res);
}
}
......@@ -9,20 +9,11 @@
namespace GrEduLabs\Schools\Service;
use InvalidArgumentException;
use RedBeanPHP\R;
class LabService implements LabServiceInterface
{
protected $schoolService;
protected $staffService;
public function __construct(
SchoolServiceInterface $schoolService,
StaffServiceInterface $staffService
) {
$this->schoolService = $schoolService;
$this->staffService = $staffService;
}
public function createLab(array $data)
{
......@@ -30,60 +21,61 @@ class LabService implements LabServiceInterface
$lab = R::dispense('lab');
$this->persist($lab, $data);
//return $this->export($lab);
return $lab;
return $this->exportLab($lab);
}
public function updateLab(array $data, $id)
{
$lab = R::load('lab', $id);
if (!$lab->id) {
throw new \InvalidArgumentException('No lab found');
throw new InvalidArgumentException('No lab found');
}
$this->persist($lab, $data);
return $this->export($lab);
return $this->exportLab($lab);
}
private function persist($lab, $data)
{
$lab->school_id = $data['school_id'];
$lab->name = $data['name'];
$lab->type = $data['type'];
$lab->area = $data['area'];
$lab->sharedLesson[] = $this->getLessonsById($data['lessons']);
$lab->use_ext_program = $data['use_ext_program'];
$lab->use_in_program = $data['use_in_program'];
$lab->attachment = $data['attachment'];
$lab->has_network = $data['has_network'];
$lab->has_server = isset($data['has_server']);
$lab->responsible = $data['responsible'];
$id = R::store($lab);
$lab->school_id = $data['school_id'];
$lab->name = $data['name'];
$lab->labtype_id = $data['labtype_id'];
$lab->area = $data['area'];
$lab->sharedLesson = $this->getLessonsById($data['lessons']);
$lab->use_ext_program = $data['use_ext_program'];
$lab->use_in_program = $data['use_in_program'];
$lab->attachment = $data['attachment'];
$lab->has_network = $data['has_network'];
$lab->has_server = $data['has_server'];
$lab->responsible = R::load('teacher', $data['responsible_id']);
R::store($lab);
}
public function getLabById($id)
{
$lab = R::load('lab', $id);
if (!$lab->id) {
throw new InvalidArgumentException('No lab found');
}
return $lab;
return $this->export($lab);
}
public function getLabsBySchoolId($id)
{
$labs = R::findAll('lab', 'school_id = ?', [$id]);
$elabs=[];
foreach($labs as $lab) {
$elabs[] = $lab->export();
}
return $elabs;
return array_map([$this, 'exportLab'], $labs);
}
public function getLessons()
{
$lessons = R::findAll('lesson');
return $lessons;
return array_map(function ($lesson) {
return $lesson->export();
}, $lessons);
}
public function getLessonsByLabId($id)
......@@ -91,17 +83,57 @@ class LabService implements LabServiceInterface
$lab = R::load('lab', $id);
$lessons = $lab->sharedLesson;
return $lessons;
return array_map(function ($lesson) {
return $lesson->export();
}, $lessons);
}
public function getLabTypes()
{
return array_map(function ($lab) {
return $lab->export();
}, R::find('labtype'));
}
private function getLessonsById(array $ids)
private function getLessonsById($ids)
{
$lessons= [];
foreach ($ids as $id) {
$lesson= R::load('lesson', $id);
$lessons[] = $lesson;
if (!is_array($ids)) {
$ids = [$ids];
}
return $lesson;
return array_values(R::loadAll('lesson', $ids));
}
private function exportLab($bean)
{
$responsible = $bean->fetchAs('teacher')->responsible;
if ($responsible) {
$responsible = sprintf("%s %s", $responsible->name, $responsible->surname);
}
return array_merge($bean->export(), [
'labtype' => $bean->labtype->name,
'responsible' => $responsible,
'lessons' => array_reduce($bean->sharedLesson, function ($ids, $lesson) {
$ids[] = $lesson->id;
return $ids;
}, []),
]);
}
public function getHasNetworkValues()
{
return [
'ΔΟΜΗΜΕΝΗ ΚΑΛΩΔΙΩΣΗ',
'ΑΣΥΡΜΑΤΗ ΔΙΚΤΥΟ ΜΕΣΩ WIFI',
'ΔΕΝ ΥΠΑΡΧΕΙ ΔΙΚΤΥΟ',
];
}
public function getHasServerValues()
{
return ['ΝΑΙ', 'ΟΧΙ'];
}
}
......@@ -18,4 +18,9 @@ interface LabServiceInterface
public function getLessons();
public function getLessonsByLabId($id);
public function getLabTypes();
public function getHasNetworkValues();
public function getHasServerValues();
}
......@@ -86,6 +86,7 @@ class StaffService implements StaffServiceInterface
return array_merge($teacherBean->export(), [
'branch' => $teacherBean->branch->name,
'position' => implode(', ', $position),
'fullname' => sprintf("%s %s", $teacherBean->name, $teacherBean->surname),
]);
}
}
......@@ -35,7 +35,7 @@
<div class="form-group">
<label class="control-label hidden-xs hidden-sm col-md-3" for="el-{{ name }}">{{ label|raw }}</label>
<div class="col-xs-12 col-sm-12 col-md-9">
<div class="input-group">
{#<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Επιλέξτε&hellip; <input type="file" multiple name="{{ name }}" id="el-{{ name }}">
......@@ -50,7 +50,8 @@
<i class="fa fa-remove"></i>
</span>
</span>
</div>
</div>#}
<input type="file" multiple name="{{ name }}" id="el-{{ name }}">
</div>
</div>
{% endmacro %}
......
......@@ -24,6 +24,8 @@
<th>Ονομασία</th>
<th>Τύπος</th>
<th>Υπεύθυνος</th>
<th>Δίκτυο</th>
<th>Server</th>
<th>Επιφάνεια (m<sup>2</sup>)</th>
</tr>
</thead>
......@@ -31,13 +33,15 @@
{% for lab in labs %}
<tr data-lab="{{ lab|json_encode }}" data-id="{{ lab.id }}">
<td>{{ lab.name }}</td>
<td>{{ lab.typeName }}</td>
<td>{{ lab.responsibleName }}</td>
<td>{{ lab.labtype }}</td>
<td>{{ lab.responsible }}</td>
<td>{{ lab.has_network }}</td>
<td>{{ lab.has_server }}</td>
<td>{{ lab.area }}</td>
</tr>
{% else %}
<tr>
<td colspan="4" class="text-center text-muted">
<td colspan="6" class="no-records text-center text-muted">
Δεν έχουν καταχωρηθεί χώροι.
</td>
</tr>
......@@ -45,13 +49,13 @@
</tbody>
<tfoot>
<tr>
<td colspan="4"></td>
<td colspan="6"></td>
</tr>
</tfoot>
</table>
</div>
<div class="modal fade" id="lab-form-modal" tabindex="-1" role="dialog" aria-labelledby="lab-from">
<form class="form-horizontal">
<form class="form-horizontal" enctype="multipart/form-data">
<div class="modal-dialog" role="form">
<div class="modal-content">
<div class="modal-header">
......@@ -62,11 +66,13 @@
</div>
<div class="modal-body">
{{ macros.input('name', 'Όνομα', '', 'text', {'required': ''}) }}
{{ macros.select('responsible', 'Υπεύθυνος', staff, '', {'required': ''}) }}
{{ macros.select('type', 'Τύπος', lab_types, '', {'required': ''}) }}
{{ macros.select('responsible_id', 'Υπεύθυνος', staff, '', {'required': ''}) }}
{{ macros.select('labtype_id', 'Τύπος', lab_types, '', {'required': ''}) }}
{{ macros.select('has_network', 'Δίκτυο', network_options, '', {'required': ''}) }}
{{ macros.select('has_server', 'Ύπαρξη server', server_options, '', {'required': ''}) }}
{{ macros.input('area', 'Επιφάνεια (m<sup>2</sup>)', '', 'number', {'required': '', 'min': 0, 'max': 500