Skip to content

Instantly share code, notes, and snippets.

@alexey-kar
Created January 22, 2018 22:37
Show Gist options
  • Save alexey-kar/d4a89b161e08e7d136c04161811bb6c5 to your computer and use it in GitHub Desktop.
Save alexey-kar/d4a89b161e08e7d136c04161811bb6c5 to your computer and use it in GitHub Desktop.
Create recovery agent for module agents
<?
IncludeModuleLangFile(__FILE__);
if (class_exists('mcart_myintegration'))
return;
class mcart_myintegration extends CModule
{
public $MODULE_ID = 'mcart.myintegration';
public $MODULE_VERSION;
public $MODULE_VERSION_DATE;
public $MODULE_NAME;
public $MODULE_DESCRIPTION;
public $MODULE_GROUP_RIGHTS = 'Y';
static $LANG_PREFIX = 'MCART_MYINTEGRATION';
public function mcart_myintegration()
{
$arModuleVersion = array();
$path = str_replace("\\", "/", __FILE__);
$path = substr($path, 0, strlen($path) - strlen('/index.php'));
include($path.'/version.php');
if (is_array($arModuleVersion) && array_key_exists('VERSION', $arModuleVersion)) {
$this->MODULE_VERSION = $arModuleVersion['VERSION'];
$this->MODULE_VERSION_DATE = $arModuleVersion['VERSION_DATE'];
}
$this->MODULE_NAME = GetMessage(self::$LANG_PREFIX.'_MODULE_NAME');
$this->MODULE_DESCRIPTION = GetMessage(self::$LANG_PREFIX.'_MODULE_DESCRIPTION');
$this->PARTNER_NAME = GetMessage(self::$LANG_PREFIX.'_PARTNER_NAME');
$this->PARTNER_URI = 'http://mcart.ru/';
}
public function DoInstall()
{
if (!IsModuleInstalled($this->MODULE_ID)) {
$this->_installDb();
RegisterModule($this->MODULE_ID);
} else {
// say 'Module Already Installed'
}
}
public function DoUninstall()
{
$this->_unInstallDb();
UnRegisterModule($this->MODULE_ID);
}
private function _installDb($arParams = array())
{
$agent_function = '\MCArt\Data\CSourceIntegrator::processIntegration();';
\CAgent::AddAgent(
$agent_function,
$this->MODULE_ID,
'N',
5,
'',
'Y',
'',
30);
//
\CAgent::AddAgent(
'\MCArt\Data\CSourceIntegrator::recoveryAgent("'.$agent_function.'");',
$this->MODULE_ID,
'N',
5,
'',
'Y',
'',
30);
return true;
}
private function _unInstallDb($arParams = array())
{
\CAgent::RemoveModuleAgents($this->MODULE_ID);
return true;
}
}
?>
<?
namespace MCArt\Data;
\Bitrix\Main\Localization\Loc::loadMessages(__FILE__);
if (!\Bitrix\Main\Loader::includeModule('other.module')) {
echo 'Module -other.module- not included';
return true;
}
class CSourceIntegrator
{
public static $MODULE_ID = 'mcart.myintegration';
public static function processIntegration()
{
// ...
// logic module integration
// ...
// like this: return "\MCArt\Data\CSourceIntegrator::processIntegration();";
return '\\'.self::class.'::'.__FUNCTION__.'()';
}
// ...
// other methods
// ...
public static function recoveryAgent($agent_function)
{
$res = \CAgent::GetList(array('ID' => 'DESC'), array(
'MODULE_ID' => self::$MODULE_ID,
'NAME' => $agent_function,
));
if ($arAgent = $res->Fetch()) {
// todo
} else {
\CAgent::AddAgent(
$agent_function,
self::$MODULE_ID,
'N',
5,
'',
'Y',
'',
30);
}
return '\\'.self::class.'::'.__FUNCTION__.'('.$agent_function.')';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment