BxBaseStudioAgentsModels.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 BxDolStudioAgentsGrid
  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. $oForm->initChecker();
  49. if($oForm->isSubmittedAndValid()) {
  50. $aValsToAdd = ['changed' => time()];
  51. if($oForm->update($iId, $aValsToAdd) !== false)
  52. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  53. else
  54. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  55. return echoJson($aRes);
  56. }
  57. $sFormId = $oForm->getId();
  58. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_models_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  59. 'form_id' => $sFormId,
  60. 'form' => $oForm->getCode(true),
  61. 'object' => $this->_sObject,
  62. 'action' => $sAction
  63. ]));
  64. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  65. }
  66. protected function _getFormEdit($sAction, $aModel = [])
  67. {
  68. $aForm = $this->_getForm($sAction, $aModel);
  69. $aForm['form_attrs']['action'] .= '&id=' . $aModel['id'];
  70. return $aForm;
  71. }
  72. protected function _getForm($sAction, $aModel = [])
  73. {
  74. $sJsObject = $this->getPageJsObject();
  75. $sParams = $aModel['params'];
  76. if (!empty($aModel['params_user']))
  77. $sParams = $aModel['params_user'];
  78. $s = json_decode($sParams, true);
  79. $sParams = json_encode($s, JSON_PRETTY_PRINT);
  80. $oParsedown = new Parsedown();
  81. $oParsedown->setSafeMode(false);
  82. $sDocs = $oParsedown->text($aModel['docs']);
  83. $aForm = array(
  84. 'form_attrs' => array(
  85. 'id' => 'bx_std_agents_models_' . $sAction,
  86. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_models&a=' . $sAction,
  87. 'method' => 'post',
  88. ),
  89. 'params' => array (
  90. 'db' => array(
  91. 'table' => 'sys_agents_models',
  92. 'key' => 'id',
  93. 'submit_name' => 'do_submit',
  94. ),
  95. ),
  96. 'inputs' => array(
  97. 'docs' => [
  98. 'type' => 'custom',
  99. 'name' => 'docs',
  100. 'caption' => '',
  101. 'content' => $sDocs,
  102. ],
  103. 'title' => [
  104. 'type' => 'text',
  105. 'name' => 'title',
  106. 'required' => '1',
  107. 'caption' => _t('_sys_agents_models_field_title'),
  108. 'info' => _t('_sys_agents_models_field_title_info'),
  109. 'value' => isset($aModel['title']) ? $aModel['title'] : '',
  110. 'required' => '1',
  111. 'checker' => [
  112. 'func' => 'Avail',
  113. 'params' => [],
  114. 'error' => _t('_sys_agents_form_field_err_enter'),
  115. ],
  116. 'db' => [
  117. 'pass' => 'Xss',
  118. ]
  119. ],
  120. 'model' => [
  121. 'type' => 'text',
  122. 'name' => 'model',
  123. 'caption' => _t('_sys_agents_models_field_model'),
  124. 'info' => _t('_sys_agents_models_field_model_info'),
  125. 'value' => isset($aModel['model']) ? $aModel['model'] : '',
  126. 'db' => [
  127. 'pass' => 'Xss',
  128. ]
  129. ],
  130. 'key' => [
  131. 'type' => 'text',
  132. 'name' => 'key',
  133. 'caption' => _t('_sys_agents_models_field_key'),
  134. 'info' => _t('_sys_agents_models_field_key_info'),
  135. 'value' => isset($aModel['key']) ? $aModel['key'] : '',
  136. 'db' => [
  137. 'pass' => 'Xss',
  138. ]
  139. ],
  140. 'params_user' => [
  141. 'type' => 'textarea',
  142. 'name' => 'params_user',
  143. 'caption' => _t('_sys_agents_models_field_params'),
  144. 'info' => _t('_sys_agents_models_field_params_info'),
  145. 'value' => $sParams,
  146. 'checker' => [
  147. 'func' => 'Json',
  148. 'params' => ['allow_empty' => true],
  149. 'error' => _t('_sys_agents_json_field_err'),
  150. ],
  151. 'db' => [
  152. 'pass' => 'All'
  153. ]
  154. ],
  155. 'submit' => $this->_getFormControls(),
  156. ),
  157. );
  158. return $aForm;
  159. }
  160. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  161. {
  162. if ($sType == 'single' && $aRow['duplicate'] == 0)
  163. return '';
  164. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  165. }
  166. protected function _delete ($mixedId)
  167. {
  168. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedId]);
  169. if (empty($aModel) || $aModel['duplicate'] == 0)
  170. return false;
  171. return parent::_delete($mixedId);
  172. }
  173. protected function _getCellDesign($sKey, $aField, $aRow)
  174. {
  175. if ($sKey == 'icon')
  176. $aField['display'] = 'Raw';
  177. return parent::_getCellDesign($sKey, $aField, $aRow);
  178. }
  179. protected function _getCellIcon($mixedValue, $sKey, $aField, $aRow)
  180. {
  181. if (strpos($mixedValue, '.svg') !== false)
  182. $mixedValue = $this->_oTemplate->getIconContent($mixedValue);
  183. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  184. }
  185. }
  186. /** @} */