Skip to content

Commit

Permalink
Widget and Report check is module disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Jun 17, 2022
1 parent 2e79a9b commit 0f5f592
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/Models/Common/Widget.php
Expand Up @@ -53,6 +53,22 @@ public function users()
return $this->hasManyThrough('App\Models\Auth\User', 'App\Models\Common\Dashboard');
}

/**
* Get the alias based on class.
*
* @return string
*/
public function getAliasAttribute()
{
if (Str::startsWith($this->class, 'App\\')) {
return 'core';
}

$arr = explode('\\', $this->class);

return Str::kebab($arr[1]);
}

/**
* Create a new factory instance for the model.
*
Expand Down
4 changes: 4 additions & 0 deletions app/Traits/Modules.php
Expand Up @@ -439,6 +439,10 @@ public function moduleIsEnabled($alias): bool
return false;
}

if (module($alias)->disabled()) {
return false;
}

if (! Module::alias($alias)->enabled()->first()) {
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions app/Utilities/Reports.php
Expand Up @@ -4,10 +4,13 @@

use App\Models\Common\Report;
use App\Models\Module\Module;
use App\Traits\Modules;
use Illuminate\Support\Str;

class Reports
{
use Modules;

public static function getClasses($check_permission = true)
{
$classes = [];
Expand Down Expand Up @@ -35,6 +38,12 @@ public static function getClasses($check_permission = true)
continue;
}

$alias = static::getModuleAlias($class);

if (! empty($alias) && (new Reports)->moduleIsDisabled($alias)) {
continue;
}

$classes[$class] = static::getDefaultName($class);
}

Expand All @@ -51,6 +60,10 @@ public static function getClassInstance($model, $load_data = true)
return false;
}

if ($model->alias != 'core' && (new Reports)->moduleIsDisabled($model->alias)) {
return false;
}

$class = $model->class;

return new $class($model, $load_data);
Expand Down
27 changes: 22 additions & 5 deletions app/Utilities/Widgets.php
Expand Up @@ -4,10 +4,13 @@

use App\Models\Common\Widget;
use App\Models\Module\Module;
use App\Traits\Modules;
use Illuminate\Support\Str;

class Widgets
{
use Modules;

public static $core_widgets = [
'App\Widgets\Receivables',
'App\Widgets\Payables',
Expand All @@ -27,21 +30,27 @@ public static function getClasses($alias = 'core', $check_permission = true)
}

Module::enabled()->each(function ($module) use (&$list, $alias) {
if (!in_array($alias, [$module->alias, 'all'])) {
if (! in_array($alias, [$module->alias, 'all'])) {
return;
}

$m = module($module->alias);

if (!$m || empty($m->get('widgets'))) {
if (! $m || empty($m->get('widgets'))) {
return;
}

$list = array_merge($list, (array) $m->get('widgets'));
});

foreach ($list as $class) {
if (!class_exists($class) || ($check_permission && !static::canRead($class))) {
if (! class_exists($class) || ($check_permission && ! static::canRead($class))) {
continue;
}

$alias = static::getModuleAlias($class);

if (! empty($alias) && (new Widgets)->moduleIsDisabled($alias)) {
continue;
}

Expand All @@ -62,7 +71,11 @@ public static function getClassInstance($model)

$model = Widget::where('dashboard_id', session('dashboard_id'))->where('class', $class_name)->first();

if (!$model instanceof Widget) {
if ($model->alias != 'core' && (new Widgets)->moduleIsDisabled($model->alias)) {
return false;
}

if (! $model instanceof Widget) {
$class = (new $class_name());

$model = new Widget();
Expand All @@ -79,6 +92,10 @@ public static function getClassInstance($model)
return false;
}

if ($model->alias != 'core' && (new Widgets)->moduleIsDisabled($model->alias)) {
return false;
}

$class_name = $model->class;
}

Expand All @@ -87,7 +104,7 @@ public static function getClassInstance($model)

public static function show($model, ...$arguments)
{
if (!$class = static::getClassInstance($model)) {
if (! $class = static::getClassInstance($model)) {
return '';
}

Expand Down

0 comments on commit 0f5f592

Please sign in to comment.