BxBaseStudioAgentsProviders.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 BxBaseStudioAgentsProviders extends BxDolStudioAgentsProviders
  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=providers';
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionAdd()
  25. {
  26. $sAction = 'add';
  27. $aProvider = [];
  28. if(($iTypeId = bx_get('type_id')) !== false && $iTypeId > 0) {
  29. $aProvider = [
  30. 'type_id' => (int)$iTypeId,
  31. 'options' => $this->_oDb->getProviderOptionsBy(['sample' => 'provider_type_id', 'provider_type_id' => $iTypeId])
  32. ];
  33. }
  34. $aForm = $this->_getForm($sAction, $aProvider);
  35. $oForm = new BxTemplFormView($aForm);
  36. $oForm->initChecker();
  37. if($oForm->isSubmittedAndValid()) {
  38. $aValsToAdd = [
  39. 'profile_id' => bx_get_logged_profile_id(),
  40. 'added' => time(),
  41. 'active' => 1
  42. ];
  43. $sName = $oForm->getCleanValue($this->_sFieldName);
  44. $sName = $this->_getProviderName($sName);
  45. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  46. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  47. if(!empty($aProvider['options']) && is_array($aProvider['options']))
  48. foreach($aProvider['options'] as $aOption)
  49. $this->_oDb->insertProviderValue([
  50. 'provider_id' => $iId,
  51. 'option_id' => $aOption['id'],
  52. 'value' => $oForm->getCleanValue($aOption['name'])
  53. ]);
  54. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  55. }
  56. else
  57. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  58. return echoJson($aRes);
  59. }
  60. $sFormId = $oForm->getId();
  61. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_providers_popup_add'), $this->_oTemplate->parseHtmlByName('agents_provider_form.html', [
  62. 'form_id' => $sFormId,
  63. 'form' => $oForm->getCode(true),
  64. 'object' => $this->_sObject,
  65. 'action' => $sAction
  66. ]));
  67. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  68. }
  69. public function performActionEdit()
  70. {
  71. $sAction = 'edit';
  72. $iId = $this->_getId();
  73. $oAIProvider = BxDolAI::getInstance()->getProviderObject($iId);
  74. if(!$oAIProvider)
  75. return echoJson([]);
  76. $aProviderInfo = $oAIProvider->getInfo();
  77. $aForm = $this->_getFormEdit($sAction, $aProviderInfo);
  78. $oForm = new BxTemplFormView($aForm);
  79. $oForm->initChecker();
  80. if($oForm->isSubmittedAndValid()) {
  81. $sName = $oForm->getCleanValue($this->_sFieldName);
  82. if($aProviderInfo[$this->_sFieldName] != $sName) {
  83. $sName = $this->_getProviderName($sName);
  84. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  85. }
  86. if($oForm->update($iId) === false)
  87. return echoJson(['msg' => _t('_sys_txt_error_occured')]);
  88. if(!empty($aProviderInfo['options']) && is_array($aProviderInfo['options']))
  89. foreach($aProviderInfo['options'] as $aOption)
  90. $this->_oDb->insertProviderValue([
  91. 'provider_id' => $iId,
  92. 'option_id' => $aOption['id'],
  93. 'value' => $oForm->getCleanValue($aOption['name'])
  94. ]);
  95. return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  96. }
  97. $sFormId = $oForm->getId();
  98. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_providers_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_provider_form.html', [
  99. 'form_id' => $sFormId,
  100. 'form' => $oForm->getCode(true),
  101. 'object' => $this->_sObject,
  102. 'action' => $sAction
  103. ]));
  104. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  105. }
  106. public function performActionInfo()
  107. {
  108. $sAction = 'view';
  109. $iId = $this->_getId();
  110. $oAIProvider = BxDolAI::getInstance()->getProviderObject($iId);
  111. if(!$oAIProvider)
  112. return echoJson([]);
  113. $aProviderInfo = $oAIProvider->getInfo();
  114. $aForm = $this->_getFormView($sAction, $aProviderInfo);
  115. $oForm = new BxTemplFormView($aForm);
  116. $oForm->initChecker($aProviderInfo);
  117. $sFormId = $oForm->getId();
  118. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_providers_popup_view'), $this->_oTemplate->parseHtmlByName('agents_provider_form.html', [
  119. 'form_id' => $oForm->getId(),
  120. 'form' => $oForm->getCode(true),
  121. 'object' => $this->_sObject,
  122. 'action' => $sAction
  123. ]));
  124. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => true, 'removeOnClose' => true]]]);
  125. }
  126. protected function _delete ($mixedId)
  127. {
  128. $mixedResult = parent::_delete($mixedId);
  129. if($mixedResult) {
  130. $this->_oDb->deleteProviderValues(['provider_id' => (int)$mixedId]);
  131. $this->_oDb->deleteAutomatorProviders(['provider_id' => (int)$mixedId]);
  132. }
  133. return $mixedResult;
  134. }
  135. protected function _getFormView($sAction, $aProvider = [])
  136. {
  137. $aForm = $this->_getForm($sAction, $aProvider);
  138. $aForm['params']['view_mode'] = 1;
  139. return $aForm;
  140. }
  141. protected function _getFormEdit($sAction, $aProvider = [])
  142. {
  143. $aForm = $this->_getForm($sAction, $aProvider);
  144. $aForm['form_attrs']['action'] .= '&id=' . $aProvider['id'];
  145. return $aForm;
  146. }
  147. protected function _getForm($sAction, $aProvider = [])
  148. {
  149. $bProvider = !empty($aProvider) && is_array($aProvider);
  150. $aForm = [
  151. 'form_attrs' => [
  152. 'id' => 'bx_std_agents_providers_' . $sAction,
  153. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_providers&a=' . $sAction,
  154. 'method' => 'post',
  155. ],
  156. 'params' => [
  157. 'db' => [
  158. 'table' => 'sys_agents_providers',
  159. 'key' => 'id',
  160. 'submit_name' => 'do_submit',
  161. ],
  162. ],
  163. 'inputs' => [
  164. 'controls' => $this->_getFormControls(),
  165. ],
  166. ];
  167. if($sAction == 'add' && !$bProvider) {
  168. $aForm['inputs'] = bx_array_insert_before([
  169. 'type_id' => [
  170. 'type' => 'select',
  171. 'name' => 'type_id',
  172. 'caption' => _t('_sys_agents_providers_field_type_id'),
  173. 'info' => '',
  174. 'value' => isset($aProvider['type_id']) ? $aProvider['type_id'] : 0,
  175. 'values' => array_map(function($sTitle) {
  176. return _t($sTitle);
  177. }, ['' => '_sys_please_select'] + $this->_oDb->getProviderTypesBy(['sample' => 'all_pairs', 'active' => 1])),
  178. 'required' => '1',
  179. 'db' => [
  180. 'pass' => 'Int',
  181. ]
  182. ],
  183. ], $aForm['inputs'], 'controls');
  184. $aForm['inputs']['controls'][0] = array_merge($aForm['inputs']['controls'][0], [
  185. 'name' => 'do_select',
  186. 'value' => _t('_sys_select')
  187. ]);
  188. }
  189. else {
  190. $aInputsAdd = [
  191. 'type_id' => [
  192. 'type' => 'hidden',
  193. 'name' => 'type_id',
  194. 'value' => isset($aProvider['type_id']) ? $aProvider['type_id'] : 0,
  195. 'db' => [
  196. 'pass' => 'Int',
  197. ]
  198. ],
  199. 'name' => [
  200. 'type' => 'text',
  201. 'name' => 'name',
  202. 'caption' => _t('_sys_agents_providers_field_name'),
  203. 'value' => isset($aProvider['name']) ? $aProvider['name'] : '',
  204. 'required' => '1',
  205. 'checker' => [
  206. 'func' => 'Avail',
  207. 'params' => [],
  208. 'error' => _t('_sys_agents_form_field_err_enter'),
  209. ],
  210. 'db' => [
  211. 'pass' => 'Xss',
  212. ],
  213. ],
  214. ];
  215. if(!empty($aProvider['options']) && is_array($aProvider['options']))
  216. foreach($aProvider['options'] as $aOption) {
  217. if($aOption['type'] == 'value' && empty($aOption['value']))
  218. continue;
  219. $aInputsAdd[$aOption['name']] = [
  220. 'type' => $aOption['type'],
  221. 'name' => $aOption['name'],
  222. 'caption' => _t($aOption['title']),
  223. 'info' => _t($aOption['description']),
  224. 'value' => isset($aOption['value']) ? $aOption['value'] : '',
  225. 'checker' => !empty($aOption['check_type']) ? [
  226. 'func' => $aOption['check_type'],
  227. 'params' => !empty($aOption['check_params']) ? unserialize($aOption['check_params']) : '',
  228. 'error' => _t($aOption['check_error']),
  229. ] : null,
  230. ];
  231. }
  232. $aForm['inputs'] = bx_array_insert_before($aInputsAdd, $aForm['inputs'], 'controls');
  233. }
  234. return $aForm;
  235. }
  236. protected function _getProviderName($sName)
  237. {
  238. return uriGenerate($sName, 'sys_agents_providers', 'name', ['lowercase' => false]);
  239. }
  240. }
  241. /** @} */