BxBaseStudioAgentsProviders.php 11 KB

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