BxBaseStudioAgentsAutomators.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
  11. {
  12. protected $_sUrlPage;
  13. protected $_sFieldName;
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionAdd()
  25. {
  26. $sAction = 'add';
  27. $oAI = BxDolAI::getInstance();
  28. $aForm = $this->_getForm($sAction);
  29. $oForm = new BxTemplFormView($aForm);
  30. $oForm->initChecker();
  31. if($oForm->isSubmittedAndValid()) {
  32. $aValsToAdd = [
  33. 'added' => time()
  34. ];
  35. $sName = $oForm->getCleanValue($this->_sFieldName);
  36. $sName = $this->_getAutomatorName($sName);
  37. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  38. $iProfileId = $oForm->getCleanValue('profile_id');
  39. if(empty($iProfileId)) {
  40. $iProfileId = (int)getParam('sys_agents_profile');
  41. if(empty($iProfileId))
  42. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  43. $aValsToAdd['profile_id'] = $iProfileId;
  44. }
  45. $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
  46. if(!empty($sSchedulerTime))
  47. $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
  48. $iModel = $oForm->getCleanValue('model_id');
  49. $sType = $oForm->getCleanValue('type');
  50. $aProviders = $oForm->getCleanValue('providers');
  51. $aHelpers = $oForm->getCleanValue('helpers');
  52. $sMessage = $oForm->getCleanValue('message');
  53. $bMessage = !empty($sMessage);
  54. $sMessageAdd = '';
  55. $sMessageResponse = '';
  56. $oAIModel = $oAI->getModelObject($iModel);
  57. $sInstructions = $oAI->getAutomatorInstruction('profile', $iProfileId);
  58. if(!empty($aProviders) && is_array($aProviders))
  59. $sInstructions .= $oAI->getAutomatorInstruction('providers', $aProviders);
  60. if(!empty($aHelpers) && is_array($aHelpers))
  61. $sInstructions .= $oAI->getAutomatorInstruction('helpers', $aHelpers);
  62. $bIsValid = true;
  63. $aResponseInit = [];
  64. switch($sType) {
  65. case BX_DOL_AI_AUTOMATOR_EVENT:
  66. if(($aResponseInit = $oAIModel->getResponseInit($sType, $sMessage)) !== false) {
  67. $oAIModel->setParams($aResponseInit['params']);
  68. $sMessageAdd = $aResponseInit['params']['trigger'];
  69. $aValsToAdd = array_merge($aValsToAdd, $aResponseInit);
  70. if(!empty($aValsToAdd['params']) && is_array($aValsToAdd['params']))
  71. $aValsToAdd['params'] = json_encode($aValsToAdd['params']);
  72. }
  73. else {
  74. $oForm->aInputs['message']['error'] = _t('_sys_agents_automators_err_event_not_found');
  75. $bIsValid = false;
  76. }
  77. break;
  78. case BX_DOL_AI_AUTOMATOR_SCHEDULER:
  79. case BX_DOL_AI_AUTOMATOR_WEBHOOK:
  80. if(($aResponseInit = $oAIModel->getResponseInit($sType, $sMessage)) !== false) {
  81. $oAIModel->setParams($aResponseInit['params']);
  82. $aValsToAdd = array_merge($aValsToAdd, $aResponseInit);
  83. if(!empty($aValsToAdd['params']) && is_array($aValsToAdd['params']))
  84. $aValsToAdd['params'] = json_encode($aValsToAdd['params']);
  85. }
  86. else {
  87. $oForm->aInputs['message']['error'] = _t('_sys_agents_automators_err_event_not_found');
  88. $bIsValid = false;
  89. }
  90. break;
  91. }
  92. if($bIsValid) {
  93. if(($sResponse = $oAIModel->getResponse($sType, $sMessage . $sInstructions . $sMessageAdd, $oAIModel->getParams())) !== false) {
  94. $sMessageResponse = $sResponse;
  95. }
  96. else {
  97. $oForm->aInputs['message']['error'] = _t('_sys_agents_automators_err_cannot_get_code');
  98. $bIsValid = false;
  99. }
  100. }
  101. if($bIsValid) {
  102. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  103. $aProviders = $oForm->getCleanValue('providers');
  104. if(!empty($aProviders) && is_array($aProviders))
  105. foreach($aProviders as $iProviderId)
  106. $this->_oDb->insertAutomatorProvider([
  107. 'automator_id' => $iId,
  108. 'provider_id' => $iProviderId
  109. ]);
  110. $aHelpers = $oForm->getCleanValue('helpers');
  111. if(!empty($aHelpers) && is_array($aHelpers))
  112. foreach($aHelpers as $iHelperId)
  113. $this->_oDb->insertAutomatorHelper([
  114. 'automator_id' => $iId,
  115. 'helper_id' => $iHelperId
  116. ]);
  117. if(($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $iId)) !== null) {
  118. if($bMessage) {
  119. $iProfileId = bx_get_logged_profile_id();
  120. $aResult = $oCmts->addAuto([
  121. 'cmt_author_id' => $iProfileId,
  122. 'cmt_parent_id' => 0,
  123. 'cmt_text' => $sMessage
  124. ]);
  125. if(!empty($aResult['id']))
  126. $this->_oDb->updateAutomators(['message_id' => $aResult['id']], ['id' => $iId]);
  127. sleep(1);
  128. $oCmts->addAuto([
  129. 'cmt_author_id' => $iProfileId,
  130. 'cmt_parent_id' => 0,
  131. 'cmt_text' => $sInstructions
  132. ]);
  133. }
  134. if($sMessageResponse) {
  135. sleep(1);
  136. $oCmts->addAuto([
  137. 'cmt_author_id' => $this->_iProfileIdAi,
  138. 'cmt_parent_id' => 0,
  139. 'cmt_text' => $sMessageResponse
  140. ]);
  141. }
  142. }
  143. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  144. }
  145. else
  146. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  147. return echoJson($aRes);
  148. }
  149. }
  150. $sFormId = $oForm->getId();
  151. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  152. 'form_id' => $sFormId,
  153. 'form' => $oForm->getCode(true),
  154. 'object' => $this->_sObject,
  155. 'action' => $sAction
  156. ]));
  157. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  158. }
  159. public function performActionEdit()
  160. {
  161. $sAction = 'edit';
  162. $oAI = BxDolAI::getInstance();
  163. $iId = $this->_getId();
  164. $aAutomator = $oAI->getAutomator($iId);
  165. $aAutomator['providers'] = $this->_oDb->getAutomatorsBy(['sample' => 'providers_by_id_pairs', 'id' => $iId]);
  166. $aAutomator['helpers'] = $this->_oDb->getAutomatorsBy(['sample' => 'helpers_by_id_pairs', 'id' => $iId]);
  167. $aForm = $this->_getFormEdit($sAction, $aAutomator);
  168. $oForm = new BxTemplFormView($aForm);
  169. $oForm->initChecker();
  170. if($oForm->isSubmittedAndValid()) {
  171. $aValsToAdd = [];
  172. $sName = $oForm->getCleanValue($this->_sFieldName);
  173. if($aAutomator[$this->_sFieldName] != $sName) {
  174. $sName = $this->_getAutomatorName($sName);
  175. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  176. }
  177. /**
  178. * Process Providers
  179. */
  180. $aProvidersIds = $oForm->getCleanValue('providers_ids');
  181. $bProvidersIds = !empty($aProvidersIds) && is_array($aProvidersIds);
  182. $aProvidersValues = $oForm->getCleanValue('providers');
  183. $bProvidersValues = !empty($aProvidersValues) && is_array($aProvidersValues);
  184. //--- Providers: Remove deleted
  185. if(!empty($aAutomator['providers']) && is_array($aAutomator['providers']))
  186. $this->_oDb->deleteAutomatorProvidersById(array_diff(array_keys($aAutomator['providers']), $bProvidersIds ? $aProvidersIds : []));
  187. //--- Providers: Update existed
  188. if($bProvidersIds)
  189. foreach($aProvidersIds as $iIndex => $iApId)
  190. $this->_oDb->updateAutomatorProvider(['provider_id' => (int)$aProvidersValues[$iIndex]], ['id' => (int)$iApId]);
  191. //--- Providers: Add new
  192. $iProvidersIds = $bProvidersIds ? count($aProvidersIds) : 0;
  193. $iProvidersValues = $bProvidersValues ? count($aProvidersValues) : 0;
  194. if($iProvidersValues > $iProvidersIds) {
  195. $aProvidersValues = array_slice($aProvidersValues, $iProvidersIds);
  196. foreach($aProvidersValues as $iProvidersValue)
  197. $this->_oDb->insertAutomatorProvider([
  198. 'automator_id' => $iId,
  199. 'provider_id' => (int)$iProvidersValue,
  200. ]);
  201. }
  202. /**
  203. * Process Helpers
  204. */
  205. $aHelpersIds = $oForm->getCleanValue('helpers_ids');
  206. $bHelpersIds = !empty($aHelpersIds) && is_array($aHelpersIds);
  207. $aHelpersValues = $oForm->getCleanValue('helpers');
  208. $bHelpersValues = !empty($aHelpersValues) && is_array($aHelpersValues);
  209. //--- Helpers: Remove deleted
  210. if(!empty($aAutomator['helpers']) && is_array($aAutomator['helpers']))
  211. $this->_oDb->deleteAutomatorHelpersById(array_diff(array_keys($aAutomator['helpers']), $bHelpersIds ? $aHelpersIds : []));
  212. //--- Helpers: Update existed
  213. if($bHelpersIds)
  214. foreach($aHelpersIds as $iIndex => $iAhId)
  215. $this->_oDb->updateAutomatorHelper(['helper_id' => (int)$aHelpersValues[$iIndex]], ['id' => (int)$iAhId]);
  216. //--- Helpers: Add new
  217. $iHelpersIds = $bHelpersIds ? count($aHelpersIds) : 0;
  218. $iHelpersValues = $bHelpersValues ? count($aHelpersValues) : 0;
  219. if($iHelpersValues > $iHelpersIds) {
  220. $aHelpersValues = array_slice($aHelpersValues, $iHelpersIds);
  221. foreach($aHelpersValues as $iHelpersValue)
  222. $this->_oDb->insertAutomatorHelper([
  223. 'automator_id' => $iId,
  224. 'helper_id' => (int)$iHelpersValue,
  225. ]);
  226. }
  227. $iProfileId = $oForm->getCleanValue('profile_id');
  228. if(empty($iProfileId)) {
  229. $iProfileId = (int)getParam('sys_agents_profile');
  230. if(empty($iProfileId))
  231. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  232. $aValsToAdd['profile_id'] = $iProfileId;
  233. }
  234. $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
  235. if(!empty($sSchedulerTime))
  236. $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
  237. if($oForm->update($iId, $aValsToAdd) !== false) {
  238. if(($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $iId)) !== null) {
  239. $sInstructions = $oAI->getAutomatorInstruction('profile', $iProfileId);
  240. $aProviders = $this->_oDb->getAutomatorsBy(['sample' => 'providers_by_id_pairs', 'id' => $iId]);
  241. if(!empty($aProviders) && is_array($aProviders))
  242. $sInstructions .= $oAI->getAutomatorInstruction('providers', array_values($aProviders));
  243. $aHelpers = $this->_oDb->getAutomatorsBy(['sample' => 'helpers_by_id_pairs', 'id' => $iId]);
  244. if(!empty($aHelpers) && is_array($aHelpers))
  245. $sInstructions .= $oAI->getAutomatorInstruction('helpers', array_values($aHelpers));
  246. $oCmts->addAuto([
  247. 'cmt_author_id' => $iProfileId,
  248. 'cmt_parent_id' => 0,
  249. 'cmt_text' => $sInstructions
  250. ]);
  251. if(($sResponse = $oAI->getModelObject($aAutomator['model_id'])->getResponse($aAutomator['type'], $sInstructions, $aAutomator['params'])) !== false) {
  252. sleep(1);
  253. $oCmts->addAuto([
  254. 'cmt_author_id' => $this->_iProfileIdAi,
  255. 'cmt_parent_id' => 0,
  256. 'cmt_text' => $sResponse
  257. ]);
  258. }
  259. }
  260. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  261. }
  262. else
  263. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  264. return echoJson($aRes);
  265. }
  266. $sFormId = $oForm->getId();
  267. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  268. 'form_id' => $sFormId,
  269. 'form' => $oForm->getCode(true),
  270. 'object' => $this->_sObject,
  271. 'action' => $sAction
  272. ]));
  273. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  274. }
  275. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  276. {
  277. if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  278. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  279. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  280. }
  281. protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  282. {
  283. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  284. if(!empty($aModel) && is_array($aModel))
  285. $mixedValue = $aModel['title'];
  286. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  287. }
  288. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  289. {
  290. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  291. }
  292. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  293. {
  294. return parent::_getCellDefault(_t('_sys_agents_automators_field_type_' . $mixedValue), $sKey, $aField, $aRow);
  295. }
  296. protected function _getCellData($sKey, $aField, $aRow)
  297. {
  298. if($sKey == 'message_id' && ($iCmtId = (int)$aRow[$sKey]) != 0 && ($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $aRow['id'])) !== null) {
  299. $aCmt = $oCmts->getCommentSimple($iCmtId);
  300. if(!empty($aCmt) && is_array($aCmt))
  301. $aRow[$sKey] = $aCmt['cmt_text'];
  302. }
  303. return parent::_getCellData($sKey, $aField, $aRow);
  304. }
  305. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  306. {
  307. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  308. }
  309. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  310. {
  311. return parent::_getCellDefault(_t('_sys_agents_automators_field_status_' . $mixedValue), $sKey, $aField, $aRow);
  312. }
  313. protected function _getActionTune($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  314. {
  315. $a['attr'] = array_merge($a['attr'], [
  316. "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
  317. ]);
  318. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  319. }
  320. protected function _addJsCss()
  321. {
  322. parent::_addJsCss();
  323. $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_automators.js']);
  324. $oForm = new BxTemplStudioFormView([]);
  325. $oForm->addCssJs();
  326. }
  327. protected function _isCheckboxDisabled($aRow)
  328. {
  329. return false;
  330. }
  331. protected function _getActionsDisabledBehavior($aRow)
  332. {
  333. return false;
  334. }
  335. protected function _delete ($mixedId)
  336. {
  337. $mixedResult = parent::_delete($mixedId);
  338. if($mixedResult)
  339. $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
  340. return $mixedResult;
  341. }
  342. protected function _getFormEdit($sAction, $aAutomator = [])
  343. {
  344. $aForm = $this->_getForm($sAction, $aAutomator);
  345. $aForm['form_attrs']['action'] .= '&id=' . $aAutomator['id'];
  346. unset($aForm['inputs']['type']);
  347. unset($aForm['inputs']['message']);
  348. return $aForm;
  349. }
  350. protected function _getForm($sAction, $aAutomator = [])
  351. {
  352. $sJsObject = $this->getPageJsObject();
  353. $sType = isset($aAutomator['type']) ? $aAutomator['type'] : 'event';
  354. if(!empty($aAutomator['params']) && is_string($aAutomator['params']))
  355. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  356. $aForm = array(
  357. 'form_attrs' => array(
  358. 'id' => 'bx_std_agents_automators_' . $sAction,
  359. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_automators&a=' . $sAction,
  360. 'method' => 'post',
  361. ),
  362. 'params' => array (
  363. 'db' => array(
  364. 'table' => 'sys_agents_automators',
  365. 'key' => 'id',
  366. 'submit_name' => 'do_submit',
  367. ),
  368. ),
  369. 'inputs' => array(
  370. 'name' => [
  371. 'type' => 'text',
  372. 'name' => 'name',
  373. 'caption' => _t('_sys_agents_automators_field_name'),
  374. 'value' => isset($aAutomator['name']) ? $aAutomator['name'] : '',
  375. 'required' => '1',
  376. 'checker' => [
  377. 'func' => 'Avail',
  378. 'params' => [],
  379. 'error' => _t('_sys_agents_form_field_err_enter'),
  380. ],
  381. 'db' => [
  382. 'pass' => 'Xss',
  383. ],
  384. ],
  385. 'model_id' => [
  386. 'type' => 'select',
  387. 'name' => 'model_id',
  388. 'caption' => _t('_sys_agents_automators_field_model_id'),
  389. 'info' => '',
  390. 'value' => isset($aAutomator['model_id']) ? $aAutomator['model_id'] : BxDolAI::getInstance()->getDefaultModel(),
  391. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs']),
  392. 'required' => '1',
  393. 'checker' => [
  394. 'func' => 'Avail',
  395. 'params' => [],
  396. 'error' => _t('_sys_agents_form_field_err_select'),
  397. ],
  398. 'db' => [
  399. 'pass' => 'Int',
  400. ],
  401. ],
  402. 'profile_id' => [
  403. 'type' => 'select',
  404. 'name' => 'profile_id',
  405. 'caption' => _t('_sys_agents_automators_field_profile_id'),
  406. 'info' => _t('_sys_agents_automators_field_profile_id_inf'),
  407. 'value' => isset($aAutomator['profile_id']) ? $aAutomator['profile_id'] : 0,
  408. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  409. 'required' => '0',
  410. 'db' => [
  411. 'pass' => 'Int',
  412. ]
  413. ],
  414. 'type' => [
  415. 'type' => 'select',
  416. 'name' => 'type',
  417. 'caption' => _t('_sys_agents_automators_field_type'),
  418. 'info' => '',
  419. 'value' => $sType,
  420. 'values' => [
  421. 'event' => _t('_sys_agents_automators_field_type_event'),
  422. 'scheduler' => _t('_sys_agents_automators_field_type_scheduler'),
  423. 'webhook' => _t('_sys_agents_automators_field_type_webhook'),
  424. ],
  425. 'attrs' => [
  426. 'onchange' => $this->getPageJsObject() . '.onChangeAutomatorType(this)',
  427. ],
  428. 'required' => '1',
  429. 'db' => [
  430. 'pass' => 'Xss',
  431. ]
  432. ],
  433. 'alert_unit' => [
  434. 'type' => 'hidden',
  435. 'name' => 'alert_unit',
  436. 'caption' => _t('_sys_agents_automators_field_alert_unit'),
  437. 'value' => isset($aAutomator['alert_unit']) ? $aAutomator['alert_unit'] : '',
  438. 'tr_attrs' => [
  439. 'style' => $sType != 'event' ? 'display:none' : ''
  440. ],
  441. 'db' => [
  442. 'pass' => 'Xss',
  443. ],
  444. ],
  445. 'alert_action' => [
  446. 'type' => 'hidden',
  447. 'name' => 'alert_action',
  448. 'caption' => _t('_sys_agents_automators_field_alert_action'),
  449. 'value' => isset($aAutomator['alert_action']) ? $aAutomator['alert_action'] : '',
  450. 'tr_attrs' => [
  451. 'style' => $sType != 'event' ? 'display:none' : ''
  452. ],
  453. 'db' => [
  454. 'pass' => 'Xss',
  455. ],
  456. ],
  457. 'scheduler_time' => [
  458. 'type' => 'hidden',
  459. 'name' => 'scheduler_time',
  460. 'caption' => _t('_sys_agents_automators_field_scheduler_time'),
  461. 'value' => isset($aAutomator['params']['scheduler_time']) ? $aAutomator['params']['scheduler_time'] : '',
  462. 'tr_attrs' => [
  463. 'style' => $sType != 'scheduler' ? 'display:none' : ''
  464. ],
  465. ],
  466. 'providers' => [
  467. 'type' => 'custom',
  468. 'name' => 'providers',
  469. 'caption' => _t('_sys_agents_automators_field_providers'),
  470. 'value' => '',
  471. ],
  472. 'helpers' => [
  473. 'type' => 'custom',
  474. 'name' => 'helpers',
  475. 'caption' => _t('_sys_agents_automators_field_helpers'),
  476. 'value' => '',
  477. ],
  478. 'message' => [
  479. 'type' => 'textarea',
  480. 'name' => 'message',
  481. 'caption' => _t('_sys_agents_automators_field_message'),
  482. 'required' => '1',
  483. 'checker' => [
  484. 'func' => 'Avail',
  485. 'params' => [],
  486. 'error' => _t('_sys_agents_automators_field_message_err'),
  487. ],
  488. ],
  489. 'submit' => array(
  490. 'type' => 'input_set',
  491. 0 => array (
  492. 'type' => 'submit',
  493. 'name' => 'do_submit',
  494. 'value' => _t('_sys_submit'),
  495. ),
  496. 1 => array (
  497. 'type' => 'reset',
  498. 'name' => 'close',
  499. 'value' => _t('_sys_close'),
  500. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  501. ),
  502. ),
  503. ),
  504. );
  505. if(isset($aForm['inputs']['providers'])) {
  506. $oForm = new BxTemplFormView([]);
  507. $sProviders = 'providers';
  508. $aProviders = array_map(function($sTitle) {
  509. return _t($sTitle);
  510. }, ['' => '_sys_please_select'] + $this->_oDb->getProvidersBy(['sample' => 'all_pairs', 'active' => 1]));
  511. $aTmplVarsProviders = [];
  512. if(!empty($aAutomator[$sProviders]) && is_array($aAutomator[$sProviders])) {
  513. foreach($aAutomator[$sProviders] as $iApId => $iProviderId) {
  514. $aInputSelect = [
  515. 'type' => 'select',
  516. 'name' => $sProviders . '[]',
  517. 'values' => $aProviders,
  518. 'value' => (int)$iProviderId,
  519. 'attrs' => [
  520. 'class' => 'bx-def-margin-sec-top-auto'
  521. ]
  522. ];
  523. $sInput = $oForm->genInput($aInputSelect);
  524. $aInputHidden = [
  525. 'type' => 'hidden',
  526. 'name' => $sProviders . '_ids[]',
  527. 'value' => (int)$iApId,
  528. ];
  529. $sInput .= $oForm->genInput($aInputHidden);
  530. $aTmplVarsProviders[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  531. }
  532. }
  533. else {
  534. $aInputSelect = [
  535. 'type' => 'select',
  536. 'name' => $sProviders . '[]',
  537. 'values' => $aProviders,
  538. 'value' => '',
  539. 'attrs' => [
  540. 'class' => 'bx-def-margin-sec-top-auto'
  541. ]
  542. ];
  543. $aTmplVarsProviders = [
  544. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  545. ];
  546. }
  547. $aInputButton = [
  548. 'type' => 'button',
  549. 'name' => $sProviders . '_add',
  550. 'value' => _t('_sys_agents_automators_field_providers_add'),
  551. 'attrs' => [
  552. 'class' => 'bx-def-margin-sec-top',
  553. 'onclick' => $sJsObject . ".providerAdd(this, '" . $sProviders . "');"
  554. ]
  555. ];
  556. $aForm['inputs'][$sProviders]['content'] = $this->_oTemplate->parseHtmlByName('agents_automator_form_providers.html', [
  557. 'bx_repeat:providers' => $aTmplVarsProviders,
  558. 'btn_add' => $oForm->genInputButton($aInputButton)
  559. ]);
  560. }
  561. if(isset($aForm['inputs']['helpers'])) {
  562. $oForm = new BxTemplFormView([]);
  563. $sHelpers = 'helpers';
  564. $aHelpers = array_map(function($sTitle) {
  565. return _t($sTitle);
  566. }, ['' => '_sys_please_select'] + $this->_oDb->getHelpersBy(['sample' => 'all_pairs', 'active' => 1]));
  567. $aTmplVarsHelpers = [];
  568. if(!empty($aAutomator[$sHelpers]) && is_array($aAutomator[$sHelpers])) {
  569. foreach($aAutomator[$sHelpers] as $iAhId => $iHelperId) {
  570. $aInputSelect = [
  571. 'type' => 'select',
  572. 'name' => $sHelpers . '[]',
  573. 'values' => $aHelpers,
  574. 'value' => (int)$iHelperId,
  575. 'attrs' => [
  576. 'class' => 'bx-def-margin-sec-top-auto'
  577. ]
  578. ];
  579. $sInput = $oForm->genInput($aInputSelect);
  580. $aInputHidden = [
  581. 'type' => 'hidden',
  582. 'name' => $sHelpers . '_ids[]',
  583. 'value' => (int)$iAhId,
  584. ];
  585. $sInput .= $oForm->genInput($aInputHidden);
  586. $aTmplVarsHelpers[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  587. }
  588. }
  589. else {
  590. $aInputSelect = [
  591. 'type' => 'select',
  592. 'name' => $sHelpers . '[]',
  593. 'values' => $aHelpers,
  594. 'value' => '',
  595. 'attrs' => [
  596. 'class' => 'bx-def-margin-sec-top-auto'
  597. ]
  598. ];
  599. $aTmplVarsHelpers = [
  600. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  601. ];
  602. }
  603. $aInputButton = [
  604. 'type' => 'button',
  605. 'name' => $sHelpers . '_add',
  606. 'value' => _t('_sys_agents_automators_field_helpers_add'),
  607. 'attrs' => [
  608. 'class' => 'bx-def-margin-sec-top',
  609. 'onclick' => $sJsObject . ".helperAdd(this, '" . $sHelpers . "');"
  610. ]
  611. ];
  612. $aForm['inputs'][$sHelpers]['content'] = $this->_oTemplate->parseHtmlByName('agents_automator_form_helpers.html', [
  613. 'bx_repeat:helpers' => $aTmplVarsHelpers,
  614. 'btn_add' => $oForm->genInputButton($aInputButton)
  615. ]);
  616. }
  617. return $aForm;
  618. }
  619. protected function _getId()
  620. {
  621. $aIds = bx_get('ids');
  622. if(!empty($aIds) && is_array($aIds))
  623. return array_shift($aIds);
  624. $iId = (int)bx_get('id');
  625. if(!$iId)
  626. return false;
  627. return $iId;
  628. }
  629. protected function _getAutomatorName($sName)
  630. {
  631. return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
  632. }
  633. }
  634. /** @} */