BxBaseStudioAgentsProviders.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 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. $aProvider = $this->_oDb->getProviderBy(['sample' => 'id', 'id' => $iId]);
  65. if(empty($aProvider) || !is_array($aProvider))
  66. return echoJson([]);
  67. $aProvider['options'] = $this->_oDb->getProviderBy(['sample' => 'options_by_id', 'id' => $iId]);
  68. $aForm = $this->_getFormEdit($sAction, $aProvider);
  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($aProvider['options']) && is_array($aProvider['options']))
  75. foreach($aProvider['options'] as $aOption)
  76. $this->_oDb->updateProviderValue([
  77. 'value' => $oForm->getCleanValue($aOption['name'])
  78. ], [
  79. 'provider_id' => $iId,
  80. 'option_id' => $aOption['id'],
  81. ]);
  82. return echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  83. }
  84. $sFormId = $oForm->getId();
  85. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_providers_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_provider_form.html', [
  86. 'form_id' => $sFormId,
  87. 'form' => $oForm->getCode(true),
  88. 'object' => $this->_sObject,
  89. 'action' => $sAction
  90. ]));
  91. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  92. }
  93. public function performActionInfo()
  94. {
  95. $sAction = 'view';
  96. $iId = $this->_getId();
  97. $aProvider = $this->_oDb->getProviderBy(['sample' => 'id', 'id' => $iId]);
  98. if(empty($aProvider) || !is_array($aProvider))
  99. return echoJson([]);
  100. $aProvider['options'] = $this->_oDb->getProviderBy(['sample' => 'options_by_id', 'id' => $iId]);
  101. $aForm = $this->_getFormView($sAction, $aProvider);
  102. $oForm = new BxTemplFormView($aForm);
  103. $oForm->initChecker($aProvider);
  104. $sFormId = $oForm->getId();
  105. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup_' . $sAction, _t('_sys_agents_providers_popup_view'), $this->_oTemplate->parseHtmlByName('agents_provider_form.html', [
  106. 'form_id' => $oForm->getId(),
  107. 'form' => $oForm->getCode(true),
  108. 'object' => $this->_sObject,
  109. 'action' => $sAction
  110. ]));
  111. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => true, 'removeOnClose' => true]]]);
  112. }
  113. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  114. {
  115. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  116. }
  117. protected function _addJsCss()
  118. {
  119. parent::_addJsCss();
  120. $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_automators.js']);
  121. $oForm = new BxTemplStudioFormView([]);
  122. $oForm->addCssJs();
  123. }
  124. protected function _isCheckboxDisabled($aRow)
  125. {
  126. return false;
  127. }
  128. protected function _getActionsDisabledBehavior($aRow)
  129. {
  130. return false;
  131. }
  132. protected function _delete ($mixedId)
  133. {
  134. $mixedResult = parent::_delete($mixedId);
  135. if($mixedResult)
  136. $this->_oDb->deleteProviderValues(['provider_id' => (int)$mixedId]);
  137. return $mixedResult;
  138. }
  139. protected function _getFormView($sAction, $aProvider = [])
  140. {
  141. $aForm = $this->_getForm($sAction, $aProvider);
  142. $aForm['params']['view_mode'] = 1;
  143. return $aForm;
  144. }
  145. protected function _getFormEdit($sAction, $aProvider = [])
  146. {
  147. $aForm = $this->_getForm($sAction, $aProvider);
  148. $aForm['form_attrs']['action'] .= '&id=' . $aProvider['id'];
  149. return $aForm;
  150. }
  151. protected function _getForm($sAction, $aProvider = [])
  152. {
  153. $bProvider = !empty($aProvider) && is_array($aProvider);
  154. $aForm = [
  155. 'form_attrs' => [
  156. 'id' => 'bx_std_agents_providers_' . $sAction,
  157. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_providers&a=' . $sAction,
  158. 'method' => 'post',
  159. ],
  160. 'params' => [
  161. 'db' => [
  162. 'table' => 'sys_agents_providers',
  163. 'key' => 'id',
  164. 'submit_name' => 'do_submit',
  165. ],
  166. ],
  167. 'inputs' => [
  168. 'controls' => [
  169. 'type' => 'input_set',
  170. 0 => [
  171. 'type' => 'submit',
  172. 'name' => 'do_submit',
  173. 'value' => _t('_sys_submit'),
  174. ],
  175. 1 => [
  176. 'type' => 'reset',
  177. 'name' => 'close',
  178. 'value' => _t('_sys_close'),
  179. 'attrs' => ['class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'],
  180. ],
  181. ],
  182. ],
  183. ];
  184. if($sAction == 'add' && !$bProvider) {
  185. $aForm['inputs'] = bx_array_insert_before([
  186. 'type_id' => [
  187. 'type' => 'select',
  188. 'name' => 'type_id',
  189. 'caption' => _t('_sys_agents_providers_field_type_id'),
  190. 'info' => '',
  191. 'value' => isset($aProvider['type_id']) ? $aProvider['type_id'] : 0,
  192. 'values' => array_map(function($sTitle) {
  193. return _t($sTitle);
  194. }, ['' => '_sys_please_select'] + $this->_oDb->getProviderTypesBy(['sample' => 'all_pairs'])),
  195. 'required' => '1',
  196. 'db' => [
  197. 'pass' => 'Int',
  198. ]
  199. ],
  200. ], $aForm['inputs'], 'controls');
  201. $aForm['inputs']['controls'][0] = array_merge($aForm['inputs']['controls'][0], [
  202. 'name' => 'do_select',
  203. 'value' => _t('_sys_select')
  204. ]);
  205. }
  206. else {
  207. $aInputsAdd = [
  208. 'type_id' => [
  209. 'type' => 'hidden',
  210. 'name' => 'type_id',
  211. 'value' => isset($aProvider['type_id']) ? $aProvider['type_id'] : 0,
  212. 'db' => [
  213. 'pass' => 'Int',
  214. ]
  215. ],
  216. 'title' => [
  217. 'type' => 'text',
  218. 'name' => 'title',
  219. 'caption' => _t('_sys_agents_providers_field_title'),
  220. 'value' => isset($aProvider['title']) ? $aProvider['title'] : '',
  221. 'db' => [
  222. 'pass' => 'Xss',
  223. ],
  224. ],
  225. ];
  226. if(!empty($aProvider['options']) && is_array($aProvider['options']))
  227. foreach($aProvider['options'] as $aOption)
  228. $aInputsAdd[$aOption['name']] = [
  229. 'type' => $aOption['type'],
  230. 'name' => $aOption['name'],
  231. 'caption' => _t($aOption['title']),
  232. 'info' => _t($aOption['description']),
  233. 'value' => isset($aOption['value']) ? $aOption['value'] : '',
  234. 'checker' => !empty($aOption['check_type']) ? [
  235. 'func' => $aOption['check_type'],
  236. 'params' => !empty($aOption['check_params']) ? unserialize($aOption['check_params']) : '',
  237. 'error' => _t($aOption['check_error']),
  238. ] : null,
  239. ];
  240. $aForm['inputs'] = bx_array_insert_before($aInputsAdd, $aForm['inputs'], 'controls');
  241. }
  242. return $aForm;
  243. }
  244. protected function _getId()
  245. {
  246. $aIds = bx_get('ids');
  247. if(!empty($aIds) && is_array($aIds))
  248. return array_shift($aIds);
  249. $iId = (int)bx_get('id');
  250. if(!$iId)
  251. return false;
  252. return $iId;
  253. }
  254. }
  255. /** @} */