BxBaseStudioAgentsHelpers.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 BxBaseStudioAgentsHelpers extends BxDolStudioAgentsHelpers
  11. {
  12. protected $_sUrlPage;
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. parent::__construct ($aOptions, $oTemplate);
  16. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=helpers';
  17. }
  18. public function getPageJsObject()
  19. {
  20. return 'oBxDolStudioPageAgents';
  21. }
  22. protected function _getActionsDisabledBehavior($aRow)
  23. {
  24. return false;
  25. }
  26. public function performActionAdd()
  27. {
  28. $sAction = 'add';
  29. $aForm = $this->_getForm($sAction);
  30. $oForm = new BxTemplFormView($aForm);
  31. $oForm->initChecker();
  32. if($oForm->isSubmittedAndValid()) {
  33. $aValsToAdd = ['added' => time()];
  34. $iProfileId = $oForm->getCleanValue('profile_id');
  35. if(empty($iProfileId)) {
  36. $iProfileId = (int)getParam('sys_agents_profile');
  37. if(empty($iProfileId))
  38. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  39. $aValsToAdd['profile_id'] = $iProfileId;
  40. }
  41. $aValsToAdd['name'] = $oForm->getCleanValue('name');
  42. $aValsToAdd['prompt'] = $oForm->getCleanValue('prompt');
  43. $bIsValid = true;
  44. if($bIsValid) {
  45. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  46. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  47. }
  48. else
  49. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  50. return echoJson($aRes);
  51. }
  52. }
  53. $sFormId = $oForm->getId();
  54. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_helpers_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  55. 'form_id' => $sFormId,
  56. 'form' => $oForm->getCode(true),
  57. 'object' => $this->_sObject,
  58. 'action' => $sAction
  59. ]));
  60. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  61. }
  62. public function performActionEdit()
  63. {
  64. $sAction = 'edit';
  65. $iId = $this->_getId();
  66. $aHelper = $this->_oDb->getHelpersBy(['sample' => 'id', 'id' => $iId]);
  67. $aForm = $this->_getFormEdit($sAction, $aHelper);
  68. $oForm = new BxTemplFormView($aForm);
  69. $oForm->initChecker();
  70. if($oForm->isSubmittedAndValid()) {
  71. $aValsToAdd = [];
  72. $iProfileId = $oForm->getCleanValue('profile_id');
  73. if(empty($iProfileId)) {
  74. $iProfileId = (int)getParam('sys_agents_profile');
  75. if(empty($iProfileId))
  76. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  77. $aValsToAdd['profile_id'] = $iProfileId;
  78. }
  79. $aValsToAdd['name'] = $oForm->getCleanValue('name');
  80. $aValsToAdd['prompt'] = $oForm->getCleanValue('prompt');
  81. if($oForm->update($iId, $aValsToAdd) !== false)
  82. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  83. else
  84. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  85. return echoJson($aRes);
  86. }
  87. $sFormId = $oForm->getId();
  88. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_helpers_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  89. 'form_id' => $sFormId,
  90. 'form' => $oForm->getCode(true),
  91. 'object' => $this->_sObject,
  92. 'action' => $sAction
  93. ]));
  94. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  95. }
  96. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  97. {
  98. if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  99. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  100. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  101. }
  102. protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  103. {
  104. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  105. if(!empty($aModel) && is_array($aModel))
  106. $mixedValue = $aModel['title'];
  107. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  108. }
  109. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  110. {
  111. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  112. }
  113. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  114. {
  115. return parent::_getCellDefault(_t('_sys_agents_helpers_field_type_' . $mixedValue), $sKey, $aField, $aRow);
  116. }
  117. protected function _getCellData($sKey, $aField, $aRow)
  118. {
  119. if($sKey == 'message_id' && ($iCmtId = (int)$aRow[$sKey]) != 0 && ($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $aRow['id'])) !== null) {
  120. $aCmt = $oCmts->getCommentSimple($iCmtId);
  121. if(!empty($aCmt) && is_array($aCmt))
  122. $aRow[$sKey] = $aCmt['cmt_text'];
  123. }
  124. return parent::_getCellData($sKey, $aField, $aRow);
  125. }
  126. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  127. {
  128. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  129. }
  130. protected function _getActionCheck($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  131. {
  132. $a['attr'] = array_merge($a['attr'], [
  133. "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
  134. ]);
  135. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  136. }
  137. protected function _addJsCss()
  138. {
  139. parent::_addJsCss();
  140. $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_helpers.js']);
  141. $oForm = new BxTemplStudioFormView([]);
  142. $oForm->addCssJs();
  143. }
  144. protected function _isCheckboxDisabled($aRow)
  145. {
  146. return false;
  147. }
  148. protected function _getFormEdit($sAction, $aHelper = [])
  149. {
  150. $aForm = $this->_getForm($sAction, $aHelper);
  151. $aForm['form_attrs']['action'] .= '&id=' . $aHelper['id'];
  152. return $aForm;
  153. }
  154. protected function _getForm($sAction, $aHelper = [])
  155. {
  156. $sJsObject = $this->getPageJsObject();
  157. $sType = isset($aHelper['type']) ? $aHelper['type'] : 'event';
  158. $aForm = array(
  159. 'form_attrs' => array(
  160. 'id' => 'bx_std_agents_helpers_' . $sAction,
  161. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_helpers&a=' . $sAction,
  162. 'method' => 'post',
  163. ),
  164. 'params' => array (
  165. 'db' => array(
  166. 'table' => 'sys_agents_helpers',
  167. 'key' => 'id',
  168. 'submit_name' => 'do_submit',
  169. ),
  170. ),
  171. 'inputs' => array(
  172. 'name' => [
  173. 'type' => 'text',
  174. 'name' => 'name',
  175. 'required' => '1',
  176. 'caption' => _t('_sys_agents_helpers_field_name'),
  177. 'value' => isset($aHelper['name']) ? $aHelper['name'] : '',
  178. ],
  179. 'model_id' => [
  180. 'type' => 'select',
  181. 'name' => 'model_id',
  182. 'caption' => _t('_sys_agents_helpers_field_model_id'),
  183. 'info' => '',
  184. 'value' => isset($aHelper['model_id']) ? $aHelper['model_id'] : BxDolAI::getInstance()->getDefaultModel(),
  185. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs']),
  186. 'required' => '1',
  187. 'db' => [
  188. 'pass' => 'Int',
  189. ]
  190. ],
  191. 'profile_id' => [
  192. 'type' => 'select',
  193. 'name' => 'profile_id',
  194. 'caption' => _t('_sys_agents_helpers_field_profile_id'),
  195. 'info' => _t('_sys_agents_helpers_field_profile_id_inf'),
  196. 'value' => isset($aHelper['profile_id']) ? $aHelper['profile_id'] : 0,
  197. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  198. 'required' => '0',
  199. 'db' => [
  200. 'pass' => 'Int',
  201. ]
  202. ],
  203. 'prompt' => [
  204. 'type' => 'textarea',
  205. 'name' => 'prompt',
  206. 'required' => '1',
  207. 'caption' => _t('_sys_agents_helpers_field_prompt'),
  208. 'value' => isset($aHelper['prompt']) ? $aHelper['prompt'] : '',
  209. ],
  210. 'submit' => array(
  211. 'type' => 'input_set',
  212. 0 => array (
  213. 'type' => 'submit',
  214. 'name' => 'do_submit',
  215. 'value' => _t('_sys_submit'),
  216. ),
  217. 1 => array (
  218. 'type' => 'reset',
  219. 'name' => 'close',
  220. 'value' => _t('_sys_close'),
  221. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  222. ),
  223. ),
  224. ),
  225. );
  226. return $aForm;
  227. }
  228. protected function _getId()
  229. {
  230. $aIds = bx_get('ids');
  231. if(!empty($aIds) && is_array($aIds))
  232. return array_shift($aIds);
  233. $iId = (int)bx_get('id');
  234. if(!$iId)
  235. return false;
  236. return $iId;
  237. }
  238. }
  239. /** @} */