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

N°7506 - Add method to check if a specific module is installed in iTop #39

Merged
27 changes: 27 additions & 0 deletions core/restclient.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,31 @@ public static function GetFullSynchroDataSource(&$aSource, $iSourceId)

return $bResult;
}

Hipska marked this conversation as resolved.
Show resolved Hide resolved
/**
* Check if the given module is installed in iTop
Hipska marked this conversation as resolved.
Show resolved Hide resolved
* @param string $sName Name of the module to be found
* @param bool $bRequired Whether to throw exceptions when module not found
* @return bool True when the given module is installed, false otherwise
* @throws Exception When the module is required but could not be found
*/
public function CheckModuleInstallation(string $sName, bool $bRequired = false): bool
{
try {
$aResults = static::Get('ModuleInstallation', ['name' => $sName], 'name,version');
if ($aResults['code'] != 0 || empty($aResults['objects'])) {
Hipska marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception($aResults['message'], $aResults['code']);
}
$aObject = current($aResults['objects']);
Utils::Log(LOG_DEBUG, sprintf('iTop module %s version %s is installed.', $aObject['fields']['name'], $aObject['fields']['version']));
} catch (Exception $e) {
$sMessage = sprintf('%s iTop module %s is considered as not installed due to: %s', $bRequired ? 'Required' : 'Optional', $sName, $e->getMessage());
if ($bRequired) throw new Exception($sMessage, 0, $e);
Hipska marked this conversation as resolved.
Show resolved Hide resolved
else {
Utils::Log(LOG_INFO, $sMessage);
return false;
}
}
return true;
}
}