BxBaseStudioAgentsHelpers.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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']);
  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 _delete ($mixedId)
  136. {
  137. $mixedResult = parent::_delete($mixedId);
  138. if($mixedResult)
  139. $this->_oDb->deleteAutomatorHelpers(['helper_id' => (int)$mixedId]);
  140. return $mixedResult;
  141. }
  142. protected function _getFormEdit($sAction, $aHelper = [])
  143. {
  144. $aForm = $this->_getForm($sAction, $aHelper);
  145. $aForm['form_attrs']['action'] .= '&id=' . $aHelper['id'];
  146. return $aForm;
  147. }
  148. protected function _getForm($sAction, $aHelper = [])
  149. {
  150. $sJsObject = $this->getPageJsObject();
  151. $aForm = array(
  152. 'form_attrs' => array(
  153. 'id' => 'bx_std_agents_helpers_' . $sAction,
  154. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_helpers&a=' . $sAction,
  155. 'method' => 'post',
  156. ),
  157. 'params' => array (
  158. 'db' => array(
  159. 'table' => 'sys_agents_helpers',
  160. 'key' => 'id',
  161. 'submit_name' => 'do_submit',
  162. ),
  163. ),
  164. 'inputs' => array(
  165. 'name' => [
  166. 'type' => 'text',
  167. 'name' => 'name',
  168. 'required' => '1',
  169. 'caption' => _t('_sys_agents_helpers_field_name'),
  170. 'value' => isset($aHelper['name']) ? $aHelper['name'] : '',
  171. 'required' => '1',
  172. 'checker' => [
  173. 'func' => 'Avail',
  174. 'params' => [],
  175. 'error' => _t('_sys_agents_form_field_err_enter'),
  176. ],
  177. 'db' => [
  178. 'pass' => 'Xss',
  179. ]
  180. ],
  181. 'model_id' => [
  182. 'type' => 'select',
  183. 'name' => 'model_id',
  184. 'caption' => _t('_sys_agents_helpers_field_model_id'),
  185. 'info' => '',
  186. 'value' => isset($aHelper['model_id']) ? $aHelper['model_id'] : BxDolAI::getDefaultModel(),
  187. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'hidden' => 0]),
  188. 'required' => '1',
  189. 'db' => [
  190. 'pass' => 'Int',
  191. ]
  192. ],
  193. 'profile_id' => [
  194. 'type' => 'select',
  195. 'name' => 'profile_id',
  196. 'caption' => _t('_sys_agents_helpers_field_profile_id'),
  197. 'info' => _t('_sys_agents_helpers_field_profile_id_inf'),
  198. 'value' => isset($aHelper['profile_id']) ? $aHelper['profile_id'] : 0,
  199. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  200. 'required' => '0',
  201. 'db' => [
  202. 'pass' => 'Int',
  203. ]
  204. ],
  205. 'description' => [
  206. 'type' => 'textarea',
  207. 'name' => 'description',
  208. 'caption' => _t('_sys_agents_helpers_field_description'),
  209. 'value' => isset($aHelper['description']) ? $aHelper['description'] : '',
  210. 'required' => '1',
  211. 'checker' => [
  212. 'func' => 'Avail',
  213. 'params' => [],
  214. 'error' => _t('_sys_agents_form_field_err_enter'),
  215. ],
  216. 'db' => [
  217. 'pass' => 'Xss',
  218. ]
  219. ],
  220. 'prompt' => [
  221. 'type' => 'textarea',
  222. 'name' => 'prompt',
  223. 'caption' => _t('_sys_agents_helpers_field_prompt'),
  224. 'value' => isset($aHelper['prompt']) ? $aHelper['prompt'] : '',
  225. 'required' => '1',
  226. 'checker' => [
  227. 'func' => 'Avail',
  228. 'params' => [],
  229. 'error' => _t('_sys_agents_helpers_field_prompt_err'),
  230. ],
  231. 'db' => [
  232. 'pass' => 'Xss',
  233. ]
  234. ],
  235. 'submit' => array(
  236. 'type' => 'input_set',
  237. 0 => array (
  238. 'type' => 'submit',
  239. 'name' => 'do_submit',
  240. 'value' => _t('_sys_submit'),
  241. ),
  242. 1 => array (
  243. 'type' => 'reset',
  244. 'name' => 'close',
  245. 'value' => _t('_sys_close'),
  246. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  247. ),
  248. ),
  249. ),
  250. );
  251. return $aForm;
  252. }
  253. protected function _getId()
  254. {
  255. $aIds = bx_get('ids');
  256. if(!empty($aIds) && is_array($aIds))
  257. return array_shift($aIds);
  258. $iId = (int)bx_get('id');
  259. if(!$iId)
  260. return false;
  261. return $iId;
  262. }
  263. protected function _getHelperName($sName)
  264. {
  265. return uriGenerate($sName, 'sys_agents_helpers', 'name', ['lowercase' => false]);
  266. }
  267. }
  268. /** @} */