BxBaseStudioAgentsAgents.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
  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=agents';
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionGetAlert()
  25. {
  26. $aAlert = $this->_getAlertPayload(bx_get('alert'));
  27. echoJson([
  28. 'code' => $aAlert ? 200 : 404,
  29. 'desc' => $aAlert ? $this->_oDb->getAlertDesc(bx_get('alert')) : '',
  30. 'payload' => $aAlert,
  31. ]);
  32. }
  33. public function performActionManual()
  34. {
  35. $iId = $this->_getId();
  36. $aAgent = BxDolAiQuery::getAgentObject($iId);
  37. if (!$aAgent) {
  38. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  39. return;
  40. }
  41. if (!$aAgent['active']) {
  42. echoJson(['msg' => _t('_sys_txt_agent_inactive')]);
  43. return;
  44. }
  45. $oAi = BxDolAI::getInstance();
  46. $sResponse = $oAi->callAgent('manual', $aAgent);
  47. $oParsedown = new Parsedown();
  48. $oParsedown->setSafeMode(true);
  49. $sMessageHtml = $oParsedown->text($sResponse);
  50. $oTemplate = BxDolStudioTemplate::getInstance();
  51. $sHtml = $oTemplate->parseHtmlByName('agents_manual_response.html', [
  52. 'response' => $sMessageHtml
  53. ]);
  54. echoJson(['msg' => $sHtml]);
  55. }
  56. public function performActionWipeChatHistory()
  57. {
  58. $iId = $this->_getId();
  59. $aAgent = BxDolAiQuery::getAgentObject($iId);
  60. if (!$aAgent) {
  61. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  62. return;
  63. }
  64. $oDb = new BxDolAIQuery();
  65. $oDb->wipeAgentChatHistory($aAgent);
  66. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  67. echoJson($aRes);
  68. }
  69. public function performActionAdd()
  70. {
  71. $sAction = 'add';
  72. $oTemplate = BxDolStudioTemplate::getInstance();
  73. $oAI = BxDolAI::getInstance();
  74. $aForm = $this->_getForm($sAction);
  75. $oForm = new BxTemplFormView($aForm);
  76. $oForm->initChecker();
  77. if ($oForm->isSubmitted() && $oForm->getCleanValue('trigger') == 'message') {
  78. $aAgent = $this->_getAgentWithProfile($oForm->getCleanValue('profile_id'));
  79. if ($aAgent) {
  80. $oForm->aInputs['profile_id']['error'] = _t('_sys_agents_form_field_err_profile_has_agent', $aAgent['name']);
  81. $oForm->setValid(false);
  82. }
  83. }
  84. if($oForm->isSubmittedAndValid()) {
  85. $aValsToAdd = [
  86. 'added' => time()
  87. ];
  88. $sName = $oForm->getCleanValue($this->_sFieldName);
  89. $sName = $this->_getUniqName($sName);
  90. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  91. $iProfileId = $oForm->getCleanValue('profile_id');
  92. if(empty($iProfileId)) {
  93. $iProfileId = (int)getParam('sys_agents_profile');
  94. if(empty($iProfileId))
  95. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  96. $aValsToAdd['profile_id'] = $iProfileId;
  97. }
  98. $iModel = $oForm->getCleanValue('model_id');
  99. $sTrigger = $oForm->getCleanValue('trigger');
  100. $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
  101. $aValsToAdd['tools'] = $sTools;
  102. $bIsValid = true;
  103. if($bIsValid) {
  104. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  105. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  106. }
  107. else
  108. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  109. return echoJson($aRes);
  110. }
  111. }
  112. $sFormId = $oForm->getId();
  113. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  114. 'form_id' => $sFormId,
  115. 'form' => $oForm->getCode(true),
  116. 'object' => $this->_sObject,
  117. 'action' => $sAction
  118. ]));
  119. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  120. }
  121. public function performActionEdit()
  122. {
  123. $sAction = 'edit';
  124. $oTemplate = BxDolStudioTemplate::getInstance();
  125. $oAI = BxDolAI::getInstance();
  126. $iId = $this->_getId();
  127. $aAgent = BxDolAiQuery::getAgentObject($iId);
  128. $aForm = $this->_getFormEdit($sAction, $aAgent);
  129. $oForm = new BxTemplFormView($aForm);
  130. $oForm->initChecker();
  131. if ($oForm->isSubmitted() && $oForm->getCleanValue('trigger') == 'message') {
  132. $aAgent = $this->_getAgentWithProfile($oForm->getCleanValue('profile_id'), $iId);
  133. if ($aAgent) {
  134. $oForm->aInputs['profile_id']['error'] = _t('_sys_agents_form_field_err_profile_has_agent', $aAgent['name']);
  135. $oForm->setValid(false);
  136. }
  137. }
  138. if($oForm->isSubmittedAndValid()) {
  139. $aValsToAdd = [];
  140. $sName = $oForm->getCleanValue($this->_sFieldName);
  141. $sName = $this->_getUniqName($sName);
  142. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  143. $iProfileId = $oForm->getCleanValue('profile_id');
  144. if(empty($iProfileId)) {
  145. $iProfileId = (int)getParam('sys_agents_profile');
  146. if(empty($iProfileId))
  147. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  148. $aValsToAdd['profile_id'] = $iProfileId;
  149. }
  150. $iModel = $oForm->getCleanValue('model_id');
  151. $sTrigger = $oForm->getCleanValue('trigger');
  152. $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
  153. $aValsToAdd['tools'] = $sTools;
  154. $bIsValid = true;
  155. if($bIsValid) {
  156. if(($iId = $oForm->update($iId, $aValsToAdd)) !== false) {
  157. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  158. }
  159. else
  160. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  161. return echoJson($aRes);
  162. }
  163. }
  164. $sFormId = $oForm->getId();
  165. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_edit', $aAgent['name']), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  166. 'form_id' => $sFormId,
  167. 'form' => $oForm->getCode(true),
  168. 'object' => $this->_sObject,
  169. 'action' => $sAction
  170. ]));
  171. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  172. }
  173. public function performActionLogs()
  174. {
  175. $iId = $this->_getId();
  176. $aAgent = BxDolAiQuery::getAgentObject($iId);
  177. if (!$aAgent) {
  178. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  179. return;
  180. }
  181. $oGrid = BxDolGrid::getObjectInstance('sys_studio_agents_logs');
  182. $oGrid->addMarkers(['agent_id' => $iId]);
  183. $oGrid->setBrowseParams(['agent_id' => $iId]);
  184. $sGrid = $oGrid->getCode();
  185. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('popup_files_' . $iId, _t('_sys_agents_logs_popup', $aAgent['name']), $this->_oTemplate->parseHtmlByName('agents_popup_grid.html', [
  186. 'grid' => $sGrid,
  187. ]));
  188. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  189. }
  190. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  191. {
  192. // if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  193. // return parent::_getCellDefault('', $sKey, $aField, $aRow);
  194. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  195. }
  196. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  197. {
  198. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  199. }
  200. protected function _delete ($mixedId)
  201. {
  202. $mixedResult = parent::_delete($mixedId);
  203. if($mixedResult) {
  204. // $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
  205. // $this->_oDb->deleteAutomatorHelpers(['automator_id' => (int)$mixedId]);
  206. // $this->_oDb->deleteAutomatorAssistants(['automator_id' => (int)$mixedId]);
  207. // if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($mixedId)) !== false)
  208. // $oCmts->onObjectDelete();
  209. }
  210. return $mixedResult;
  211. }
  212. protected function _getFormEdit($sAction, $aAgent = [])
  213. {
  214. $aForm = $this->_getForm($sAction, $aAgent);
  215. $aForm['form_attrs']['action'] .= '&id=' . $aAgent['id'];
  216. unset($aForm['inputs']['name']);
  217. return $aForm;
  218. }
  219. protected function _getForm($sAction, $aAgent = [])
  220. {
  221. $aForm = array(
  222. 'form_attrs' => array(
  223. 'id' => 'bx_std_agents_' . $sAction,
  224. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_agents&a=' . $sAction,
  225. 'method' => 'post',
  226. ),
  227. 'params' => array (
  228. 'db' => array(
  229. 'table' => 'sys_agents_agents',
  230. 'key' => 'id',
  231. 'submit_name' => 'do_submit',
  232. ),
  233. ),
  234. 'inputs' => array(
  235. 'name' => [
  236. 'type' => 'text',
  237. 'name' => 'name',
  238. 'caption' => _t('_sys_agents_field_name'),
  239. 'info' => _t('_sys_agents_field_name_info'),
  240. 'value' => isset($aAgent['name']) ? $aAgent['name'] : '',
  241. 'required' => '1',
  242. 'checker' => [
  243. 'func' => 'Avail',
  244. 'error' => _t('_sys_agents_form_field_err_enter'),
  245. ],
  246. 'db' => [
  247. 'pass' => 'Xss',
  248. ],
  249. ],
  250. 'model_id' => [
  251. 'type' => 'select',
  252. 'name' => 'model_id',
  253. 'caption' => _t('_sys_agents_field_model_id'),
  254. 'info' => _t('_sys_agents_field_model_id_info'),
  255. 'value' => isset($aAgent['model_id']) ? $aAgent['model_id'] : BxDolAI::getDefaultModel(),
  256. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'capabilities' => ['chatvlm', 'chatllm']]),
  257. 'required' => '1',
  258. 'checker' => [
  259. 'func' => 'Avail',
  260. 'error' => _t('_sys_agents_form_field_err_select'),
  261. ],
  262. 'db' => [
  263. 'pass' => 'Int',
  264. ],
  265. ],
  266. 'profile_id' => [
  267. 'type' => 'select',
  268. 'name' => 'profile_id',
  269. 'caption' => _t('_sys_agents_field_profile_id'),
  270. 'info' => _t('_sys_agents_field_profile_id_inf'),
  271. 'value' => isset($aAgent['profile_id']) ? $aAgent['profile_id'] : 0,
  272. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  273. 'required' => '1',
  274. 'required' => '1',
  275. 'checker' => [
  276. 'func' => 'Avail',
  277. 'error' => _t('_sys_agents_form_field_err_select'),
  278. ],
  279. 'db' => [
  280. 'pass' => 'Int',
  281. ]
  282. ],
  283. 'trigger' => [
  284. 'type' => 'select',
  285. 'name' => 'trigger',
  286. 'caption' => _t('_sys_agents_field_trigger'),
  287. 'info' => _t('_sys_agents_field_trigger_info'),
  288. 'value' => isset($aAgent['trigger']) ? $aAgent['trigger'] : 0,
  289. 'values' => [
  290. 'alert' => 'alert',
  291. 'scheduler' => 'scheduler',
  292. 'webhook' => 'webhook',
  293. 'manual' => 'manual',
  294. 'agent' => 'agent',
  295. 'message' => 'message',
  296. ],
  297. 'attrs' => [
  298. 'onchange' => $this->getPageJsObject() . '.onChangeAgentTrigger(this)',
  299. ],
  300. 'required' => '1',
  301. 'db' => [
  302. 'pass' => 'Xss',
  303. ]
  304. ],
  305. 'alert_section' => array(
  306. 'type' => 'block_header',
  307. 'caption' => 'Trigger - alert',
  308. 'collapsable' => true,
  309. 'collapsed' => true,
  310. ),
  311. 'alert_sample' => [
  312. 'type' => 'custom',
  313. 'name' => 'alert_sample',
  314. 'caption' => _t('_sys_agents_field_alert_sample'),
  315. 'content' => $this->_oTemplate->parseHtmlByName('agents_agents_alerts_payload.html', [
  316. 'text-visibility' => empty($aAgent['alert']) ? 'block' : 'none',
  317. 'text' => _t('_sys_agents_waiting_for_sample_data'),
  318. 'payload-visibility' => !empty($aAgent['alert']) ? 'block' : 'none',
  319. 'desc' => $aAgent ? $this->_oDb->getAlertDesc($aAgent['alert']) : '',
  320. 'payload' => !empty($aAgent['alert']) ? json_encode($this->_getAlertPayload($aAgent['alert']), JSON_PRETTY_PRINT) : '',
  321. ]),
  322. ],
  323. 'alert' => [
  324. 'type' => 'custom',
  325. 'name' => 'alert',
  326. 'caption' => _t('_sys_agents_field_alert'),
  327. 'info' => _t('_sys_agents_field_alert_info'),
  328. 'content' => $this->_oTemplate->parseHtmlByName('agents_agents_alerts_select.html', $this->_getAlertValues(isset($aAgent['alert']) ? $aAgent['alert'] : '')),
  329. 'db' => [
  330. 'pass' => 'Xss',
  331. ],
  332. ],
  333. 'scheduler_section' => [
  334. 'type' => 'block_header',
  335. 'caption' => 'Trigger - scheduler',
  336. 'collapsable' => true,
  337. 'collapsed' => true,
  338. ],
  339. 'scheduler_cron' => [
  340. 'type' => 'text',
  341. 'name' => 'scheduler_cron',
  342. 'caption' => _t('_sys_agents_field_scheduler_cron'),
  343. 'info' => _t('_sys_agents_field_scheduler_cron_info'),
  344. 'value' => isset($aAgent['scheduler_cron']) ? $aAgent['scheduler_cron'] : '',
  345. 'db' => [
  346. 'pass' => 'Xss',
  347. ],
  348. ],
  349. 'webhook_section' => array(
  350. 'type' => 'block_header',
  351. 'caption' => 'Trigger - webhook',
  352. 'collapsable' => true,
  353. 'collapsed' => true,
  354. ),
  355. 'webhook_sample' => [
  356. 'type' => 'custom',
  357. 'name' => 'webhook_sample',
  358. 'caption' => _t('_sys_agents_field_webhook_sample'),
  359. 'content' => isset($aAgent['webhook_sample']) ? $aAgent['webhook_sample'] : _t('_sys_agents_waiting_for_sample_data'),
  360. ],
  361. 'webhook_key' => [
  362. 'type' => 'text',
  363. 'name' => 'webhook_key',
  364. 'caption' => _t('_sys_agents_field_webhook_key'),
  365. 'info' => _t('_sys_agents_field_webhook_key_info'),
  366. 'value' => isset($aAgent['webhook_key']) ? $aAgent['webhook_key'] : '',
  367. 'db' => [
  368. 'pass' => 'Xss',
  369. ],
  370. ],
  371. 'message_section' => array(
  372. 'type' => 'block_header',
  373. 'caption' => 'Trigger - message',
  374. 'collapsable' => true,
  375. 'collapsed' => true,
  376. ),
  377. 'message_profile_id' => [
  378. 'type' => 'select',
  379. 'name' => 'message_profile_id',
  380. 'caption' => _t('_sys_agents_field_message_profile'),
  381. 'info' => _t('_sys_agents_field_message_profile_info'),
  382. 'value' => isset($aAgent['message_profile_id']) ? $aAgent['message_profile_id'] : '',
  383. 'values' => bx_srv('system', 'get_options_agents_profile', [true, '_adm_nav_txt_items_visible_for_all'], 'TemplServices'),
  384. 'db' => [
  385. 'pass' => 'Xss',
  386. ],
  387. ],
  388. 'message_section_end' => array(
  389. 'type' => 'block_end',
  390. ),
  391. 'vector_store_id' => [
  392. 'type' => 'select',
  393. 'name' => 'vector_store_id',
  394. 'caption' => _t('_sys_agents_field_vector_store_id'),
  395. 'info' => _t('_sys_agents_field_vector_store_id_info'),
  396. 'value' => isset($aAgent['vector_store_id']) ? $aAgent['vector_store_id'] : 0,
  397. 'values' => $this->_oDb->getVectorStores(),
  398. 'required' => '1',
  399. 'checker' => [
  400. 'func' => 'Avail',
  401. 'error' => _t('_sys_agents_form_field_err_select'),
  402. ],
  403. 'db' => [
  404. 'pass' => 'Xss',
  405. ],
  406. ],
  407. 'chat_history_context' => [
  408. 'type' => 'text',
  409. 'name' => 'chat_history_context',
  410. 'caption' => _t('_sys_agents_field_chat_history_context'),
  411. 'info' => _t('_sys_agents_field_chat_history_context_info'),
  412. 'value' => isset($aAgent['chat_history_context']) ? $aAgent['chat_history_context'] : 50000,
  413. 'db' => [
  414. 'pass' => 'Int',
  415. ],
  416. ],
  417. 'tools' => [
  418. 'type' => 'custom',
  419. 'name' => 'tools',
  420. 'caption' => _t('_sys_agents_field_tools'),
  421. 'info' => _t('_sys_agents_field_tools_info'),
  422. 'value' => '',
  423. ],
  424. 'prompt_system' => [
  425. 'type' => 'textarea',
  426. 'name' => 'prompt_system',
  427. 'caption' => _t('_sys_agents_field_prompt_system'),
  428. 'info' => _t('_sys_agents_field_prompt_system_info'),
  429. 'value' => isset($aAgent['prompt_system']) ? $aAgent['prompt_system'] : '',
  430. 'db' => [
  431. 'pass' => 'Xss',
  432. ],
  433. ],
  434. 'prompts_section' => array(
  435. 'type' => 'block_header',
  436. 'caption' => 'Other prompts',
  437. 'collapsable' => true,
  438. 'collapsed' => true,
  439. ),
  440. 'prompt_steps' => [
  441. 'type' => 'textarea',
  442. 'name' => 'prompt_steps',
  443. 'caption' => _t('_sys_agents_field_prompt_steps'),
  444. 'info' => _t('_sys_agents_field_prompt_steps_info'),
  445. 'value' => isset($aAgent['prompt_steps']) ? $aAgent['prompt_steps'] : '',
  446. 'db' => [
  447. 'pass' => 'Xss',
  448. ],
  449. ],
  450. 'prompt_output' => [
  451. 'type' => 'textarea',
  452. 'name' => 'prompt_output',
  453. 'caption' => _t('_sys_agents_field_prompt_output'),
  454. 'info' => _t('_sys_agents_field_prompt_output_info'),
  455. 'value' => isset($aAgent['prompt_output']) ? $aAgent['prompt_output'] : '',
  456. 'db' => [
  457. 'pass' => 'Xss',
  458. ],
  459. ],
  460. 'prompt_tools' => [
  461. 'type' => 'textarea',
  462. 'name' => 'prompt_tools',
  463. 'caption' => _t('_sys_agents_field_prompt_tools'),
  464. 'info' => _t('_sys_agents_field_prompt_tools_info'),
  465. 'value' => isset($aAgent['prompt_tools']) ? $aAgent['prompt_tools'] : '',
  466. 'db' => [
  467. 'pass' => 'Xss',
  468. ],
  469. ],
  470. 'prompts_section_end' => array(
  471. 'type' => 'block_end',
  472. ),
  473. 'submit' => $this->_getFormControls(),
  474. ),
  475. );
  476. $this->_getMultiField('tools', $aAgent, 'getTools', 'toolAdd', 'agents_agents_form_tools.html', $aForm);
  477. return $aForm;
  478. }
  479. protected function _getAlertPayload($s)
  480. {
  481. $aAlert = $this->_oDb->getAlert($s);
  482. if (!$aAlert)
  483. return '';
  484. return [
  485. 'object_id' => $aAlert['object'],
  486. 'sender_profile_id' => $aAlert['sender'],
  487. 'extra' => json_decode($aAlert['extra'], true),
  488. 'extra_modifiable_keys' => json_decode($aAlert['extra_refs'], true),
  489. ];
  490. }
  491. protected function _getAlertValues($sValue)
  492. {
  493. $aAlerts = $this->_oDb->getAlerts();
  494. $a = [
  495. 'grid_object' => $this->_sObject,
  496. 'bx_repeat:alerts' => [
  497. [
  498. 'name' => _t('_sys_please_select'),
  499. 'value' => '',
  500. 'desc' => '',
  501. 'bx_if:sel' => [
  502. 'condition' => false,
  503. 'content' => ['sel' => '']
  504. ]
  505. ]
  506. ]
  507. ];
  508. foreach ($aAlerts as $k => $r) {
  509. $a['bx_repeat:alerts'][] = [
  510. 'name' => $r['name'],
  511. 'value' => $r['key'],
  512. 'desc' => bx_html_attribute($r['desc']),
  513. 'bx_if:sel' => [
  514. 'condition' => $sValue == $r['key'],
  515. 'content' => [
  516. 'sel' => $sValue == $r['key'] ? 'selected' : '',
  517. ]
  518. ]
  519. ];
  520. }
  521. return $a;
  522. }
  523. protected function _getUniqName($sName)
  524. {
  525. return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
  526. }
  527. protected function _getMultiField($sField, $aAgent, $sGetValuesMethod, $sJsAddMethod, $sTemplateName, &$aForm)
  528. {
  529. if(!isset($aForm['inputs']['tools']))
  530. return;
  531. $sJsObject = $this->getPageJsObject();
  532. $oForm = new BxTemplFormView([]);
  533. $aValues = ['0' => _t('_sys_please_select')];
  534. $a = $this->_oDb->$sGetValuesMethod();
  535. foreach ($a as $k => $v) {
  536. $aValues[$k] = $v;
  537. }
  538. $aTmplVars = [];
  539. if(!empty($aAgent[$sField])) {
  540. $a = explode(',', $aAgent[$sField]);
  541. foreach($a as $v) {
  542. $aInputSelect = [
  543. 'type' => 'select',
  544. 'name' => $sField . '[]',
  545. 'values' => $aValues,
  546. 'value' => $v,
  547. 'attrs' => [
  548. 'class' => 'bx-def-margin-sec-top-auto'
  549. ]
  550. ];
  551. $sInput = $oForm->genInput($aInputSelect);
  552. $aInputHidden = [
  553. 'type' => 'hidden',
  554. 'name' => $sField . '_ids[]',
  555. 'value' => $v,
  556. ];
  557. $sInput .= $oForm->genInput($aInputHidden);
  558. $aTmplVars[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  559. }
  560. }
  561. else {
  562. $aInputSelect = [
  563. 'type' => 'select',
  564. 'name' => $sField . '[]',
  565. 'values' => $aValues,
  566. 'value' => '',
  567. 'attrs' => [
  568. 'class' => 'bx-def-margin-sec-top-auto'
  569. ]
  570. ];
  571. $aTmplVars = [
  572. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  573. ];
  574. }
  575. $aInputButton = [
  576. 'type' => 'button',
  577. 'name' => $sField . '_add',
  578. 'value' => _t('_adm_nav_btn_items_add'),
  579. 'attrs' => [
  580. 'class' => 'bx-def-margin-sec-top',
  581. 'onclick' => $sJsObject . ".$sJsAddMethod(this, '" . $sField . "');"
  582. ]
  583. ];
  584. $aForm['inputs'][$sField]['content'] = $this->_oTemplate->parseHtmlByName($sTemplateName, [
  585. "bx_repeat:$sField" => $aTmplVars,
  586. 'btn_add' => $oForm->genInputButton($aInputButton)
  587. ]);
  588. }
  589. protected function _getActionManual ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  590. {
  591. if ($aRow['trigger'] != 'manual' && $aRow['trigger'] != 'scheduler')
  592. return '';
  593. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  594. }
  595. protected function _getAgentWithProfile($iProfileId, $iExcludeAgentId = 0)
  596. {
  597. $aAgents = BxDolAi::getInstance()->getAgentsByProfileId($iProfileId);
  598. foreach($aAgents as $aAgent) {
  599. if($aAgent['id'] != $iExcludeAgentId)
  600. return $aAgent;
  601. }
  602. return null;
  603. }
  604. protected function _addJsCss()
  605. {
  606. parent::_addJsCss();
  607. $this->_oTemplate->addJs(['select2/js/select2.min.js']);
  608. $this->_oTemplate->addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'select2/css/|select2.min.css']);
  609. }
  610. }
  611. /** @} */