BxBaseStudioAgentsHelpers.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 _delete ($mixedId)
  99. {
  100. $mixedResult = parent::_delete($mixedId);
  101. if($mixedResult)
  102. $this->_oDb->deleteAutomatorHelpers(['helper_id' => (int)$mixedId]);
  103. return $mixedResult;
  104. }
  105. protected function _getFormEdit($sAction, $aHelper = [])
  106. {
  107. $aForm = $this->_getForm($sAction, $aHelper);
  108. $aForm['form_attrs']['action'] .= '&id=' . $aHelper['id'];
  109. return $aForm;
  110. }
  111. protected function _getForm($sAction, $aHelper = [])
  112. {
  113. $sJsObject = $this->getPageJsObject();
  114. $aForm = array(
  115. 'form_attrs' => array(
  116. 'id' => 'bx_std_agents_helpers_' . $sAction,
  117. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_helpers&a=' . $sAction,
  118. 'method' => 'post',
  119. ),
  120. 'params' => array (
  121. 'db' => array(
  122. 'table' => 'sys_agents_helpers',
  123. 'key' => 'id',
  124. 'submit_name' => 'do_submit',
  125. ),
  126. ),
  127. 'inputs' => array(
  128. 'name' => [
  129. 'type' => 'text',
  130. 'name' => 'name',
  131. 'required' => '1',
  132. 'caption' => _t('_sys_agents_helpers_field_name'),
  133. 'value' => isset($aHelper['name']) ? $aHelper['name'] : '',
  134. 'required' => '1',
  135. 'checker' => [
  136. 'func' => 'Avail',
  137. 'params' => [],
  138. 'error' => _t('_sys_agents_form_field_err_enter'),
  139. ],
  140. 'db' => [
  141. 'pass' => 'Xss',
  142. ]
  143. ],
  144. 'model_id' => [
  145. 'type' => 'select',
  146. 'name' => 'model_id',
  147. 'caption' => _t('_sys_agents_helpers_field_model_id'),
  148. 'info' => '',
  149. 'value' => isset($aHelper['model_id']) ? $aHelper['model_id'] : BxDolAI::getDefaultModel(),
  150. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'hidden' => 0]),
  151. 'required' => '1',
  152. 'db' => [
  153. 'pass' => 'Int',
  154. ]
  155. ],
  156. 'profile_id' => [
  157. 'type' => 'select',
  158. 'name' => 'profile_id',
  159. 'caption' => _t('_sys_agents_helpers_field_profile_id'),
  160. 'info' => _t('_sys_agents_helpers_field_profile_id_inf'),
  161. 'value' => isset($aHelper['profile_id']) ? $aHelper['profile_id'] : 0,
  162. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  163. 'required' => '0',
  164. 'db' => [
  165. 'pass' => 'Int',
  166. ]
  167. ],
  168. 'description' => [
  169. 'type' => 'textarea',
  170. 'name' => 'description',
  171. 'caption' => _t('_sys_agents_helpers_field_description'),
  172. 'value' => isset($aHelper['description']) ? $aHelper['description'] : '',
  173. 'required' => '1',
  174. 'checker' => [
  175. 'func' => 'Avail',
  176. 'params' => [],
  177. 'error' => _t('_sys_agents_form_field_err_enter'),
  178. ],
  179. 'db' => [
  180. 'pass' => 'Xss',
  181. ]
  182. ],
  183. 'prompt' => [
  184. 'type' => 'textarea',
  185. 'name' => 'prompt',
  186. 'caption' => _t('_sys_agents_helpers_field_prompt'),
  187. 'value' => isset($aHelper['prompt']) ? $aHelper['prompt'] : '',
  188. 'required' => '1',
  189. 'checker' => [
  190. 'func' => 'Avail',
  191. 'params' => [],
  192. 'error' => _t('_sys_agents_helpers_field_prompt_err'),
  193. ],
  194. 'db' => [
  195. 'pass' => 'Xss',
  196. ]
  197. ],
  198. 'submit' => $this->_getFormControls(),
  199. ),
  200. );
  201. return $aForm;
  202. }
  203. protected function _getHelperName($sName)
  204. {
  205. return uriGenerate($sName, 'sys_agents_helpers', 'name', ['lowercase' => false]);
  206. }
  207. }
  208. /** @} */