BxBaseStudioAgentsAutomators.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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(($aResponseInit = $oAIModel->getResponseInit($sType, $sMessage)) !== 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(($aResponseInit = $oAIModel->getResponseInit($sType, $sMessage)) !== 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(($sResponse = $oAIModel->getResponse($sType, $sMessage . $sInstructions . $sMessageAdd, $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(($sResponse = $oAI->getModelObject($aAutomator['model_id'])->getResponse($aAutomator['type'], $sInstructions, $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 _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  322. {
  323. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  324. if(!empty($aModel) && is_array($aModel))
  325. $mixedValue = $aModel['title'];
  326. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  327. }
  328. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  329. {
  330. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  331. }
  332. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  333. {
  334. return parent::_getCellDefault(_t('_sys_agents_automators_field_type_' . $mixedValue), $sKey, $aField, $aRow);
  335. }
  336. protected function _getCellData($sKey, $aField, $aRow)
  337. {
  338. if($sKey == 'message_id' && ($iCmtId = (int)$aRow[$sKey]) != 0 && ($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($aRow['id'])) !== false) {
  339. $aCmt = $oCmts->getCommentSimple($iCmtId);
  340. if(!empty($aCmt) && is_array($aCmt))
  341. $aRow[$sKey] = $aCmt['cmt_text'];
  342. }
  343. return parent::_getCellData($sKey, $aField, $aRow);
  344. }
  345. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  346. {
  347. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  348. }
  349. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  350. {
  351. return parent::_getCellDefault(_t('_sys_agents_automators_field_status_' . $mixedValue), $sKey, $aField, $aRow);
  352. }
  353. protected function _getActionTune($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  354. {
  355. $a['attr'] = array_merge($a['attr'], [
  356. "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
  357. ]);
  358. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  359. }
  360. protected function _addJsCss()
  361. {
  362. parent::_addJsCss();
  363. $this->_oTemplate->addJs(['jquery.form.min.js']);
  364. $oForm = new BxTemplStudioFormView([]);
  365. $oForm->addCssJs();
  366. }
  367. protected function _isCheckboxDisabled($aRow)
  368. {
  369. return false;
  370. }
  371. protected function _getActionsDisabledBehavior($aRow)
  372. {
  373. return false;
  374. }
  375. protected function _delete ($mixedId)
  376. {
  377. $mixedResult = parent::_delete($mixedId);
  378. if($mixedResult) {
  379. $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
  380. $this->_oDb->deleteAutomatorHelpers(['automator_id' => (int)$mixedId]);
  381. $this->_oDb->deleteAutomatorAssistants(['automator_id' => (int)$mixedId]);
  382. if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($mixedId)) !== false)
  383. $oCmts->onObjectDelete();
  384. }
  385. return $mixedResult;
  386. }
  387. protected function _getFormEdit($sAction, $aAutomator = [])
  388. {
  389. $aForm = $this->_getForm($sAction, $aAutomator);
  390. $aForm['form_attrs']['action'] .= '&id=' . $aAutomator['id'];
  391. unset($aForm['inputs']['type']);
  392. unset($aForm['inputs']['message']);
  393. return $aForm;
  394. }
  395. protected function _getForm($sAction, $aAutomator = [])
  396. {
  397. $sJsObject = $this->getPageJsObject();
  398. $sType = isset($aAutomator['type']) ? $aAutomator['type'] : 'event';
  399. if(!empty($aAutomator['params']) && is_string($aAutomator['params']))
  400. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  401. $aForm = array(
  402. 'form_attrs' => array(
  403. 'id' => 'bx_std_agents_automators_' . $sAction,
  404. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_automators&a=' . $sAction,
  405. 'method' => 'post',
  406. ),
  407. 'params' => array (
  408. 'db' => array(
  409. 'table' => 'sys_agents_automators',
  410. 'key' => 'id',
  411. 'submit_name' => 'do_submit',
  412. ),
  413. ),
  414. 'inputs' => array(
  415. 'name' => [
  416. 'type' => 'text',
  417. 'name' => 'name',
  418. 'caption' => _t('_sys_agents_automators_field_name'),
  419. 'value' => isset($aAutomator['name']) ? $aAutomator['name'] : '',
  420. 'required' => '1',
  421. 'checker' => [
  422. 'func' => 'Avail',
  423. 'params' => [],
  424. 'error' => _t('_sys_agents_form_field_err_enter'),
  425. ],
  426. 'db' => [
  427. 'pass' => 'Xss',
  428. ],
  429. ],
  430. 'model_id' => [
  431. 'type' => 'select',
  432. 'name' => 'model_id',
  433. 'caption' => _t('_sys_agents_automators_field_model_id'),
  434. 'info' => '',
  435. 'value' => isset($aAutomator['model_id']) ? $aAutomator['model_id'] : BxDolAI::getDefaultModel(),
  436. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'hidden' => 0]),
  437. 'required' => '1',
  438. 'checker' => [
  439. 'func' => 'Avail',
  440. 'params' => [],
  441. 'error' => _t('_sys_agents_form_field_err_select'),
  442. ],
  443. 'db' => [
  444. 'pass' => 'Int',
  445. ],
  446. ],
  447. 'profile_id' => [
  448. 'type' => 'select',
  449. 'name' => 'profile_id',
  450. 'caption' => _t('_sys_agents_automators_field_profile_id'),
  451. 'info' => _t('_sys_agents_automators_field_profile_id_inf'),
  452. 'value' => isset($aAutomator['profile_id']) ? $aAutomator['profile_id'] : 0,
  453. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  454. 'required' => '0',
  455. 'db' => [
  456. 'pass' => 'Int',
  457. ]
  458. ],
  459. 'type' => [
  460. 'type' => 'select',
  461. 'name' => 'type',
  462. 'caption' => _t('_sys_agents_automators_field_type'),
  463. 'info' => '',
  464. 'value' => $sType,
  465. 'values' => [
  466. 'event' => _t('_sys_agents_automators_field_type_event'),
  467. 'scheduler' => _t('_sys_agents_automators_field_type_scheduler'),
  468. 'webhook' => _t('_sys_agents_automators_field_type_webhook'),
  469. ],
  470. 'attrs' => [
  471. 'onchange' => $this->getPageJsObject() . '.onChangeAutomatorType(this)',
  472. ],
  473. 'required' => '1',
  474. 'db' => [
  475. 'pass' => 'Xss',
  476. ]
  477. ],
  478. 'alert_unit' => [
  479. 'type' => 'hidden',
  480. 'name' => 'alert_unit',
  481. 'caption' => _t('_sys_agents_automators_field_alert_unit'),
  482. 'value' => isset($aAutomator['alert_unit']) ? $aAutomator['alert_unit'] : '',
  483. 'tr_attrs' => [
  484. 'style' => $sType != 'event' ? 'display:none' : ''
  485. ],
  486. 'db' => [
  487. 'pass' => 'Xss',
  488. ],
  489. ],
  490. 'alert_action' => [
  491. 'type' => 'hidden',
  492. 'name' => 'alert_action',
  493. 'caption' => _t('_sys_agents_automators_field_alert_action'),
  494. 'value' => isset($aAutomator['alert_action']) ? $aAutomator['alert_action'] : '',
  495. 'tr_attrs' => [
  496. 'style' => $sType != 'event' ? 'display:none' : ''
  497. ],
  498. 'db' => [
  499. 'pass' => 'Xss',
  500. ],
  501. ],
  502. 'scheduler_time' => [
  503. 'type' => 'hidden',
  504. 'name' => 'scheduler_time',
  505. 'caption' => _t('_sys_agents_automators_field_scheduler_time'),
  506. 'value' => isset($aAutomator['params']['scheduler_time']) ? $aAutomator['params']['scheduler_time'] : '',
  507. 'tr_attrs' => [
  508. 'style' => $sType != 'scheduler' ? 'display:none' : ''
  509. ],
  510. ],
  511. 'providers' => [
  512. 'type' => 'custom',
  513. 'name' => 'providers',
  514. 'caption' => _t('_sys_agents_automators_field_providers'),
  515. 'value' => '',
  516. ],
  517. 'helpers' => [
  518. 'type' => 'custom',
  519. 'name' => 'helpers',
  520. 'caption' => _t('_sys_agents_automators_field_helpers'),
  521. 'value' => '',
  522. ],
  523. 'assistants' => [
  524. 'type' => 'custom',
  525. 'name' => 'assistants',
  526. 'caption' => _t('_sys_agents_automators_field_assistants'),
  527. 'value' => '',
  528. ],
  529. 'message' => [
  530. 'type' => 'textarea',
  531. 'name' => 'message',
  532. 'caption' => _t('_sys_agents_automators_field_message'),
  533. 'required' => '1',
  534. 'checker' => [
  535. 'func' => 'Avail',
  536. 'params' => [],
  537. 'error' => _t('_sys_agents_automators_field_message_err'),
  538. ],
  539. ],
  540. 'submit' => array(
  541. 'type' => 'input_set',
  542. 0 => array (
  543. 'type' => 'submit',
  544. 'name' => 'do_submit',
  545. 'value' => _t('_sys_submit'),
  546. ),
  547. 1 => array (
  548. 'type' => 'reset',
  549. 'name' => 'close',
  550. 'value' => _t('_sys_close'),
  551. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  552. ),
  553. ),
  554. ),
  555. );
  556. if(isset($aForm['inputs']['providers'])) {
  557. $oForm = new BxTemplFormView([]);
  558. $sProviders = 'providers';
  559. $aProviders = array_map(function($sTitle) {
  560. return _t($sTitle);
  561. }, ['' => '_sys_please_select'] + $this->_oDb->getProvidersBy(['sample' => 'all_pairs', 'active' => 1]));
  562. $aTmplVarsProviders = [];
  563. if(!empty($aAutomator[$sProviders]) && is_array($aAutomator[$sProviders])) {
  564. foreach($aAutomator[$sProviders] as $iApId => $iProviderId) {
  565. $aInputSelect = [
  566. 'type' => 'select',
  567. 'name' => $sProviders . '[]',
  568. 'values' => $aProviders,
  569. 'value' => (int)$iProviderId,
  570. 'attrs' => [
  571. 'class' => 'bx-def-margin-sec-top-auto'
  572. ]
  573. ];
  574. $sInput = $oForm->genInput($aInputSelect);
  575. $aInputHidden = [
  576. 'type' => 'hidden',
  577. 'name' => $sProviders . '_ids[]',
  578. 'value' => (int)$iApId,
  579. ];
  580. $sInput .= $oForm->genInput($aInputHidden);
  581. $aTmplVarsProviders[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  582. }
  583. }
  584. else {
  585. $aInputSelect = [
  586. 'type' => 'select',
  587. 'name' => $sProviders . '[]',
  588. 'values' => $aProviders,
  589. 'value' => '',
  590. 'attrs' => [
  591. 'class' => 'bx-def-margin-sec-top-auto'
  592. ]
  593. ];
  594. $aTmplVarsProviders = [
  595. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  596. ];
  597. }
  598. $aInputButton = [
  599. 'type' => 'button',
  600. 'name' => $sProviders . '_add',
  601. 'value' => _t('_sys_agents_automators_field_providers_add'),
  602. 'attrs' => [
  603. 'class' => 'bx-def-margin-sec-top',
  604. 'onclick' => $sJsObject . ".providerAdd(this, '" . $sProviders . "');"
  605. ]
  606. ];
  607. $aForm['inputs'][$sProviders]['content'] = $this->_oTemplate->parseHtmlByName('agents_automator_form_providers.html', [
  608. 'bx_repeat:providers' => $aTmplVarsProviders,
  609. 'btn_add' => $oForm->genInputButton($aInputButton)
  610. ]);
  611. }
  612. if(isset($aForm['inputs']['helpers'])) {
  613. $oForm = new BxTemplFormView([]);
  614. $sHelpers = 'helpers';
  615. $aHelpers = array_map(function($sTitle) {
  616. return _t($sTitle);
  617. }, ['' => '_sys_please_select'] + $this->_oDb->getHelpersBy(['sample' => 'all_pairs', 'active' => 1]));
  618. $aTmplVarsHelpers = [];
  619. if(!empty($aAutomator[$sHelpers]) && is_array($aAutomator[$sHelpers])) {
  620. foreach($aAutomator[$sHelpers] as $iAhId => $iHelperId) {
  621. $aInputSelect = [
  622. 'type' => 'select',
  623. 'name' => $sHelpers . '[]',
  624. 'values' => $aHelpers,
  625. 'value' => (int)$iHelperId,
  626. 'attrs' => [
  627. 'class' => 'bx-def-margin-sec-top-auto'
  628. ]
  629. ];
  630. $sInput = $oForm->genInput($aInputSelect);
  631. $aInputHidden = [
  632. 'type' => 'hidden',
  633. 'name' => $sHelpers . '_ids[]',
  634. 'value' => (int)$iAhId,
  635. ];
  636. $sInput .= $oForm->genInput($aInputHidden);
  637. $aTmplVarsHelpers[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  638. }
  639. }
  640. else {
  641. $aInputSelect = [
  642. 'type' => 'select',
  643. 'name' => $sHelpers . '[]',
  644. 'values' => $aHelpers,
  645. 'value' => '',
  646. 'attrs' => [
  647. 'class' => 'bx-def-margin-sec-top-auto'
  648. ]
  649. ];
  650. $aTmplVarsHelpers = [
  651. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  652. ];
  653. }
  654. $aInputButton = [
  655. 'type' => 'button',
  656. 'name' => $sHelpers . '_add',
  657. 'value' => _t('_sys_agents_automators_field_helpers_add'),
  658. 'attrs' => [
  659. 'class' => 'bx-def-margin-sec-top',
  660. 'onclick' => $sJsObject . ".helperAdd(this, '" . $sHelpers . "');"
  661. ]
  662. ];
  663. $aForm['inputs'][$sHelpers]['content'] = $this->_oTemplate->parseHtmlByName('agents_automator_form_helpers.html', [
  664. 'bx_repeat:helpers' => $aTmplVarsHelpers,
  665. 'btn_add' => $oForm->genInputButton($aInputButton)
  666. ]);
  667. }
  668. if(isset($aForm['inputs']['assistants'])) {
  669. $oForm = new BxTemplFormView([]);
  670. $sAssistants = 'assistants';
  671. $aAssistants = array_map(function($sTitle) {
  672. return _t($sTitle);
  673. }, ['' => '_sys_please_select'] + $this->_oDb->getAssistantsBy(['sample' => 'all_pairs', 'active' => 1]));
  674. $aTmplVarsAssistants = [];
  675. if(!empty($aAutomator[$sAssistants]) && is_array($aAutomator[$sAssistants])) {
  676. foreach($aAutomator[$sAssistants] as $iAhId => $iAssistantId) {
  677. $aInputSelect = [
  678. 'type' => 'select',
  679. 'name' => $sAssistants . '[]',
  680. 'values' => $aAssistants,
  681. 'value' => (int)$iAssistantId,
  682. 'attrs' => [
  683. 'class' => 'bx-def-margin-sec-top-auto'
  684. ]
  685. ];
  686. $sInput = $oForm->genInput($aInputSelect);
  687. $aInputHidden = [
  688. 'type' => 'hidden',
  689. 'name' => $sAssistants . '_ids[]',
  690. 'value' => (int)$iAhId,
  691. ];
  692. $sInput .= $oForm->genInput($aInputHidden);
  693. $aTmplVarsAssistants[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  694. }
  695. }
  696. else {
  697. $aInputSelect = [
  698. 'type' => 'select',
  699. 'name' => $sAssistants . '[]',
  700. 'values' => $aAssistants,
  701. 'value' => '',
  702. 'attrs' => [
  703. 'class' => 'bx-def-margin-sec-top-auto'
  704. ]
  705. ];
  706. $aTmplVarsAssistants = [
  707. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  708. ];
  709. }
  710. $aInputButton = [
  711. 'type' => 'button',
  712. 'name' => $sAssistants . '_add',
  713. 'value' => _t('_sys_agents_automators_field_assistants_add'),
  714. 'attrs' => [
  715. 'class' => 'bx-def-margin-sec-top',
  716. 'onclick' => $sJsObject . ".assistantAdd(this, '" . $sAssistants . "');"
  717. ]
  718. ];
  719. $aForm['inputs'][$sAssistants]['content'] = $this->_oTemplate->parseHtmlByName('agents_automator_form_assistants.html', [
  720. 'bx_repeat:assistants' => $aTmplVarsAssistants,
  721. 'btn_add' => $oForm->genInputButton($aInputButton)
  722. ]);
  723. }
  724. return $aForm;
  725. }
  726. protected function _getId()
  727. {
  728. $aIds = bx_get('ids');
  729. if(!empty($aIds) && is_array($aIds))
  730. return array_shift($aIds);
  731. $iId = (int)bx_get('id');
  732. if(!$iId)
  733. return false;
  734. return $iId;
  735. }
  736. protected function _getAutomatorName($sName)
  737. {
  738. return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
  739. }
  740. }
  741. /** @} */