2018-08-10 17:14:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Models;
|
|
|
|
|
|
|
|
use App;
|
|
|
|
use Auth;
|
2018-08-13 10:01:15 +02:00
|
|
|
use Traits\RecordTrait;
|
|
|
|
use Traits\UploadTrait;
|
2018-08-21 11:06:37 +02:00
|
|
|
use Traits\StoreTrait;
|
2018-08-10 17:14:09 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
|
|
|
class Module extends Model
|
|
|
|
{
|
2018-08-21 11:06:37 +02:00
|
|
|
use RecordTrait, UploadTrait, StoreTrait;
|
2018-08-11 15:37:38 +02:00
|
|
|
|
2018-08-10 17:14:09 +02:00
|
|
|
protected $table = 'zz_modules';
|
2018-08-11 15:37:38 +02:00
|
|
|
protected $main_folder = 'modules';
|
2018-08-10 17:14:09 +02:00
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'permission',
|
|
|
|
'option',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $hidden = [
|
|
|
|
'options',
|
|
|
|
'options2',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::addGlobalScope('enabled', function (Builder $builder) {
|
|
|
|
$builder->where('enabled', true);
|
|
|
|
});
|
2018-08-21 11:06:37 +02:00
|
|
|
|
|
|
|
static::addGlobalScope('permission', function (Builder $builder) {
|
|
|
|
$builder->with('groups');
|
|
|
|
});
|
2018-08-10 17:14:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce i permessi relativi all'account in utilizzo.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPermissionAttribute()
|
|
|
|
{
|
2018-08-21 11:06:37 +02:00
|
|
|
if (Auth::user()->is_admin) {
|
|
|
|
return 'rw';
|
|
|
|
}
|
|
|
|
|
|
|
|
$group = Auth::user()->group->id;
|
|
|
|
|
|
|
|
$pivot = $this->pivot ?? $this->groups->first(function ($item) use ($group) {
|
|
|
|
return $item->id == $group;
|
|
|
|
})->pivot;
|
2018-08-10 17:14:09 +02:00
|
|
|
|
2018-08-21 11:06:37 +02:00
|
|
|
return $pivot->permessi ?? '-';
|
2018-08-10 17:14:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
|
|
|
return !empty($this->options) ? $this->options : $this->options2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOptionsAttribute($value)
|
|
|
|
{
|
|
|
|
return App::replacePlaceholder($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOptions2Attribute($value)
|
|
|
|
{
|
|
|
|
return App::replacePlaceholder($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Relazioni Eloquent */
|
|
|
|
|
|
|
|
public function plugins()
|
|
|
|
{
|
2018-08-11 15:37:38 +02:00
|
|
|
return $this->hasMany(Plugin::class, 'idmodule_to');
|
2018-08-10 17:14:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function prints()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PrintTemplate::class, 'id_module');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function views()
|
|
|
|
{
|
|
|
|
return $this->hasMany(View::class, 'id_module');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function groups()
|
|
|
|
{
|
2018-08-21 11:06:37 +02:00
|
|
|
return $this->belongsToMany(Group::class, 'zz_permissions', 'idmodule', 'idgruppo')->withPivot('permessi');
|
2018-08-10 17:14:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function clauses()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Clause::class, 'idmodule');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function children()
|
|
|
|
{
|
|
|
|
return $this->hasMany(self::class, 'parent')
|
|
|
|
->orderBy('order');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parent()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(self::class, 'parent');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function allParents()
|
|
|
|
{
|
|
|
|
return $this->parent()->with('allParents');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function allChildren()
|
|
|
|
{
|
|
|
|
return $this->children()->with('allChildren');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Metodi statici */
|
|
|
|
|
|
|
|
public static function getHierarchy()
|
|
|
|
{
|
|
|
|
return self::with('allChildren')
|
|
|
|
->whereNull('parent')
|
|
|
|
->orderBy('order')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getCompleteHierarchy()
|
|
|
|
{
|
|
|
|
return self::withoutGlobalScope('enabled')
|
|
|
|
->with('allChildren')
|
|
|
|
->whereNull('parent')
|
|
|
|
->orderBy('order')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
}
|