BxBaseStudioAgentsModels.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 BxBaseStudioAgentsModels extends BxTemplStudioGridAgents
  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=models';
  17. }
  18. public function getPageJsObject()
  19. {
  20. return 'oBxDolStudioPageAgents';
  21. }
  22. public function performActionDuplicate()
  23. {
  24. $iId = $this->_getId();
  25. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
  26. if (!empty($aModel)) {
  27. unset($aModel['id']);
  28. $aModel['title'] .= ' (Copy)';
  29. $aModel['duplicate'] = 1;
  30. $aModel['changed'] = time();
  31. $aModel['active'] = 0;
  32. $iNewId = $this->_oDb->insertModel($aModel);
  33. if ($iNewId) {
  34. $aRes = ['grid' => $this->getCode(false), 'blink' => $iNewId];
  35. return echoJson($aRes);
  36. }
  37. }
  38. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  39. return echoJson($aRes);
  40. }
  41. public function performActionEdit()
  42. {
  43. $sAction = 'edit';
  44. $iId = $this->_getId();
  45. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
  46. $aForm = $this->_getFormEdit($sAction, $aModel);
  47. $oForm = new BxTemplFormView($aForm);
  48. if ($oForm->isSubmitted() && $this->_isUnaProxyModel($aModel)) {
  49. BxDolForm::setSubmittedValue('model', $aModel['model'], $oForm->aFormAttrs['method']);
  50. BxDolForm::setSubmittedValue('key', $aModel['key'], $oForm->aFormAttrs['method']);
  51. }
  52. $oForm->initChecker();
  53. if($oForm->isSubmittedAndValid()) {
  54. $aValsToAdd = ['changed' => time()];
  55. if($oForm->update($iId, $aValsToAdd) !== false)
  56. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  57. else
  58. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  59. return echoJson($aRes);
  60. }
  61. $sFormId = $oForm->getId();
  62. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_models_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_agents_form.html', [
  63. 'form_id' => $sFormId,
  64. 'form' => $oForm->getCode(true),
  65. 'object' => $this->_sObject,
  66. 'action' => $sAction
  67. ]));
  68. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  69. }
  70. protected function _getFormEdit($sAction, $aModel = [])
  71. {
  72. $aForm = $this->_getForm($sAction, $aModel);
  73. $aForm['form_attrs']['action'] .= '&id=' . $aModel['id'];
  74. return $aForm;
  75. }
  76. protected function _getForm($sAction, $aModel = [])
  77. {
  78. $sJsObject = $this->getPageJsObject();
  79. $sParams = $aModel['params'];
  80. if (!empty($aModel['params_user']))
  81. $sParams = $aModel['params_user'];
  82. $s = json_decode($sParams, true);
  83. $sParams = json_encode($s, JSON_PRETTY_PRINT);
  84. $oParsedown = new Parsedown();
  85. $oParsedown->setSafeMode(false);
  86. $sDocs = $oParsedown->text($aModel['docs']);
  87. $aForm = array(
  88. 'form_attrs' => array(
  89. 'id' => 'bx_std_agents_models_' . $sAction,
  90. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_models&a=' . $sAction,
  91. 'method' => 'post',
  92. ),
  93. 'params' => array (
  94. 'db' => array(
  95. 'table' => 'sys_agents_models',
  96. 'key' => 'id',
  97. 'submit_name' => 'do_submit',
  98. ),
  99. ),
  100. 'inputs' => array(
  101. 'docs' => [
  102. 'type' => 'custom',
  103. 'name' => 'docs',
  104. 'caption' => '',
  105. 'content' => $sDocs,
  106. ],
  107. 'title' => [
  108. 'type' => 'text',
  109. 'name' => 'title',
  110. 'required' => '1',
  111. 'caption' => _t('_sys_agents_models_field_title'),
  112. 'info' => _t('_sys_agents_models_field_title_info'),
  113. 'value' => isset($aModel['title']) ? $aModel['title'] : '',
  114. 'required' => '1',
  115. 'checker' => [
  116. 'func' => 'Avail',
  117. 'params' => [],
  118. 'error' => _t('_sys_agents_form_field_err_enter'),
  119. ],
  120. 'db' => [
  121. 'pass' => 'Xss',
  122. ]
  123. ],
  124. 'model' => [
  125. 'type' => 'text',
  126. 'name' => 'model',
  127. 'caption' => _t('_sys_agents_models_field_model'),
  128. 'info' => _t('_sys_agents_models_field_model_info'),
  129. 'value' => isset($aModel['model']) ? $aModel['model'] : '',
  130. 'db' => [
  131. 'pass' => 'Xss',
  132. ]
  133. ],
  134. 'key' => [
  135. 'type' => 'text',
  136. 'name' => 'key',
  137. 'caption' => _t('_sys_agents_models_field_key'),
  138. 'info' => _t('_sys_agents_models_field_key_info'),
  139. 'value' => isset($aModel['key']) ? $aModel['key'] : '',
  140. 'secret' => 1,
  141. 'db' => [
  142. 'pass' => 'Xss',
  143. ]
  144. ],
  145. 'params_user' => [
  146. 'type' => 'textarea',
  147. 'name' => 'params_user',
  148. 'caption' => _t('_sys_agents_models_field_params'),
  149. 'info' => _t('_sys_agents_models_field_params_info'),
  150. 'value' => $sParams,
  151. 'checker' => [
  152. 'func' => 'Json',
  153. 'params' => ['allow_empty' => true],
  154. 'error' => _t('_sys_agents_json_field_err'),
  155. ],
  156. 'db' => [
  157. 'pass' => 'All'
  158. ]
  159. ],
  160. 'submit' => $this->_getFormControls(),
  161. ),
  162. );
  163. if ($sAction === 'edit' && $this->_isUnaProxyModel($aModel)) {
  164. $aForm['inputs']['model']['attrs']['readonly'] = 'readonly';
  165. $aForm['inputs']['key']['attrs']['readonly'] = 'readonly';
  166. }
  167. return $aForm;
  168. }
  169. protected function _isUnaProxyModel($aModel)
  170. {
  171. return is_array($aModel) && ($aModel['type'] ?? '') === 'una-proxy';
  172. }
  173. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  174. {
  175. if ($this->_isUnaProxyModel($aRow))
  176. return '';
  177. if ($sType == 'single' && $aRow['duplicate'] == 0)
  178. return '';
  179. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  180. }
  181. protected function _delete ($mixedId)
  182. {
  183. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedId]);
  184. if (empty($aModel) || $this->_isUnaProxyModel($aModel) || $aModel['duplicate'] == 0)
  185. return false;
  186. return parent::_delete($mixedId);
  187. }
  188. protected function _getCellDesign($sKey, $aField, $aRow)
  189. {
  190. if ($sKey == 'icon')
  191. $aField['display'] = 'Raw';
  192. return parent::_getCellDesign($sKey, $aField, $aRow);
  193. }
  194. protected function _getCellIcon($mixedValue, $sKey, $aField, $aRow)
  195. {
  196. if (strpos($mixedValue, '.svg') !== false)
  197. $mixedValue = $this->_oTemplate->getIconContent($mixedValue);
  198. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  199. }
  200. }
  201. /** @} */