Handle error when trying to remove a package that other packages depend on.

This commit is contained in:
Flavio Copes
2016-03-17 15:57:10 +01:00
parent a88bd75c02
commit 2c00618b87
4 changed files with 42 additions and 0 deletions

View File

@@ -522,6 +522,23 @@ class Admin
return $dependencies; 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 * Check the passed packages list can be updated
* *

View File

@@ -367,6 +367,22 @@ class AdminController
require_once __DIR__ . '/gpm.php'; 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 <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove those first.";
} else {
$message = "The installed package <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> 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 { try {
$dependencies = $this->admin->dependenciesThatCanBeRemovedWhenRemoving($package); $dependencies = $this->admin->dependenciesThatCanBeRemovedWhenRemoving($package);
$result = \Grav\Plugin\Admin\Gpm::uninstall($package, []); $result = \Grav\Plugin\Admin\Gpm::uninstall($package, []);

View File

@@ -71,6 +71,9 @@ class Packages {
$(document).on('closing', '[data-remodal-id="remove-package"]', () => { $(document).on('closing', '[data-remodal-id="remove-package"]', () => {
Packages.getBackToList(type); Packages.getBackToList(type);
}); });
} else {
$('.remove-package-confirm').addClass('hidden');
$('.remove-package-error').removeClass('hidden');
} }
}); });
} }

View File

@@ -31,5 +31,11 @@
</div> </div>
</div> </div>
<div class="remove-package-error hidden">
<h1>Error removing the {{ type }}</h1>
<div class="button-bar">
<button data-remodal-action="cancel" class="button secondary remodal-cancel"><i class="fa fa-fw fa-close"></i> {{ "PLUGIN_ADMIN.CANCEL"|tu }}</button>
</div>
</div>
</form> </form>
</div> </div>