Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete favorite module uninstall #860rtkn8f #3111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions app/Jobs/Common/DeleteReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,62 @@ class DeleteReport extends Job implements ShouldDelete
public function handle(): bool
{
\DB::transaction(function () {
$this->deleteFavorite();
$this->deletePin();

$this->model->delete();
});

return true;
}

public function deleteFavorite()
{
$favorites = setting('favorites.menu', []);

if (empty($favorites)) {
return;
}

foreach ($favorites as $user_id => $user_favorites) {
$user_favorites = json_decode($user_favorites, true);

foreach ($user_favorites as $key => $favorite) {
if (! is_array($favorite['route'])) {
continue;
}

if (str_contains($favorite['route'][0], 'reports.show') && $this->model->id == $favorite['route'][1]) {
unset($user_favorites[$key]);
}
}

setting()->set('favorites.menu.' . $user_id, json_encode($user_favorites));
setting()->save();
}
}

public function deletePin()
{
$pins = setting('favorites.report', []);

if (empty($pins)) {
return;
}

foreach ($pins as $user_id => $user_pins) {
$user_pins = json_decode($user_pins, true);

foreach ($user_pins as $key => $pin) {
if ($this->model->id == $pin) {
unset($user_pins[$key]);

break;
}
}

setting()->set('favorites.report.' . $user_id, json_encode($user_pins));
setting()->save();
}
}
}
39 changes: 39 additions & 0 deletions app/Listeners/Module/FinishUninstallation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function handle(Event $event)
$this->deleteWidgets($event->alias);
$this->deleteEmailTemplates($event->alias);
$this->deleteReports($event->alias);
$this->deleteFavorites($event->alias);
}

/**
Expand Down Expand Up @@ -104,4 +105,42 @@ protected function deleteReports($alias)
}
});
}

/**
* Delete any favorite created by the module.
*
* @param string $alias
* @return void
*/
protected function deleteFavorites($alias)
{
$favorites = setting('favorites.menu', []);

if (empty($favorites)) {
return;
}

try {
foreach ($favorites as $user_id => $user_favorites) {
$user_favorites = json_decode($user_favorites, true);

foreach ($user_favorites as $key => $favorite) {
$route = $favorite['route'];

if (is_array($route)) {
$route = $route[0];
}

if (str_contains($route, $alias)) {
unset($user_favorites[$key]);
}
}

setting()->set('favorites.menu.' . $user_id, json_encode($user_favorites));
setting()->save();
}
} catch (Throwable $e) {
report($e);
}
}
}