BxDolStudioAgentsAutomatorsCmts.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 BxDolStudioAgentsCmts
  10. {
  11. protected $_sUrlPageAgents;
  12. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  13. {
  14. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  15. $this->_sUrlPageAgents = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
  16. }
  17. public function actionApproveCode()
  18. {
  19. if(!$this->isEnabled())
  20. return echoJson([]);
  21. $iCmt = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  22. $aCmt = $this->getCommentRow($iCmt);
  23. if(empty($aCmt) || !is_array($aCmt))
  24. return echoJson([]);
  25. $iObjId = (int)$this->getId();
  26. $aAutomator = $this->_oAI->getAutomator($iObjId, true);
  27. $aAutomator['code'] = $aCmt['cmt_text'];
  28. $aParams = [];
  29. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT)
  30. $aParams['alert'] = new BxDolAlerts('test', 'test', 0);
  31. if(($sResult = $this->_oAI->emulCode($aAutomator, $aParams)) != '')
  32. return echoJson(['msg' => _t('_sys_agents_automators_txt_eval_response') . $sResult]);
  33. if(!$this->_oQueryAgents->updateAutomators(['code' => $aCmt['cmt_text'], 'status' => 'ready'], ['id' => (int)$this->getId()]))
  34. return echoJson([]);
  35. return echoJson(['redirect' => $this->_sUrlPageAgents]);
  36. }
  37. public function getPageJsObject()
  38. {
  39. return 'oBxDolStudioPageAgents';
  40. }
  41. public function getCommentsBlock($aBp = [], $aDp = [])
  42. {
  43. $aComments = parent::getCommentsBlock($aBp, ['in_designbox' => false]);
  44. if(empty($aComments['content']))
  45. return MsgBox(_t('_error occured'));
  46. $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id', 'id' => (int)$this->getId()]);
  47. if(empty($aAutomator) || !is_array($aAutomator))
  48. return MsgBox(_t('_error occured'));
  49. $aTmplVarsEvent = [];
  50. $bTmplVarsEvent = $aAutomator['type'] == 'event';
  51. if($bTmplVarsEvent)
  52. $aTmplVarsEvent = [
  53. 'unit' => $aAutomator['alert_unit'],
  54. 'action' => $aAutomator['alert_action']
  55. ];
  56. $aTmplVarsScheduler = [];
  57. $bTmplVarsScheduler = $aAutomator['type'] == 'scheduler';
  58. if($bTmplVarsScheduler) {
  59. $aParams = [];
  60. if(!empty($aAutomator['params']))
  61. $aParams = json_decode($aAutomator['params'], true);
  62. $aTmplVarsScheduler = [
  63. 'time' => $aParams['scheduler_time'],
  64. ];
  65. }
  66. $sAutomator = $this->_oTemplate->parseHtmlByName('agents_automator_info.html', [
  67. 'type' => _t('_sys_agents_automators_field_type_' . $aAutomator['type']),
  68. 'bx_if:show_event' => [
  69. 'condition' => $bTmplVarsEvent,
  70. 'content' => $aTmplVarsEvent
  71. ],
  72. 'bx_if:show_scheduler' => [
  73. 'condition' => $bTmplVarsScheduler,
  74. 'content' => $aTmplVarsScheduler
  75. ],
  76. 'code' => $aAutomator['code']
  77. ]);
  78. return [
  79. $sAutomator,
  80. $aComments['content']
  81. ];
  82. }
  83. public function getComment($mixedCmt, $aBp = [], $aDp = [])
  84. {
  85. return parent::getComment($mixedCmt, $aBp, array_merge($aDp, ['class_comment' => $this->_sStylePrefix . '-agents']));
  86. }
  87. public function getFormBoxPost($aBp = [], $aDp = [])
  88. {
  89. $aComments = $this->_oQuery->getCommentsBy(['type' => 'latest', 'object_id' => (int)$this->getId(), 'start' => 0, 'per_page' => 1]);
  90. if(!empty($aComments) && is_array($aComments)) {
  91. $aLast = current($aComments);
  92. if($aLast['cmt_author_id'] != $this->_iProfileIdAi)
  93. return '';
  94. }
  95. return parent::getFormBoxPost($aBp, $aDp);
  96. }
  97. public function isAttachImageEnabled()
  98. {
  99. return false;
  100. }
  101. public function addAuto($aValues)
  102. {
  103. $this->_bAuto = true;
  104. $mixedResult = $this->add($aValues);
  105. $this->_bAuto = false;
  106. return $mixedResult;
  107. }
  108. public function onPostAfter($iCmtId, $aDp = [])
  109. {
  110. $mixedResult = parent::onPostAfter($iCmtId, $aDp);
  111. if($this->_bAuto || $mixedResult === false)
  112. return $mixedResult;
  113. $iObjId = (int)$this->getId();
  114. $aAutomator = $this->_oAI->getAutomator($iObjId, true);
  115. $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
  116. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT && !empty($aAutomator['params']['trigger']))
  117. $aComments[0]['cmt_text'] .= $aAutomator['params']['trigger'];
  118. $oMessages = new BxDolAIMessages();
  119. foreach($aComments as $aComment)
  120. $oMessages->add((int)$aComment['cmt_author_id'] == $this->_iProfileIdAi ? 'ai' : 'hb', $aComment['cmt_text']);
  121. $oAIModel = $this->_oAI->getModelObject($aAutomator['model_id']);
  122. if(($sResponse = $oAIModel->getResponse($aAutomator['type'], $oMessages, $aAutomator['params'])) !== false) {
  123. $mixedResultAuto = $this->addAuto([
  124. 'cmt_author_id' => $this->_iProfileIdAi,
  125. 'cmt_parent_id' => 0,
  126. 'cmt_text' => $sResponse
  127. ]);
  128. if($mixedResultAuto !== false)
  129. $mixedResult['id'] .= ',' . $mixedResultAuto['id'];
  130. }
  131. return $mixedResult;
  132. }
  133. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  134. {
  135. if((int)$aCmt['cmt_author_id'] != $this->_iProfileIdAi)
  136. return parent::_getActionsBox($aCmt, $aBp, array_merge($aDp, ['view_only' => true]));
  137. return $this->_oTemplate->parseHtmlByName('agents_comment_actions_automator.html', [
  138. 'js_object' => $this->getPageJsObject(),
  139. 'id' => $aCmt['cmt_id']
  140. ]);
  141. }
  142. protected function _getTmplVarsText($aCmt)
  143. {
  144. $aResult = parent::_getTmplVarsText($aCmt);
  145. if((int)$aCmt['cmt_author_id'] == $this->_iProfileIdAi)
  146. $aResult['text'] = '<pre>' . $aResult['text'] . '</pre>';
  147. return $aResult;
  148. }
  149. protected function _getForm($sAction, $iId, $aDp = [])
  150. {
  151. $aResult = parent::_getForm($sAction, $iId, $aDp);
  152. $aResult->aInputs['cmt_text']['db']['pass'] = 'xss';
  153. return $aResult;
  154. }
  155. protected function _prepareTextForOutput($s, $iCmtId = 0)
  156. {
  157. return nl2br(parent::_prepareTextForOutput($s, $iCmtId));
  158. }
  159. }
  160. /** @} */