openstamanager/src/Models/Module.php

191 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace Models;
use Auth;
2018-11-16 13:43:27 +01:00
use Common\Model;
use Illuminate\Database\Eloquent\Builder;
2019-07-29 13:16:55 +02:00
use Modules\Checklists\Traits\ChecklistTrait;
2019-07-26 16:24:20 +02:00
use Traits\Components\NoteTrait;
use Traits\Components\UploadTrait;
2018-12-29 12:03:22 +01:00
use Traits\ManagerTrait;
use Traits\StoreTrait;
class Module extends Model
{
2019-07-11 17:20:58 +02:00
use ManagerTrait;
use UploadTrait;
use StoreTrait;
2019-07-26 11:57:59 +02:00
use NoteTrait;
2019-07-26 16:24:20 +02:00
use ChecklistTrait;
2018-08-11 15:37:38 +02:00
protected $table = 'zz_modules';
2018-08-11 15:37:38 +02:00
protected $main_folder = 'modules';
2019-07-26 16:24:20 +02:00
protected $component_identifier = 'id_module';
protected $variables = [];
protected $appends = [
'permission',
'option',
];
protected $hidden = [
'options',
'options2',
];
2019-07-10 15:02:09 +02:00
public function replacePlaceholders($id_record, $value)
{
$replaces = $this->getPlaceholders($id_record);
$value = str_replace(array_keys($replaces), array_values($replaces), $value);
return $value;
}
2019-07-10 15:02:09 +02:00
public function getPlaceholders($id_record)
{
if (!isset($variables[$id_record])) {
$dbo = $database = database();
// Lettura delle variabili nei singoli moduli
$variables = include $this->filepath('variables.php');
// Sostituzione delle variabili di base
$replaces = [];
foreach ($variables as $key => $value) {
2019-07-10 15:02:09 +02:00
$replaces['{'.$key.'}'] = $value;
}
$this->variables[$id_record] = $replaces;
}
return $this->variables[$id_record];
}
/**
* Restituisce i permessi relativi all'account in utilizzo.
*
* @return string
*/
public function getPermissionAttribute()
{
if (Auth::user()->is_admin) {
return 'rw';
}
$group = Auth::user()->group->id;
2018-09-07 17:43:52 +02:00
$pivot = $this->pivot ?: $this->groups->first(function ($item) use ($group) {
return $item->id == $group;
})->pivot;
2018-09-07 17:43:52 +02:00
return $pivot->permessi ?: '-';
}
/**
* Restituisce i permessi relativi all'account in utilizzo.
*
* @return string
*/
public function getViewsAttribute()
{
$user = Auth::user();
$views = database()->fetchArray('SELECT * FROM `zz_views` WHERE `id_module` = :module_id AND
`id` IN (
SELECT `id_vista` FROM `zz_group_view` WHERE `id_gruppo` = (
SELECT `idgruppo` FROM `zz_users` WHERE `id` = :user_id
))
ORDER BY `order` ASC', [
'module_id' => $this->id,
'user_id' => $user->id,
]);
return $views;
}
public function getOptionAttribute()
{
2018-09-27 15:50:03 +02:00
return !empty($this->options2) ? $this->options2 : $this->options;
}
/* Relazioni Eloquent */
public function plugins()
{
2018-08-11 15:37:38 +02:00
return $this->hasMany(Plugin::class, 'idmodule_to');
}
public function prints()
{
return $this->hasMany(PrintTemplate::class, 'id_module');
}
2019-08-29 10:25:14 +02:00
public function Templates()
{
2019-08-29 10:25:14 +02:00
return $this->hasMany(Template::class, 'id_module');
}
public function views()
{
return $this->hasMany(View::class, 'id_module');
}
public function groups()
{
return $this->belongsToMany(Group::class, 'zz_permissions', 'idmodule', 'idgruppo')->withPivot('permessi');
}
public function clauses()
{
return $this->hasMany(Clause::class, 'idmodule');
}
/* Gerarchia */
public function children()
{
2018-09-06 09:45:49 +02:00
return $this->hasMany(self::class, 'parent')->withoutGlobalScope('enabled')
->orderBy('order');
}
public function parent()
{
2018-09-06 09:45:49 +02:00
return $this->belongsTo(self::class, 'parent')->withoutGlobalScope('enabled');
}
public function allParents()
{
return $this->parent()->with('allParents');
}
public function allChildren()
{
return $this->children()->with('allChildren');
}
public static function getHierarchy()
{
return self::with('allChildren')
2018-10-30 16:29:06 +01:00
->withoutGlobalScope('enabled')
->whereNull('parent')
->orderBy('order')
->get();
}
2018-12-29 12:03:22 +01:00
protected static function boot()
{
parent::boot();
static::addGlobalScope('enabled', function (Builder $builder) {
$builder->where('enabled', true);
});
static::addGlobalScope('permission', function (Builder $builder) {
$builder->with('groups');
});
}
}