BxBaseStudioAgentsHelpers.php 11 KB

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