From 2c00618b87c7c5cc2ac4e50e8fc37e8c50c94986 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 17 Mar 2016 15:57:10 +0100 Subject: [PATCH] Handle error when trying to remove a package that other packages depend on. --- classes/admin.php | 17 +++++++++++++++++ classes/controller.php | 16 ++++++++++++++++ themes/grav/app/utils/packages.js | 3 +++ .../partials/modal-remove-package.html.twig | 6 ++++++ 4 files changed, 42 insertions(+) diff --git a/classes/admin.php b/classes/admin.php index 2235c63d..99f43955 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -522,6 +522,23 @@ class Admin return $dependencies; } + /** + * Get list of packages that depend on the passed package slug + * + * @param string $slug The package slug + * + * @return array|bool + */ + public function getPackagesThatDependOnPackage($slug) + { + $gpm = $this->gpm(); + if (!$gpm) { + return false; + } + + return $gpm->getPackagesThatDependOnPackage($slug); + } + /** * Check the passed packages list can be updated * diff --git a/classes/controller.php b/classes/controller.php index 29956ab4..b36a3b3c 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -367,6 +367,22 @@ class AdminController require_once __DIR__ . '/gpm.php'; + //check if there are packages that have this as a dependency. Abort and show which ones + $dependent_packages = $this->admin->getPackagesThatDependOnPackage($package); + if (count($dependent_packages) > 0) { + if (count($dependent_packages) > 1) { + $message = "The installed packages " . implode(', ', $dependent_packages) . " depends on this package. Please remove those first."; + } else { + $message = "The installed package " . implode(', ', $dependent_packages) . " depends on this package. Please remove it first."; + } + + $this->admin->json_response = ['status' => 'error', 'message' => $message]; + return; + } + + $this->admin->json_response = ['status' => 'success', 'message' => 'xxx']; + return true; + try { $dependencies = $this->admin->dependenciesThatCanBeRemovedWhenRemoving($package); $result = \Grav\Plugin\Admin\Gpm::uninstall($package, []); diff --git a/themes/grav/app/utils/packages.js b/themes/grav/app/utils/packages.js index cc88b540..1ecb60dc 100644 --- a/themes/grav/app/utils/packages.js +++ b/themes/grav/app/utils/packages.js @@ -71,6 +71,9 @@ class Packages { $(document).on('closing', '[data-remodal-id="remove-package"]', () => { Packages.getBackToList(type); }); + } else { + $('.remove-package-confirm').addClass('hidden'); + $('.remove-package-error').removeClass('hidden'); } }); } diff --git a/themes/grav/templates/partials/modal-remove-package.html.twig b/themes/grav/templates/partials/modal-remove-package.html.twig index d0aa8b12..b6ca6260 100644 --- a/themes/grav/templates/partials/modal-remove-package.html.twig +++ b/themes/grav/templates/partials/modal-remove-package.html.twig @@ -31,5 +31,11 @@ + \ No newline at end of file