BxBaseStudioAgentsModels.php 6.4 KB

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