labs; need to fix ajax file upload

parent 0e973a68
...@@ -18,4 +18,9 @@ return [ ...@@ -18,4 +18,9 @@ return [
], ],
], ],
], ],
'schools' => [
'file_upload' => [
'target_path' => 'data/uploads',
],
],
]; ];
...@@ -28,7 +28,8 @@ return function (Slim\App $app) { ...@@ -28,7 +28,8 @@ return function (Slim\App $app) {
$container['settings']['db']['user'], $container['settings']['db']['user'],
$container['settings']['db']['pass'] $container['settings']['db']['pass']
); );
RedBeanPHP\R::freeze(); // RedBeanPHP\R::freeze();
// RedBeanPHP\R::debug( true );
$container['view'] = function ($c) { $container['view'] = function ($c) {
$settings = $c['settings']; $settings = $c['settings'];
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <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> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--> <![endif]-->
{% block htmlHead %}{% endblock %}
</head> </head>
<body class="{% block bodyClass %}{% endblock %}"> <body class="{% block bodyClass %}{% endblock %}">
{% include 'navigation/main.twig' %} {% include 'navigation/main.twig' %}
......
...@@ -110,10 +110,7 @@ return function (Slim\App $app) { ...@@ -110,10 +110,7 @@ return function (Slim\App $app) {
}; };
$container[Service\LabServiceInterface::class] = function ($c) { $container[Service\LabServiceInterface::class] = function ($c) {
return new Service\LabService( return new Service\LabService();
$c->get(Service\SchoolServiceInterface::class),
$c->get(Service\StaffServiceInterface::class)
);
}; };
$container[Service\AssetServiceInterface::class] = function ($c) { $container[Service\AssetServiceInterface::class] = function ($c) {
...@@ -168,7 +165,12 @@ return function (Slim\App $app) { ...@@ -168,7 +165,12 @@ return function (Slim\App $app) {
}; };
$container[InputFilter\Lab::class] = function ($c) { $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` ( ...@@ -270,7 +270,7 @@ CREATE TABLE `lab` (
`use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`attachment` 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_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, `school_id` int(11) unsigned DEFAULT NULL,
`teacher_id` int(11) unsigned DEFAULT NULL, `teacher_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
......
(function () { (function (utils) {
'use strict'; 'use strict';
var Lab, var Lab,
Labs, Labs,
LabView, LabRow,
LabsView, LabsView,
LabModalView, LabView,
labTemplate, labTemplate;
utils = window.EDULABS.utils;
Lab = Backbone.Model.extend({ idAttribute: 'id' }); Lab = Backbone.Model.extend({ idAttribute: 'id' });
...@@ -16,7 +15,7 @@ ...@@ -16,7 +15,7 @@
comparator: 'name' comparator: 'name'
}); });
LabView = Backbone.View.extend({ LabRow = Backbone.View.extend({
tagName: 'tr', tagName: 'tr',
template: (function () { template: (function () {
if (typeof labTemplate === 'undefined') { if (typeof labTemplate === 'undefined') {
...@@ -26,67 +25,134 @@ ...@@ -26,67 +25,134 @@
}()), }()),
initialize: function () { initialize: function () {
this.model.on('change', this.render, this); this.model.on('change', this.render, this);
this.model.on('remove', this.remove, this);
}, },
render: function () { render: function () {
var html = this.template({ lab: this.model.attributes }); this.$el.html(this.template({ lab: this.model.attributes }));
this.$el.html(html);
this.$el.attr('data-id', this.model.get('id')); this.$el.attr('data-id', this.model.get('id'));
return this; return this;
},
remove: function () {
this.$el.remove();
} }
}); });
LabsView = Backbone.View.extend({ LabsView = Backbone.View.extend({
el: '#school', el: '#school',
modal: null, labView: null,
events: { events: {
'click .btn-add-lab': 'addLab', 'click .btn-add-lab': 'addLab',
'click tbody tr': 'editLab' 'click tbody tr': 'editLab'
}, },
initialize: function () { initialize: function () {
var that = this; var that = this;
this.modal = new LabModalView(); this.labView = new LabView({model: this.model});
_.each(this.$el.find('tbody tr[data-lab]'), function (tr) { _.each(this.$el.find('tbody tr'), function (tr) {
var data, var data = $(tr).data('lab'),
lab; lab = new Lab(data);
tr = $(tr);
data = tr.data('lab');
lab = new Lab(data);
that.model.add(lab); that.model.add(lab);
new LabView({ model: lab }); new LabRow({ model: lab, el: tr });
tr.attr('data-lab', null); $(tr).attr('data-lab', null);
}); });
this.model.on('add', this.renderLab, this); this.model.on('add', this.renderLab, this);
}, this.model.on('remove', function () {
renderLab: function (lab) { if (this.model.length === 0) {
this.$el.find('tbody').append(new LabView({ this.$el.find('tbody tr.no-records').show();
model: lab }
}).render().el); }, this);
return this;
}, },
addLab: function (evt) { addLab: function (evt) {
var that = this;
evt.preventDefault(); evt.preventDefault();
this.modal.render(function (data) { this.labView.render();
that.model.add(data); return this;
});
}, },
editLab: function (evt) { editLab: function (evt) {
var lab = this.model.get(utils.parseInt($(evt.target).closest('tr').data('id'))); var labId;
if (!lab) return; labId = $(evt.target).closest('tr').data('id');
this.modal.render(function (data) { this.labView.render(labId);
lab.set(data); return this;
}, lab.attributes); },
renderLab: function (lab) {
this.$el.find('tbody tr.no-records').hide();
this.$el.find('tbody').append(new LabRow({
model: lab
}).render().el);
return this;
} }
}); });
LabModalView = Backbone.View.extend({ LabView = Backbone.View.extend({
el: '#lab-form-modal', el: '#lab-form-modal',
form: null, form: null,
lab: null,
url: null,
events: { events: {
'submit': 'submit', // 'submit': 'persistLab',
'click button.remove': 'removeLab'
}, },
initialize: function () { initialize: function () {
var that = this;
this.form = this.$el.find('form'); this.form = this.$el.find('form');
this.url = this.form.data('url');
this.$el.on('hide.bs.modal', function () {
utils.formMessages.clear(that.form);
that.form[0].reset();
that.form.find('input[type="hidden"]').val('');
});
this.form.find('input[type="file"]').fileupload({
url: that.form.data('url'),
multipart: true,
submit: function (e, data) {
data.formData = utils.serializeObject(that.form);
data.jqXHR = $(this).fileupload('send', data);
return false;
},
done: function (response) {
if (that.lab) {
that.lab.set(response);
} else {
that.model.add(response);
}
that.hide();
},
fail: function (xhr, err) {
var messages;
if (422 === xhr.status) {
messages = JSON.parse(xhr.responseText).messages || {};
utils.formMessages.render(that.form, messages);
} else {
alert('Προέκυψε κάποιο σφάλμα');
}
}
});
},
render: function (labId) {
var labAttributes;
if (!labId) {
this.$el.find('.modal-footer button.remove')
.prop('disabled', true)
.hide();
} else {
this.$el.find('.modal-fotter button.remove')
.prop('disabled', false)
.show();
}
this.lab = labId && this.model.get(labId) || null;
labAttributes = this.lab && this.lab.attributes || {};
_.each(this.form[0].elements, function (element) {
var name;
element = $(element);
name = element.attr('name');
if ('checkbox' === element.attr('type')) {
element.prop('checked', utils.parseInt(labAttributes[name]));
} else {
element.val(labAttributes[name] || '');
}
});
this.show();
return this;
}, },
show: function () { show: function () {
this.$el.modal('show'); this.$el.modal('show');
...@@ -96,40 +162,73 @@ ...@@ -96,40 +162,73 @@
this.$el.modal('hide'); this.$el.modal('hide');
return this; return this;
}, },
render: function (done, lab) { // persistLab: function (evt) {
var template, // var formData = utils.serializeObject(this.form),
that = this; // that = this,
// upload = this.form.find('input[type="file"]');
lab = lab || {}; // evt.preventDefault();
this.form[0].reset(); // upload
this.form.find('input[type="hidden"]').val(''); // $('#fileupload').fileupload('send', {
this.form.data('done', done); // files: upload[0].files
// });
_.each(this.form[0].elements, function (element) { ////
var element = $(element), //// ({
name = element.attr('name'); //// url: that.url,
element.val(lab[name] || ''); //// submit: function (e, data) {
}); //// var $this = $(this);
this.show(); //// data.formData = formData;
//// data.jqXHR = $this.fileupload('send', data);
return this; //// return false;
}, //// },
submit: function (evt) { //// done: function (response) {
var data; //// if (that.lab) {
evt.preventDefault(); //// that.lab.set(response);
data = _.reduce(this.form.serializeArray(), function (hash, pair) { //// } else {
hash[pair.name] = pair.value; //// that.model.add(response);
return hash; //// }
}, {}); //// that.hide();
evt.preventDefault(); //// },
if (!data.id) { //// error: function (xhr, err) {
data.id = (100 * Math.random()).toFixed(0); //// var messages;
//// if (422 === xhr.status) {
//// messages = JSON.parse(xhr.responseText).messages || {};
//// utils.formMessages.render(that.form, messages);
//// } else {
//// alert('Προέκυψε κάποιο σφάλμα');
//// }
//// }
//// });
//// upload.fileupload('send');
// upload.fileupload('destroy');
////
//// $.ajax({
//// url: that.url,
//// type: 'post',
//// contentType: 'multipart/form-data',
//// data: data,
//// }).).fail(function );
// },
removeLab: function(evt) {
var that = this;
if (!confirm('Να διαγραφεί ο χώρος;')) {
return;
}
$.ajax({
url: that.url,
type: 'delete',
dataType: 'json',
data: {
id: that.lab.get('id')
} }
this.form.data('done')(data); }).done(function () {
this.form.data('done', undefined); that.model.remove(that.asset.get('id'));
this.hide(); that.hide();
} }).fail(function (xhr, err){
alert('Δεν ήταν δυνατή η διαγραφή του χώρου');
});
}
}); });
new LabsView({ model: new Labs() }); new LabsView({ model: new Labs() });
}()); }(window.EDULABS.utils));
\ No newline at end of file \ No newline at end of file
...@@ -12,23 +12,38 @@ namespace GrEduLabs\Schools\Action\Lab; ...@@ -12,23 +12,38 @@ namespace GrEduLabs\Schools\Action\Lab;
use GrEduLabs\Schools\Service\LabServiceInterface; use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\StaffServiceInterface; use GrEduLabs\Schools\Service\StaffServiceInterface;
use RedBeanPHP\R;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
use Slim\Views\Twig; use Slim\Views\Twig;
class ListAll class ListAll
{ {
/**
*
* @var Twig
*/
protected $view; protected $view;
/**
*
* @var LabServiceInterface
*/
protected $labService;
/**
*
* @var StaffServiceInterface
*/
protected $staffService;
public function __construct( public function __construct(
Twig $view, Twig $view,
LabServiceInterface $labservice, LabServiceInterface $labservice,
StaffServiceInterface $staffservice StaffServiceInterface $staffservice
) { ) {
$this->view = $view; $this->view = $view;
$this->labservice = $labservice; $this->labService = $labservice;
$this->staffservice = $staffservice; $this->staffService = $staffservice;
} }
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
...@@ -38,44 +53,25 @@ class ListAll ...@@ -38,44 +53,25 @@ class ListAll
return $res->withStatus(403, 'No school'); return $res->withStatus(403, 'No school');
} }
$labs = $this->labservice->getLabsBySchoolId($school->id); $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;
}
return $this->view->render($res, 'schools/labs.twig', [ return $this->view->render($res, 'schools/labs.twig', [
'labs' => $labs_formatted, 'labs' => $labs,
'staff' => $clean_staff, 'staff' => array_map(function ($teacher) {
'lab_types' => [ return ['value' => $teacher['id'], 'label' => $teacher['fullname']];
[ }, $this->staffService->getTeachersBySchoolId($school->id)),
'value' => 1, 'network_options' => array_map(function ($option) {
'label' => 'ΕΡΓΑΣΤΗΡΙΟ', return ['value' => $option, 'label' => $option];
], }, $this->labService->getHasNetworkValues()),
[ 'server_options' => array_map(function ($option) {
'value' => 2, return ['value' => $option, 'label' => $option];
'label' => 'ΑΙΘΟΥΣΑ', }, $this->labService->getHasServerValues()),
], 'lab_types' => array_map(function ($type) {
[ return ['value' => $type['id'], 'label' => $type['name']];
'value' => 3, }, $this->labService->getLabTypes()),
'label' => 'ΓΡΑΦΕΙΟ', 'lessons_options' => array_map(function ($lesson) {
], return ['value' => $lesson['id'], 'label' => $lesson['name']];
], }, $this->labService->getLessons()),
'lessons' => $lessons_formatted,
]); ]);
} }
} }
...@@ -33,26 +33,21 @@ class PersistLab ...@@ -33,26 +33,21 @@ class PersistLab
$params = $req->getParams(); $params = $req->getParams();
$id = $params['id']; $id = $params['id'];
$params['school_id'] = $school->id; $params['school_id'] = $school->id;
$params['lessons'] = [1,2];
unset($params['id']); unset($params['id']);
try { try {
if ($id > 0) { if ($id) {
$id = $this->labservice->updateLab($params, $id); $lab = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id); $res = $res->withStatus(200);
} else { } else {
$lab = $this->labservice->createLab($params);
$id = $this->labservice->createLab($params); $res = $res->withStatus(201);
if ($id > 0) {
$lab = $this->labservice->getLabById($id);
}
}
if (isset($lab)) {
return $res->withJson($lab->export())->withStatus(201);
} }
$res = $res->withJson($lab);
} catch (Exception $ex) { } catch (Exception $ex) {
$res = $res->withStatus(500, $ex->getMessage()); $res = $res->withStatus(500, $ex->getMessage());
} }
return $res->withStatus(400); return $res;
} }
} }
...@@ -17,7 +17,7 @@ trait InputFilterTrait ...@@ -17,7 +17,7 @@ trait InputFilterTrait
{ {
$this->inputFilter->setData($data); $this->inputFilter->setData($data);
$isValid = $this->inputFilter->isValid(); $isValid = $this->inputFilter->isValid();
return [ return [
'is_valid' => $isValid, 'is_valid' => $isValid,
'values' => $isValid ? $this->inputFilter->getValues() : [], 'values' => $isValid ? $this->inputFilter->getValues() : [],
......
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
namespace GrEduLabs\Schools\InputFilter; namespace GrEduLabs\Schools\InputFilter;