BxBaseStudioAgentsAutomators.php 34 KB

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