BxBaseStudioAgentsAutomators.php 27 KB

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