BxBaseStudioAgentsAutomators.php 36 KB

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