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

Modules Manager action hooks for module cleanup #7150

Merged
merged 12 commits into from
Jan 15, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* Class to be called from Laminas Module Manager reporting management actions.
* Example is if module is disabled or unregistered ect.
*
* @package OpenEMR Modules
* @link https://www.open-emr.org
* @author Jerry Padgett <sjpadgett@gmail.com>
* @copyright Copyright (c) 2024 Jerry Padgett <sjpadgett@gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

/*
* Currently register isn't supported and support should be a part of install.
* If an error needs to be reported to user, return description of error.
* However, whatever action trapped here has already occurred in Manager.
* Catch any exceptions because chances are they will be overlooked in Laminas module.
* Report them in return value.
*/

class ModuleManagerActionListener
{
// Prevent instantiation
Comment on lines +22 to +24
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradymiller Do we have somewhere to put this file perhaps we have example module somewhere.

private function __construct()
{
}


/**
* @param $methodName
* @param $modId
* @param string $currentActionStatus
* @return string On method success a $currentAction status should be returned or error string.
*/
public static function moduleManagerAction($methodName, $modId, string $currentActionStatus = 'Success'): string
{
// Check if the action method exists
if (method_exists(self::class, $methodName)) {
return self::$methodName($modId, $currentActionStatus);
} else {
// TODO Perhaps this should be an exception!
return "Module cleanup method $methodName does not exist.";
}
}

/**
* @param $modId
* @param $currentActionStatus
* @return mixed
*/
private static function install($modId, $currentActionStatus): mixed
{
return $currentActionStatus;
}

/**
* @param $modId
* @param $currentActionStatus
* @return mixed
*/
private static function enable($modId, $currentActionStatus): mixed
{
return $currentActionStatus;
}

/**
* @param $modId
* @param $currentActionStatus
* @return mixed
*/
private static function disable($modId, $currentActionStatus): mixed
{
return $currentActionStatus;
}

/**
* @param $modId
* @param $currentActionStatus
* @return mixed
*/
private static function unregister($modId, $currentActionStatus): mixed
{
return $currentActionStatus;
}

/**
* @param $modId
* @param $currentActionStatus
* @return mixed
*/
private static function install_sql($modId, $currentActionStatus): mixed
{
return $currentActionStatus;
}

/**
* @param $modId
* @param $currentActionStatus
* @return mixed
*/
private static function upgrade_sql($modId, $currentActionStatus): mixed
{
return $currentActionStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public function manageAction()
$request = $this->getRequest();
$status = $this->listenerObject->z_xlt("Failure");
if ($request->isPost()) {
$modId = $request->getPost('modId') ?? null;
$registryEntry = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
$dirModule = $registryEntry->modDirectory;
$modType = $registryEntry->type;
if ($request->getPost('modAction') == "enable") {
$status = $this->EnableModule($request->getPost('modId'));
Comment on lines 143 to 144
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the heck! This is backwards. enable actually disables the module and disable enables in the controller functions.
Appears interface is reporting the actions button name and not what it is supposed to do!
I guess while here I'll fix this but gesh all I wanted was to add the hooks and not go traipsing all over Laminas!!!

} elseif ($request->getPost('modAction') == "disable") {
Expand Down Expand Up @@ -168,6 +172,10 @@ public function manageAction()
} elseif ($request->getPost('modAction') == "unregister") {
$status = $this->UnregisterModule($request->getPost('modId'));
}
// cleanup action.
if ($modType == InstModuleTable::MODULE_TYPE_CUSTOM) {
$status = $this->callModuleAfterAction($request->getPost('modAction'), $modId, $dirModule, $status);
}
}
$output = "";
if (!empty($div) && is_array($div)) {
Expand All @@ -177,11 +185,40 @@ public function manageAction()
exit(0);
}

/**
* @param $action
* @param $modId
* @param $dirModule
* @param $currentStatus
* @return mixed
*/
private function callModuleAfterAction($action, $modId, $dirModule, $currentStatus): mixed
{
$modPath = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "custom_modules/" . $dirModule;
$moduleClassPath = $modPath . '/ModuleManagerAfterActions.php';
$action = trim($action);
if (!file_exists($moduleClassPath)) {
return $currentStatus;
}
require_once($moduleClassPath);
$className = 'ModuleManagerActionListener';
$methodName = trim($action);
if (class_exists($className)) {
if (method_exists($className, 'moduleManagerAction')) {
return call_user_func([$className, 'moduleManagerAction'], $methodName, $modId);
} else {
return $currentStatus;
}
} else {
return $currentStatus;
}
}

/**
* @param $version
* @return int|string
*/
function upgradeAclFromVersion($ACL_UPGRADE, $version)
private function upgradeAclFromVersion($ACL_UPGRADE, $version)
{
$toVersion = '';
foreach ($ACL_UPGRADE as $toVersion => $function) {
Expand Down