1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-27 00:06:14 +01:00

Ottimizzazioni interne

Miglioramento della gestione dei Moduli in modo statico.
Sfruttamento delle collezioni Laravel per migliorare la flessibilità interna.
This commit is contained in:
Thomas Zilio 2018-08-21 11:06:37 +02:00
parent 1ba712c1c8
commit 2127efa03a
7 changed files with 105 additions and 34 deletions

View File

@ -17,11 +17,7 @@ class Group extends Model
public function modules()
{
if ($this->nome == 'Amministratori') {
return Module::all();
} else {
return $this->belongsToMany(Module::class, 'zz_permissions', 'idgruppo', 'idmodule')->withPivot('permessi')->get();
}
return $this->belongsToMany(Module::class, 'zz_permissions', 'idgruppo', 'idmodule')->withPivot('permessi');
}
public function views()

View File

@ -6,12 +6,13 @@ use App;
use Auth;
use Traits\RecordTrait;
use Traits\UploadTrait;
use Traits\StoreTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class Module extends Model
{
use RecordTrait, UploadTrait;
use RecordTrait, UploadTrait, StoreTrait;
protected $table = 'zz_modules';
protected $main_folder = 'modules';
@ -33,6 +34,10 @@ class Module extends Model
static::addGlobalScope('enabled', function (Builder $builder) {
$builder->where('enabled', true);
});
static::addGlobalScope('permission', function (Builder $builder) {
$builder->with('groups');
});
}
/**
@ -42,9 +47,17 @@ class Module extends Model
*/
public function getPermissionAttribute()
{
$result = Auth::user()->is_admin ? 'rw' : $this->pivot->permessi;
if (Auth::user()->is_admin) {
return 'rw';
}
return !empty($result) ? $result : '-';
$group = Auth::user()->group->id;
$pivot = $this->pivot ?? $this->groups->first(function ($item) use ($group) {
return $item->id == $group;
})->pivot;
return $pivot->permessi ?? '-';
}
/**
@ -103,7 +116,7 @@ class Module extends Model
public function groups()
{
return $this->belongsToMany(Group::class, 'zz_permissions', 'idmodule', 'idgruppo');
return $this->belongsToMany(Group::class, 'zz_permissions', 'idmodule', 'idgruppo')->withPivot('permessi');
}
public function clauses()

View File

@ -5,12 +5,13 @@ namespace Models;
use App;
use Traits\RecordTrait;
use Traits\UploadTrait;
use Traits\StoreTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class Plugin extends Model
{
use RecordTrait, UploadTrait;
use RecordTrait, UploadTrait, StoreTrait;
protected $table = 'zz_plugins';
protected $main_folder = 'plugins';

View File

@ -3,15 +3,25 @@
namespace Models;
use Traits\PathTrait;
use Traits\StoreTrait;
use Illuminate\Database\Eloquent\Model;
class PrintTemplate extends Model
{
use PathTrait;
use PathTrait, StoreTrait;
protected $table = 'zz_prints';
protected $main_folder = 'templates';
protected static function boot()
{
parent::boot();
static::addGlobalScope('enabled', function (Builder $builder) {
$builder->where('enabled', true);
});
}
/* Relazioni Eloquent */
public function module()

View File

@ -2,10 +2,13 @@
namespace Models;
use Traits\StoreTrait;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
use StoreTrait;
protected $table = 'zz_settings';
protected $appends = [

View File

@ -9,10 +9,6 @@ use Models\Module;
*/
class Modules
{
/** @var array Elenco dei moduli disponibili */
protected static $modules = [];
protected static $references = [];
/** @var array Elenco delle condizioni aggiuntive disponibili */
protected static $additionals = [];
/** @var array Elenco dei segmenti disponibili */
@ -28,23 +24,14 @@ class Modules
*/
public static function getModules()
{
if (empty(self::$modules)) {
$modules = [];
$references = [];
$results = Module::getAll();
$results = Auth::check() ? Auth::user()->modules() : Module::all();
// Caricamento dei plugin
if (!$results->first()->relationLoaded('plugins')) {
$results->load('plugins');
foreach ($results as $result) {
$modules[$result['id']] = $result;
$references[$result['name']] = $result['id'];
}
self::$modules = $modules;
self::$references = $references;
}
return self::$modules;
return $results;
}
/**
@ -75,13 +62,9 @@ class Modules
*/
public static function get($module)
{
$modules = self::getModules();
self::getModules();
if (!is_numeric($module) && !empty(self::$references[$module])) {
$module = self::$references[$module];
}
return $modules[$module];
return Module::get($module);
}
/**

65
src/Traits/StoreTrait.php Normal file
View File

@ -0,0 +1,65 @@
<?php
namespace Traits;
trait StoreTrait
{
/** @var Illuminate\Support\Collection Collezione degli oggetti disponibili */
protected static $collection = null;
/** @var bool Controllo sul salvataggio globale */
protected static $all = false;
/** @var string Nome della colonna "id" (Primary Key) */
protected static $id = 'id';
/** @var string Nome della colonna "name" */
protected static $name = 'name';
/**
* Restituisce tutti gli oggetti.
*
* @return array
*/
public static function getAll()
{
if (!self::$all) {
self::$collection = self::all();
self::$all = true;
}
return self::$collection;
}
/**
* Restituisce l'oggetto relativo all'identificativo specificato.
*
* @param string|int $identifier
*
* @return StoreTrait
*/
public static function get($identifier)
{
// Inizializzazione
if (!isset(self::$collection)) {
self::$collection = collect();
}
// Ricerca
$result = self::$collection->first(function ($item) use ($identifier) {
return $item->{self::$name} == $identifier || $item->{self::$id} == $identifier;
});
if (!empty($result)) {
return $result;
}
// Consultazione Database
$result = self::where(self::$id, $identifier)
->orWhere(self::$name, $identifier)
->first();
self::$collection->push($result);
return $result;
}
}