BxDolStudioAgentsAutomatorsCmts.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
  10. {
  11. protected $_oQueryAgents;
  12. protected $_sUrlPageAgents;
  13. protected $_iProfileIdAi;
  14. protected $_bAuto;
  15. protected $_oAI;
  16. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  17. {
  18. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  19. if ($oTemplate)
  20. $this->_oTemplate = $oTemplate;
  21. else
  22. $this->_oTemplate = BxDolStudioTemplate::getInstance();
  23. $this->_sTmplNameItemContent = 'agents_comment_content.html';
  24. $this->_bLiveUpdates = false;
  25. $this->_oQueryAgents = new BxDolStudioAgentsQuery();
  26. $this->_sUrlPageAgents = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
  27. $this->_iProfileIdAi = BxDolAI::getInstance()->getProfileId();
  28. $this->_bAuto = false;
  29. $this->_oAI = BxDolAI::getInstance();
  30. }
  31. public function actionGetCmt ()
  32. {
  33. if(!$this->isEnabled())
  34. return echoJson([]);
  35. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  36. return echoJson([]);
  37. $mixedCmtId = bx_process_input(bx_get('Cmt'));
  38. $sCmtBrowse = ($sCmtBrowse = bx_get('CmtBrowse')) !== false ? bx_process_input($sCmtBrowse, BX_DATA_TEXT) : '';
  39. $sCmtDisplay = ($sCmtDisplay = bx_get('CmtDisplay')) !== false ? bx_process_input($sCmtDisplay, BX_DATA_TEXT) : '';
  40. $aCmtIds = strpos($mixedCmtId, ',') !== false ? explode(',', $mixedCmtId) : [$mixedCmtId];
  41. $sContent = '';
  42. foreach($aCmtIds as $iCmtId)
  43. $sContent .= $this->getComment((int)$iCmtId, ['type' => $sCmtBrowse], ['type' => $sCmtDisplay, 'dynamic_mode' => true]);
  44. $aCmt = $this->getCommentRow((int)reset($aCmtIds));
  45. echoJson([
  46. 'parent_id' => $aCmt['cmt_parent_id'],
  47. 'vparent_id' => $aCmt['cmt_parent_id'],
  48. 'content' => $sContent
  49. ]);
  50. }
  51. public function actionApproveCode()
  52. {
  53. if(!$this->isEnabled())
  54. return echoJson([]);
  55. $iCmt = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  56. $aCmt = $this->getCommentRow($iCmt);
  57. if(empty($aCmt) || !is_array($aCmt))
  58. return echoJson([]);
  59. $iObjId = (int)$this->getId();
  60. $aAutomator = $this->_oAI->getAutomator($iObjId, true);
  61. $aAutomator['code'] = $aCmt['cmt_text'];
  62. /*
  63. * TODO: Improve code evaluation.
  64. *
  65. $oAlert = new BxDolAlerts('test', 'test', -1, -1, []);
  66. $sRv = $this->_oAI->evalCode($aAutomator, false, ['alert' => $oAlert]);
  67. if ($sRv != ''){
  68. return echoJson(['msg' => 'Error in code: ' . $sRv]);
  69. }
  70. */
  71. if(!$this->_oQueryAgents->updateAutomators(['code' => $aCmt['cmt_text'], 'status' => 'ready'], ['id' => (int)$this->getId()]))
  72. return echoJson([]);
  73. return echoJson(['redirect' => $this->_sUrlPageAgents]);
  74. }
  75. public function getPageJsObject()
  76. {
  77. return 'oBxDolStudioPageAgents';
  78. }
  79. public function getCommentsBlock($aBp = [], $aDp = [])
  80. {
  81. $aComments = parent::getCommentsBlock($aBp, ['in_designbox' => false]);
  82. if(empty($aComments['content']))
  83. return MsgBox(_t('_error occured'));
  84. $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id', 'id' => (int)$this->getId()]);
  85. if(empty($aAutomator) || !is_array($aAutomator))
  86. return MsgBox(_t('_error occured'));
  87. $aTmplVarsEvent = [];
  88. $bTmplVarsEvent = $aAutomator['type'] == 'event';
  89. if($bTmplVarsEvent)
  90. $aTmplVarsEvent = [
  91. 'unit' => $aAutomator['alert_unit'],
  92. 'action' => $aAutomator['alert_action']
  93. ];
  94. $aTmplVarsScheduler = [];
  95. $bTmplVarsScheduler = $aAutomator['type'] == 'scheduler';
  96. if($bTmplVarsScheduler) {
  97. $aParams = [];
  98. if(!empty($aAutomator['params']))
  99. $aParams = json_decode($aAutomator['params'], true);
  100. $aTmplVarsScheduler = [
  101. 'time' => $aParams['scheduler_time'],
  102. ];
  103. }
  104. $sAutomator = $this->_oTemplate->parseHtmlByName('agents_automator_info.html', [
  105. 'type' => _t('_sys_agents_automators_field_type_' . $aAutomator['type']),
  106. 'bx_if:show_event' => [
  107. 'condition' => $bTmplVarsEvent,
  108. 'content' => $aTmplVarsEvent
  109. ],
  110. 'bx_if:show_scheduler' => [
  111. 'condition' => $bTmplVarsScheduler,
  112. 'content' => $aTmplVarsScheduler
  113. ],
  114. 'code' => $aAutomator['code']
  115. ]);
  116. return [
  117. $sAutomator,
  118. $aComments['content']
  119. ];
  120. }
  121. public function getFormBoxPost($aBp = [], $aDp = [])
  122. {
  123. $aComments = $this->_oQuery->getCommentsBy(['type' => 'latest', 'object_id' => (int)$this->getId(), 'start' => 0, 'per_page' => 1]);
  124. if(!empty($aComments) && is_array($aComments)) {
  125. $aLast = current($aComments);
  126. if($aLast['cmt_author_id'] != $this->_iProfileIdAi)
  127. return '';
  128. }
  129. return parent::getFormBoxPost($aBp, $aDp);
  130. }
  131. public function isAttachImageEnabled()
  132. {
  133. return false;
  134. }
  135. public function addAuto($aValues)
  136. {
  137. $this->_bAuto = true;
  138. $mixedResult = $this->add($aValues);
  139. $this->_bAuto = false;
  140. return $mixedResult;
  141. }
  142. public function onPostAfter($iCmtId, $aDp = [])
  143. {
  144. $mixedResult = parent::onPostAfter($iCmtId, $aDp);
  145. if($this->_bAuto || $mixedResult === false)
  146. return $mixedResult;
  147. $iObjId = (int)$this->getId();
  148. $aAutomator = $this->_oAI->getAutomator($iObjId, true);
  149. $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
  150. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT && !empty($aAutomator['params']['trigger']))
  151. $aComments[0]['cmt_text'] .= $aAutomator['params']['trigger'];
  152. $aMessages = [];
  153. foreach($aComments as $aComment)
  154. $aMessages[] = [
  155. 'ai' => (int)$aComment['cmt_author_id'] == $this->_iProfileIdAi,
  156. 'content' => $aComment['cmt_text']
  157. ];
  158. $oAIModel = $this->_oAI->getModelObject($aAutomator['model_id']);
  159. if(($sResponse = $oAIModel->getResponse($aAutomator['type'], $aMessages, $aAutomator['params'])) !== false) {
  160. $mixedResultAuto = $this->addAuto([
  161. 'cmt_author_id' => $this->_iProfileIdAi,
  162. 'cmt_parent_id' => 0,
  163. 'cmt_text' => $sResponse
  164. ]);
  165. if($mixedResultAuto !== false)
  166. $mixedResult['id'] .= ',' . $mixedResultAuto['id'];
  167. }
  168. return $mixedResult;
  169. }
  170. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  171. {
  172. if((int)$aCmt['cmt_author_id'] != $this->_iProfileIdAi)
  173. return parent::_getActionsBox($aCmt, $aBp, array_merge($aDp, ['view_only' => true]));
  174. return $this->_oTemplate->parseLink('javascript:void(0)', _t('_sys_agents_automators_btn_approve'), [
  175. 'class' => 'bx-btn bx-btn-small bx-btn-primary',
  176. 'onclick' => $this->getPageJsObject() . '.approveCode(this, ' . $aCmt['cmt_id'] . ')',
  177. ]);
  178. }
  179. protected function _getCountersBox(&$aCmt, $aBp = [], $aDp = [])
  180. {
  181. return '';
  182. }
  183. protected function _getFormBox($sType, $aBp, $aDp)
  184. {
  185. return parent::_getFormBox($sType, $aBp, array_merge($aDp, ['min_post_form' => false]));
  186. }
  187. protected function _getTmplVarsText($aCmt)
  188. {
  189. $aResult = parent::_getTmplVarsText($aCmt);
  190. if((int)$aCmt['cmt_author_id'] == $this->_iProfileIdAi)
  191. $aResult['text'] = '<pre>' . $aResult['text'] . '</pre>';
  192. return $aResult;
  193. }
  194. protected function _getForm($sAction, $iId)
  195. {
  196. $aResult = parent::_getForm($sAction, $iId);
  197. $aResult->aInputs['cmt_text']['db']['pass'] = 'xss';
  198. return $aResult;
  199. }
  200. protected function _prepareTextForOutput($s, $iCmtId = 0)
  201. {
  202. return nl2br(parent::_prepareTextForOutput($s, $iCmtId));
  203. }
  204. }
  205. /** @} */