Supporto ai template per le checklist

This commit is contained in:
Thomas Zilio 2019-07-29 17:42:24 +02:00
parent 56f4e10e84
commit 7c1582d29c
12 changed files with 253 additions and 67 deletions

View File

@ -5,6 +5,7 @@ include_once __DIR__.'/core.php';
use Models\Note; use Models\Note;
use Models\User; use Models\User;
use Modules\Checklists\Check; use Modules\Checklists\Check;
use Modules\Checklists\Checklist;
if (empty($structure) || empty($structure['enabled'])) { if (empty($structure) || empty($structure['enabled'])) {
die(tr('Accesso negato')); die(tr('Accesso negato'));
@ -117,17 +118,27 @@ elseif (filter('op') == 'delete_nota') {
flash()->info(tr('Nota interna aggiunta correttamente!')); flash()->info(tr('Nota interna aggiunta correttamente!'));
} }
// Rimozione checklist // Clonazione di una checklist
elseif (filter('op') == 'clone_checklist') {
$content = post('content');
$checklist_id = post('checklist');
$assigned_user = User::find(post('assigned_user'));
$checklist = Checklist::find($checklist_id);
$checklist->copia($user, $assigned_user, $id_record);
}
// Aggiunta check alla checklist
elseif (filter('op') == 'add_check') { elseif (filter('op') == 'add_check') {
$content = post('content'); $content = post('content');
$parent_id = post('parent') ?: null; $parent_id = post('parent') ?: null;
$assigned_user = User::find(post('assigned_user')); $assigned_user = User::find(post('assigned_user'));
$check = Check::build($user, $assigned_user, $structure, $id_record, $content, $parent_id); $check = Check::build($user, $structure, $id_record, $content, $assigned_user, $parent_id);
} }
// Rimozione checklist // Rimozione di un check della checklist
elseif (filter('op') == 'delete_check') { elseif (filter('op') == 'delete_check') {
$check_id = post('check_id'); $check_id = post('check_id');
$check = Check::find($check_id); $check = Check::find($check_id);

View File

@ -44,17 +44,17 @@ echo '
$(document).ready(function() { $(document).ready(function() {
$("#module").change(function() { $("#module").change(function() {
if ($(this).val()){ if ($(this).val()){
$("#plugin").attr("disabled", true); $("#plugin").val("").attr("disabled", true);
} else { } else {
$("#plugin").attr("disabled", false); $("#plugin").val("").attr("disabled", false);
} }
}); });
$("#plugin").change(function() { $("#plugin").change(function() {
if ($(this).val()){ if ($(this).val()){
$("#module").attr("disabled", true); $("#module").val("").attr("disabled", true);
} else { } else {
$("#module").attr("disabled", false); $("#module").val("").attr("disabled", false);
} }
}); });
}); });

View File

@ -17,17 +17,17 @@ echo '
<form action="" method="post" id="check-form"> <form action="" method="post" id="check-form">
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
{[ "type": "text", "label": "'.tr('Contenuto').'", "name": "content", "class": "unblockable", "required": 1 ]} {[ "type": "text", "label": "'.tr('Contenuto').'", "name": "content", "required": 1 ]}
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-md-6">
{[ "type": "select", "label": "'.tr('Genitore').'", "name": "parent", "class": "unblockable", "values": '.json_encode($list).' ]} {[ "type": "select", "label": "'.tr('Genitore').'", "name": "parent", "values": '.json_encode($list).', "required": 1 ]}
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
{[ "type": "select", "label": "'.tr('Utente').'", "name": "assigned_user", "class": "unblockable", "ajax-source": "utenti", "required": 1 ]} {[ "type": "select", "label": "'.tr('Utente').'", "name": "assigned_user", "ajax-source": "utenti", "required": 1 ]}
</div> </div>
</div> </div>
@ -50,6 +50,22 @@ $(document).ready(function() {
$("#check-add").click(function(event){ $("#check-add").click(function(event){
addCheck(this); addCheck(this);
}); });
$("#parent").change(function(){
if ($(this).val()) {
$("#assigned_user").val("").attr("disabled", true).attr("required", false);
} else {
$("#assigned_user").val("").attr("disabled", false).attr("required", true);
}
});
$("#assigned_user").change(function(){
if ($(this).val()) {
$("#parent").val("").attr("disabled", true).attr("required", false);
} else {
$("#parent").val("").attr("disabled", false).attr("required", true);
}
});
}); });
function addCheck(btn) { function addCheck(btn) {

View File

@ -0,0 +1,80 @@
<?php
include_once __DIR__.'/../../../core.php';
$manager_id = filter('manager_id');
$checklists = $structure->checklists();
$list = [];
foreach ($checklists as $checklist) {
$list[] = [
'id' => $checklist->id,
'text' => $checklist->name,
];
}
echo '
<form action="" method="post" id="check-form">
<div class="row">
<div class="col-md-6">
{[ "type": "select", "label": "'.tr('Checklist').'", "name": "checklist", "values": '.json_encode($list).', "required": 1 ]}
</div>
<div class="col-md-6">
{[ "type": "select", "label": "'.tr('Utente').'", "name": "assigned_user", "ajax-source": "utenti", "required": 1 ]}
</div>
</div>
<!-- PULSANTI -->
<div class="row">
<div class="col-md-12 text-right">
<button type="button" class="btn btn-primary" id="check-add">
<i class="fa fa-plus"></i> '.tr('Aggiungi').'
</button>
</div>
</div>
</form>
<script>$(document).ready(init)</script>
<script type="module">
import Checklist from "./modules/checklists/js/checklist.js";
$(document).ready(function() {
$("#check-add").click(function(event){
addChecklist(this);
});
});
function addChecklist(btn) {
var $form = $(btn).closest("form");
var continua = true;
$form.find(":input:not(:button)").each(function (index, value) {
continua &= $(this).parsley().validate();
});
if (!continua) {
swal({
type: "error",
title: "'.tr('Errore').'",
text: "'.tr('Alcuni campi obbligatori non sono stati compilati correttamente.').'",
});
return;
}
var checklist = new Checklist({
id_module: "'.$id_module.'",
id_plugin: "'.$id_plugin.'",
id_record: "'.$id_record.'",
}, "'.$manager_id.'");
checklist.cloneChecklist({
checklist: $form.find("#checklist").val(),
assigned_user: $form.find("#assigned_user").val(),
});
$form.closest(".modal").modal("hide");
}
</script>';

View File

@ -2,7 +2,7 @@
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
use HTMLBuilder\Manager\ChecklistManager; use Modules\Checklists\HTMLBuilder\ChecklistManager;
echo ' echo '
<form action="" method="post" id="edit-form"> <form action="" method="post" id="edit-form">
@ -37,17 +37,17 @@ echo '
$(document).ready(function() { $(document).ready(function() {
$("#module").change(function() { $("#module").change(function() {
if ($(this).val()){ if ($(this).val()){
$("#plugin").attr("disabled", true); $("#plugin").val("").attr("disabled", true);
} else { } else {
$("#plugin").attr("disabled", false); $("#plugin").val("").attr("disabled", false);
} }
}); });
$("#plugin").change(function() { $("#plugin").change(function() {
if ($(this).val()){ if ($(this).val()){
$("#module").attr("disabled", true); $("#module").val("").attr("disabled", true);
} else { } else {
$("#module").attr("disabled", false); $("#module").val("").attr("disabled", false);
} }
}); });
}); });

View File

@ -4,6 +4,12 @@ class Checklist {
this.id = id; this.id = id;
} }
cloneChecklist(data){
data.op = "clone_checklist";
this.request(data);
}
addCheck(data){ addCheck(data){
data.op = "add_check"; data.op = "add_check";
@ -30,24 +36,36 @@ class Checklist {
}); });
} }
deleteCheck(id) { deleteCheck(id, user_id) {
var check = this.findCheck(id); var check = this.findCheck(id);
if (check.user_id != user_id && check.assigned_user_id != user_id) {
return false;
}
check.icon.removeClass("fa-check-square-o").addClass("fa-refresh fa-spin bg-danger").show(); check.icon.removeClass("fa-check-square-o").addClass("fa-refresh fa-spin bg-danger").show();
this.request({ this.request({
op: "delete_check", op: "delete_check",
check_id: id, check_id: id,
}); });
return true;
} }
toggleCheck(id) { toggleCheck(id, user_id) {
var check = this.findCheck(id); var check = this.findCheck(id);
if (check.assigned_user_id != user_id) {
return false;
}
check.icon.removeClass("fa-square-o fa-check-square-o ").addClass("fa-refresh fa-spin").show(); check.icon.removeClass("fa-square-o fa-check-square-o ").addClass("fa-refresh fa-spin").show();
this.request({ this.request({
op: "toggle_check", op: "toggle_check",
check_id: id, check_id: id,
}); });
return true;
} }
findCheck(id) { findCheck(id) {
@ -59,6 +77,8 @@ class Checklist {
date: li.find(".check-date"), date: li.find(".check-date"),
text: li.find(".check-text"), text: li.find(".check-text"),
children: li.find(".check-children"), children: li.find(".check-children"),
user_id: li.data('user_id'),
assigned_user_id: li.data('assigned_user_id'),
}; };
} }

View File

@ -23,17 +23,23 @@ class Check extends Model
* @param User $assigned_user * @param User $assigned_user
* @param ChecklistTrait $structure * @param ChecklistTrait $structure
* @param int $id_record * @param int $id_record
* @param string $contenuto * @param string $content
* @param int $id_parent * @param int $parent_id
* *
* @return self * @return self
*/ */
public static function build(User $user, User $assigned_user, $structure, $id_record, $contenuto, $id_parent = null) public static function build(User $user, $structure, $id_record, $content, User $assigned_user = null, $parent_id = null)
{ {
$model = parent::build(); $model = parent::build();
$model->user()->associate($user); $model->user()->associate($user);
$model->id_parent = $parent_id;
if (empty($parent_id)) {
$model->assignedUser()->associate($assigned_user); $model->assignedUser()->associate($assigned_user);
} else {
$model->assignedUser()->associate($model->parent->assignedUser);
}
if ($structure instanceof Module) { if ($structure instanceof Module) {
$model->module()->associate($structure); $model->module()->associate($structure);
@ -42,8 +48,7 @@ class Check extends Model
} }
$model->id_record = $id_record; $model->id_record = $id_record;
$model->id_parent = $id_parent; $model->content = $content;
$model->content = $contenuto;
$model->save(); $model->save();

View File

@ -3,6 +3,9 @@
namespace Modules\Checklists; namespace Modules\Checklists;
use Common\Model; use Common\Model;
use Models\Module;
use Models\Plugin;
use Models\User;
class Checklist extends Model class Checklist extends Model
{ {
@ -30,6 +33,24 @@ class Checklist extends Model
return $this->checks()->whereNull('id_parent')->orderBy('created_at')->get(); return $this->checks()->whereNull('id_parent')->orderBy('created_at')->get();
} }
public function copia(User $user, User $assigned_user, $id_record)
{
$structure = $this->plugin ?: $this->module;
$checks = $this->mainChecks();
$relations = [];
while (!$checks->isEmpty()) {
$child = $checks->shift();
$id_parent = $child->id_parent ? $relations[$child->id_parent] : null;
$check = Check::build($user, $structure, $id_record, $child->content, $assigned_user, $id_parent);
$relations[$child->id] = $check->id;
$checks = $checks->merge($child->children);
}
}
/* Relazioni Eloquent */ /* Relazioni Eloquent */
public function checks() public function checks()

View File

@ -3,6 +3,7 @@
namespace Modules\Checklists\HTMLBuilder; namespace Modules\Checklists\HTMLBuilder;
use HTMLBuilder\Manager\ManagerInterface; use HTMLBuilder\Manager\ManagerInterface;
use Models\User;
use Modules; use Modules;
use Plugins; use Plugins;
@ -26,18 +27,11 @@ class ChecklistManager implements ManagerInterface
$module = Modules::get('Checklists'); $module = Modules::get('Checklists');
$structure = Plugins::get($options['id_plugin']) ?: Modules::get($options['id_module']); $structure = Plugins::get($options['id_plugin']) ?: Modules::get($options['id_module']);
$utente = \Auth::user();
// ID del form // ID del form
$manager_id = 'checklist_'.$options['id_module'].'_'.$options['id_plugin']; $manager_id = 'checklist_'.$options['id_module'].'_'.$options['id_plugin'];
$checklists = $structure->checklists();
$checklist_select = [];
foreach ($checklists as $checklist) {
$checklist_select[] = [
'id' => $checklist->id,
'text' => $checklist->name,
];
}
$result = ' $result = '
<div class="panel panel-primary" id="'.$manager_id.'" style="position:relative"> <div class="panel panel-primary" id="'.$manager_id.'" style="position:relative">
<div class="panel-heading"> <div class="panel-heading">
@ -70,10 +64,24 @@ class ChecklistManager implements ManagerInterface
<br>'; <br>';
} }
$checks = $structure->mainChecks($options['id_record']);
$users = $checks->groupBy('id_utente_assegnato');
$result .= ' $result .= '
<div class="row">';
foreach ($users as $user_id => $checks) {
$user = User::find($user_id);
$result .= '
<div class="col-md-6" '.($utente->id != $user_id ? 'style="opacity: 0.5"' : '').'>
<div class="box box-info">
<div class="box-header">
<h3 class="box-title">'.$user->nome_completo.'</h3>
</div>
<div class="box-body">
<ul class="checklist">'; <ul class="checklist">';
$checks = $structure->mainChecks($options['id_record']);
foreach ($checks as $check) { foreach ($checks as $check) {
$result .= self::renderChecklist($check); $result .= self::renderChecklist($check);
} }
@ -81,6 +89,13 @@ class ChecklistManager implements ManagerInterface
$result .= ' $result .= '
</ul> </ul>
</div> </div>
</div>
</div>';
}
$result .= '
</div>
</div>
</div>'; </div>';
$result .= ' $result .= '
@ -100,14 +115,22 @@ $(document).ready(function() {
$(".check-item").click(function(event){ $(".check-item").click(function(event){
var id = $(this).attr("id").replace("check_", ""); var id = $(this).attr("id").replace("check_", "");
checklists["'.$manager_id.'"].toggleCheck(id); var result = checklists["'.$manager_id.'"].toggleCheck(id, '.$utente->id.');
if (!result){
swal("'.tr('Errore').'", "'.tr('La tua utenza non corrisponde a quella assegnata al check').'", "error");
}
}); });
$(".check-delete").click(function(event){ $(".check-delete").click(function(event){
var li = $(this).closest("li"); var li = $(this).closest("li");
var id = li.attr("id").replace("check_", ""); var id = li.attr("id").replace("check_", "");
checklists["'.$manager_id.'"].deleteCheck(id); var result = checklists["'.$manager_id.'"].deleteCheck(id, '.$utente->id.');
if (!result){
swal("'.tr('Errore').'", "'.tr('La tua utenza non possiede i permessi di modificare questo check').'", "error");
}
event.stopPropagation(); event.stopPropagation();
}); });
@ -132,7 +155,7 @@ function deleteCheck(id) {
public static function renderChecklist($check, $level = 0) public static function renderChecklist($check, $level = 0)
{ {
$result = ' $result = '
<li id="check_'.$check->id.'" class="check-item'.(!empty($check->checked_at) ? ' checked' : '').'"> <li id="check_'.$check->id.'" class="check-item'.(!empty($check->checked_at) ? ' checked' : '').'" data-user_id="'.$check->id_utente.'" data-assigned_user_id="'.$check->id_utente_assegnato.'">
'.str_repeat('&nbsp;', $level * 8).' '.str_repeat('&nbsp;', $level * 8).'
<i class="check-icon fa '.(!empty($check->checked_at) ? 'fa-check-square-o' : 'fa-square-o').'"></i> <i class="check-icon fa '.(!empty($check->checked_at) ? 'fa-check-square-o' : 'fa-square-o').'"></i>

View File

@ -14,7 +14,7 @@ trait ChecklistTrait
public function mainChecks($id_record) public function mainChecks($id_record)
{ {
return $this->hasMany(Check::class, $this->component_identifier)->where('id_record', $id_record)->whereNull('id_parent')->orderBy('created_at')->get(); return $this->hasMany(Check::class, $this->component_identifier)->where('id_record', $id_record)->whereNull('id_parent')->orderBy('id_utente_assegnato', 'created_at')->get();
} }
public function checklists() public function checklists()

View File

@ -163,6 +163,16 @@ class User extends Model
$this->image_file_id = $upload->id; $this->image_file_id = $upload->id;
} }
public function getNomeCompletoAttribute()
{
$anagrafica = $this->anagrafica;
if (empty($anagrafica)) {
return $this->username;
}
return $anagrafica->ragione_sociale.' ('.$this->username.')';
}
/* Relazioni Eloquent */ /* Relazioni Eloquent */
public function group() public function group()