Skip to content

Commit

Permalink
delete dashboard/report after module uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Mar 29, 2021
1 parent 9a82779 commit 72bf03e
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Common/Dashboards.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function show($dashboard_id = null)
$dashboard = $this->dispatch(new CreateDashboard([
'company_id' => session('company_id'),
'name' => trans_choice('general.dashboards', 1),
'default_widgets' => true,
'default_widgets' => 'core',
]));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Common/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Widgets extends Controller
*/
public function index()
{
$widgets = Utility::getClasses();
$widgets = Utility::getClasses('all');

return response()->json($widgets);
}
Expand Down
16 changes: 13 additions & 3 deletions app/Jobs/Common/CreateDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Abstracts\Job;
use App\Models\Auth\User;
use App\Models\Common\Company;
use App\Models\Common\Dashboard;
use App\Models\Common\Widget;
use App\Utilities\Widgets;
Expand Down Expand Up @@ -55,7 +56,15 @@ protected function getUsers()
{
$list = [];

if ($this->request->has('users')) {
if ($this->request->has('all_users')) {
Company::find($this->request->get('company_id'))->users()->each(function ($user) use (&$list) {
if (!$this->shouldCreateDashboardFor($user)) {
return;
}

$list[] = $user->id;
});
} elseif ($this->request->has('users')) {
$user_ids = Arr::wrap($this->request->get('users'));

foreach($user_ids as $user_id) {
Expand Down Expand Up @@ -97,7 +106,7 @@ protected function checkAndCreateWidgets()
$sort = 1;

if ($this->request->has('default_widgets')) {
$widgets = Widgets::getClasses(false);
$widgets = Widgets::getClasses($this->request->get('default_widgets'), false);

$this->createWidgets($widgets, $sort);
}
Expand All @@ -118,10 +127,11 @@ protected function createWidgets($widgets, &$sort)
$name = (new $class())->getDefaultName();
}

Widget::create([
Widget::firstOrCreate([
'company_id' => $this->dashboard->company_id,
'dashboard_id' => $this->dashboard->id,
'class' => $class,
], [
'name' => $name,
'sort' => $sort,
'settings' => (new $class())->getDefaultSettings(),
Expand Down
62 changes: 62 additions & 0 deletions app/Listeners/Module/FinishUninstallation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Listeners\Module;

use App\Events\Module\Uninstalled as Event;
use App\Jobs\Common\DeleteDashboard;
use App\Jobs\Common\DeleteReport;
use App\Models\Common\Dashboard;
use App\Models\Common\Report;
use App\Traits\Jobs;
use App\Traits\Relationships;

class FinishUninstallation
{
use Jobs, Relationships;

/**
* Handle the event.
*
* @param Event $event
* @return void
*/
public function handle(Event $event)
{
$this->deleteDashboards($event->alias);
$this->deleteReports($event->alias);
}

/**
* Delete any dashboard created by the module.
*
* @param string $alias
* @return void
*/
protected function deleteDashboards($alias)
{
Dashboard::alias($alias)->get()->each(function ($dashboard) {
try {
$this->dispatch(new DeleteDashboard($dashboard));
} catch (\Exception | \Throwable $e) {
report($e);
}
});
}

/**
* Delete any report created by the module.
*
* @param string $alias
* @return void
*/
protected function deleteReports($alias)
{
Report::alias($alias)->get()->each(function ($report) {
try {
$this->dispatch(new DeleteReport($report));
} catch (\Exception | \Throwable $e) {
report($e);
}
});
}
}
55 changes: 55 additions & 0 deletions app/Models/Common/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;

class Dashboard extends Model
{
Expand Down Expand Up @@ -59,6 +60,60 @@ public function scopeUserId($query, $user_id)
});
}

/**
* Scope to only include dashboards of a given alias.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $alias
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAlias($query, $alias)
{
$class = ($alias == 'core') ? 'App\\\\' : 'Modules\\\\' . Str::studly($alias) . '\\\\';

return $query->whereHas('widgets', function ($query) use ($class) {
// Must have widgets of module
$query->where('class', 'like', $class . '%');
})->whereDoesntHave('widgets', function ($query) use ($class) {
// Must not have widgets from other modules
$query->where('class', 'not like', $class . '%');
});
}

/**
* Get the alias based on class.
*
* @return string
*/
public function getAliasAttribute()
{
$alias = '';

foreach ($this->widgets as $widget) {
if (Str::startsWith($widget->class, 'App\\')) {
$tmp_alias = 'core';
} else {
$arr = explode('\\', $widget->class);

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

// First time set
if ($alias == '') {
$alias = $tmp_alias;
}

// Must not have widgets from different modules
if ($alias != $tmp_alias) {
$alias = '';

break;
}
}

return $alias;
}

/**
* Create a new factory instance for the model.
*
Expand Down
31 changes: 31 additions & 0 deletions app/Models/Common/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
use Illuminate\Support\Str;

class Report extends Model
{
Expand All @@ -26,4 +27,34 @@ class Report extends Model
protected $casts = [
'settings' => 'object',
];

/**
* Scope to only include reports of a given alias.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $alias
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAlias($query, $alias)
{
$class = ($alias == 'core') ? 'App\\\\' : 'Modules\\\\' . Str::studly($alias) . '\\\\';

return $query->where('class', 'like', $class . '%');
}

/**
* 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]);
}
}
3 changes: 3 additions & 0 deletions app/Providers/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Event extends Provider
'App\Listeners\Module\InstallExtraModules',
'App\Listeners\Module\FinishInstallation',
],
'App\Events\Module\Uninstalled' => [
'App\Listeners\Module\FinishUninstallation',
],
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function isUserDashboard($id)
$user = user();

if (empty($user)) {
return false;
return app()->runningInConsole() ? true : false;
}

$dashboard = $user->dashboards()->where('id', $id)->first();
Expand Down
40 changes: 23 additions & 17 deletions app/Utilities/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@

class Widgets
{
public static function getClasses($check_permission = true)
public static function getClasses($alias = 'core', $check_permission = true)
{
$classes = [];

$list = [
'App\Widgets\TotalIncome',
'App\Widgets\TotalExpenses',
'App\Widgets\TotalProfit',
'App\Widgets\CashFlow',
'App\Widgets\IncomeByCategory',
'App\Widgets\ExpensesByCategory',
'App\Widgets\AccountBalance',
'App\Widgets\LatestIncome',
'App\Widgets\LatestExpenses',
'App\Widgets\Currencies',
];

Module::enabled()->each(function ($module) use (&$list) {
$classes = $list = [];

if (in_array($alias, ['core', 'all'])) {
$list = [
'App\Widgets\TotalIncome',
'App\Widgets\TotalExpenses',
'App\Widgets\TotalProfit',
'App\Widgets\CashFlow',
'App\Widgets\IncomeByCategory',
'App\Widgets\ExpensesByCategory',
'App\Widgets\AccountBalance',
'App\Widgets\LatestIncome',
'App\Widgets\LatestExpenses',
'App\Widgets\Currencies',
];
}

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

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

if (!$m || empty($m->get('widgets'))) {
Expand Down
2 changes: 1 addition & 1 deletion database/seeds/Dashboards.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private function create()
$this->dispatch(new CreateDashboard([
'company_id' => $company_id,
'name' => trans_choice('general.dashboards', 1),
'default_widgets' => true,
'default_widgets' => 'core',
'users' => $user_id,
]));
}
Expand Down

0 comments on commit 72bf03e

Please sign in to comment.