mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-01-01 10:37:31 +01:00
137 lines
3.3 KiB
PHP
Executable File
137 lines
3.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Checklists;
|
|
|
|
use Common\Model;
|
|
use Models\Group;
|
|
use Models\Module;
|
|
use Models\Plugin;
|
|
use Models\User;
|
|
use Modules\Checklists\Traits\ChecklistTrait;
|
|
use Traits\HierarchyTrait;
|
|
|
|
class Check extends Model
|
|
{
|
|
use HierarchyTrait;
|
|
|
|
protected static $parent_identifier = 'id_parent';
|
|
protected $table = 'zz_checks';
|
|
|
|
/**
|
|
* Crea un nuovo elemento della checklist.
|
|
*
|
|
* @param ChecklistTrait $structure
|
|
* @param int $id_record
|
|
* @param string $content
|
|
* @param int $parent_id
|
|
*
|
|
* @return self
|
|
*/
|
|
public static function build(User $user, $structure, $id_record, $content, $parent_id = null)
|
|
{
|
|
$model = parent::build();
|
|
|
|
$model->user()->associate($user);
|
|
$model->id_parent = $parent_id;
|
|
|
|
if ($structure instanceof Module) {
|
|
$model->module()->associate($structure);
|
|
} elseif ($structure instanceof Plugin) {
|
|
$model->plugin()->associate($structure);
|
|
}
|
|
|
|
$model->id_record = $id_record;
|
|
$model->content = $content;
|
|
|
|
// Ordinamento temporaneo alla creazione
|
|
$model->order = 99;
|
|
|
|
$model->save();
|
|
|
|
return $model;
|
|
}
|
|
|
|
public function toggleCheck(User $user)
|
|
{
|
|
$checked_at = $this->checked_at ? null : date('Y-m-d H:i:s');
|
|
$this->checked_at = $checked_at;
|
|
$this->checkUser()->associate($user);
|
|
$this->save();
|
|
|
|
$children = $this->children;
|
|
while (!$children->isEmpty()) {
|
|
$child = $children->shift();
|
|
$child->checked_at = $checked_at;
|
|
$child->checkUser()->associate($user);
|
|
$child->save();
|
|
|
|
$children = $children->merge($child->children);
|
|
}
|
|
}
|
|
|
|
public function setAccess($users, $group_id)
|
|
{
|
|
if (!empty($this->id_parent)) {
|
|
$users = $this->parent->assignedUsers->pluck('id')->toArray();
|
|
$this->assignedUsers()->sync($users);
|
|
|
|
return;
|
|
}
|
|
|
|
if (empty($users)) {
|
|
if (!empty($group_id)) {
|
|
$group = Group::find($group_id);
|
|
|
|
$users = $group->users->pluck('id')->toArray();
|
|
} else {
|
|
$users = User::all()->pluck('id')->toArray();
|
|
}
|
|
}
|
|
|
|
$this->assignedUsers()->sync($users);
|
|
}
|
|
|
|
/*
|
|
* Rimozione ricorsiva gestita da MySQL.
|
|
public function delete()
|
|
{
|
|
return parent::delete();
|
|
|
|
$children = $check->children;
|
|
while (!$children->isEmpty()){
|
|
$child = $children->shift();
|
|
$child->delete();
|
|
|
|
$children = $children->merge($child->children);
|
|
}
|
|
}
|
|
*/
|
|
|
|
/* Relazioni Eloquent */
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function checkUser()
|
|
{
|
|
return $this->belongsTo(User::class, 'checked_by');
|
|
}
|
|
|
|
public function assignedUsers()
|
|
{
|
|
return $this->belongsToMany(User::class, 'zz_check_user', 'id_check', 'id_utente');
|
|
}
|
|
|
|
public function plugin()
|
|
{
|
|
return $this->belongsTo(Plugin::class, 'id_plugin');
|
|
}
|
|
|
|
public function module()
|
|
{
|
|
return $this->belongsTo(Module::class, 'id_module');
|
|
}
|
|
}
|