BxDolStudioAgentsAutomatorsCmts.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. $aParams = [];
  63. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT)
  64. $aParams['alert'] = new BxDolAlerts('test', 'test', 0);
  65. if(($sResult = $this->_oAI->emulCode($aAutomator, $aParams)) != '')
  66. return echoJson(['msg' => _t('_sys_agents_automators_txt_eval_response') . $sResult]);
  67. if(!$this->_oQueryAgents->updateAutomators(['code' => $aCmt['cmt_text'], 'status' => 'ready'], ['id' => (int)$this->getId()]))
  68. return echoJson([]);
  69. return echoJson(['redirect' => $this->_sUrlPageAgents]);
  70. }
  71. public function getPageJsObject()
  72. {
  73. return 'oBxDolStudioPageAgents';
  74. }
  75. public function getCommentsBlock($aBp = [], $aDp = [])
  76. {
  77. $aComments = parent::getCommentsBlock($aBp, ['in_designbox' => false]);
  78. if(empty($aComments['content']))
  79. return MsgBox(_t('_error occured'));
  80. $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id', 'id' => (int)$this->getId()]);
  81. if(empty($aAutomator) || !is_array($aAutomator))
  82. return MsgBox(_t('_error occured'));
  83. $aTmplVarsEvent = [];
  84. $bTmplVarsEvent = $aAutomator['type'] == 'event';
  85. if($bTmplVarsEvent)
  86. $aTmplVarsEvent = [
  87. 'unit' => $aAutomator['alert_unit'],
  88. 'action' => $aAutomator['alert_action']
  89. ];
  90. $aTmplVarsScheduler = [];
  91. $bTmplVarsScheduler = $aAutomator['type'] == 'scheduler';
  92. if($bTmplVarsScheduler) {
  93. $aParams = [];
  94. if(!empty($aAutomator['params']))
  95. $aParams = json_decode($aAutomator['params'], true);
  96. $aTmplVarsScheduler = [
  97. 'time' => $aParams['scheduler_time'],
  98. ];
  99. }
  100. $sAutomator = $this->_oTemplate->parseHtmlByName('agents_automator_info.html', [
  101. 'type' => _t('_sys_agents_automators_field_type_' . $aAutomator['type']),
  102. 'bx_if:show_event' => [
  103. 'condition' => $bTmplVarsEvent,
  104. 'content' => $aTmplVarsEvent
  105. ],
  106. 'bx_if:show_scheduler' => [
  107. 'condition' => $bTmplVarsScheduler,
  108. 'content' => $aTmplVarsScheduler
  109. ],
  110. 'code' => $aAutomator['code']
  111. ]);
  112. return [
  113. $sAutomator,
  114. $aComments['content']
  115. ];
  116. }
  117. public function getFormBoxPost($aBp = [], $aDp = [])
  118. {
  119. $aComments = $this->_oQuery->getCommentsBy(['type' => 'latest', 'object_id' => (int)$this->getId(), 'start' => 0, 'per_page' => 1]);
  120. if(!empty($aComments) && is_array($aComments)) {
  121. $aLast = current($aComments);
  122. if($aLast['cmt_author_id'] != $this->_iProfileIdAi)
  123. return '';
  124. }
  125. return parent::getFormBoxPost($aBp, $aDp);
  126. }
  127. public function isAttachImageEnabled()
  128. {
  129. return false;
  130. }
  131. public function addAuto($aValues)
  132. {
  133. $this->_bAuto = true;
  134. $mixedResult = $this->add($aValues);
  135. $this->_bAuto = false;
  136. return $mixedResult;
  137. }
  138. public function onPostAfter($iCmtId, $aDp = [])
  139. {
  140. $mixedResult = parent::onPostAfter($iCmtId, $aDp);
  141. if($this->_bAuto || $mixedResult === false)
  142. return $mixedResult;
  143. $iObjId = (int)$this->getId();
  144. $aAutomator = $this->_oAI->getAutomator($iObjId, true);
  145. $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
  146. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT && !empty($aAutomator['params']['trigger']))
  147. $aComments[0]['cmt_text'] .= $aAutomator['params']['trigger'];
  148. $aMessages = [];
  149. foreach($aComments as $aComment)
  150. $aMessages[] = [
  151. 'ai' => (int)$aComment['cmt_author_id'] == $this->_iProfileIdAi,
  152. 'content' => $aComment['cmt_text']
  153. ];
  154. $oAIModel = $this->_oAI->getModelObject($aAutomator['model_id']);
  155. if(($sResponse = $oAIModel->getResponse($aAutomator['type'], $aMessages, $aAutomator['params'])) !== false) {
  156. $mixedResultAuto = $this->addAuto([
  157. 'cmt_author_id' => $this->_iProfileIdAi,
  158. 'cmt_parent_id' => 0,
  159. 'cmt_text' => $sResponse
  160. ]);
  161. if($mixedResultAuto !== false)
  162. $mixedResult['id'] .= ',' . $mixedResultAuto['id'];
  163. }
  164. return $mixedResult;
  165. }
  166. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  167. {
  168. if((int)$aCmt['cmt_author_id'] != $this->_iProfileIdAi)
  169. return parent::_getActionsBox($aCmt, $aBp, array_merge($aDp, ['view_only' => true]));
  170. return $this->_oTemplate->parseLink('javascript:void(0)', _t('_sys_agents_automators_btn_approve'), [
  171. 'class' => 'bx-btn bx-btn-small bx-btn-primary',
  172. 'onclick' => $this->getPageJsObject() . '.approveCode(this, ' . $aCmt['cmt_id'] . ')',
  173. ]);
  174. }
  175. protected function _getCountersBox(&$aCmt, $aBp = [], $aDp = [])
  176. {
  177. return '';
  178. }
  179. protected function _getFormBox($sType, $aBp, $aDp)
  180. {
  181. return parent::_getFormBox($sType, $aBp, array_merge($aDp, ['min_post_form' => false]));
  182. }
  183. protected function _getTmplVarsText($aCmt)
  184. {
  185. $aResult = parent::_getTmplVarsText($aCmt);
  186. if((int)$aCmt['cmt_author_id'] == $this->_iProfileIdAi)
  187. $aResult['text'] = '<pre>' . $aResult['text'] . '</pre>';
  188. return $aResult;
  189. }
  190. protected function _getForm($sAction, $iId, $aDp = [])
  191. {
  192. $aResult = parent::_getForm($sAction, $iId, $aDp);
  193. $aResult->aInputs['cmt_text']['db']['pass'] = 'xss';
  194. return $aResult;
  195. }
  196. protected function _prepareTextForOutput($s, $iCmtId = 0)
  197. {
  198. return nl2br(parent::_prepareTextForOutput($s, $iCmtId));
  199. }
  200. }
  201. /** @} */