BxBaseReport.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolReport
  11. */
  12. class BxBaseReport extends BxDolReport
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsObjClass;
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. protected $_aElementDefaults;
  20. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  21. {
  22. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  23. $this->_bCssJsAdded = false;
  24. $this->_sJsObjClass = 'BxDolReport';
  25. $this->_sJsObjName = 'oReport' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  26. $this->_sStylePrefix = 'bx-report';
  27. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  28. $this->_aHtmlIds = array(
  29. 'main' => 'bx-report-' . $sHtmlId,
  30. 'counter' => 'bx-report-counter-' . $sHtmlId,
  31. 'do_link' => 'bx-report-do-link-' . $sHtmlId,
  32. 'do_popup' => 'bx-report-do-popup-' . $sHtmlId,
  33. 'do_form' => 'bx-report-do-form-' . $sHtmlId,
  34. 'by_popup' => 'bx-report-by-popup-' . $sHtmlId
  35. );
  36. $this->_aElementDefaults = array(
  37. 'show_do_report_as_button' => false,
  38. 'show_do_report_as_button_small' => false,
  39. 'show_do_report_icon' => true,
  40. 'show_do_report_label' => false,
  41. 'show_counter' => true
  42. );
  43. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('report_element_block.html');
  44. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('report_element_inline.html');
  45. $this->_sTmplContentDoAction = $this->_oTemplate->getHtml('report_do_report.html');
  46. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('report_do_report_label.html');
  47. }
  48. public function getJsObjectName()
  49. {
  50. return $this->_sJsObjName;
  51. }
  52. public function getJsScript($bDynamicMode = false)
  53. {
  54. $aParams = array(
  55. 'sObjName' => $this->_sJsObjName,
  56. 'sSystem' => $this->getSystemName(),
  57. 'iAuthorId' => $this->_getAuthorId(),
  58. 'iObjId' => $this->getId(),
  59. 'sRootUrl' => BX_DOL_URL_ROOT,
  60. 'sStylePrefix' => $this->_sStylePrefix,
  61. 'aHtmlIds' => $this->_aHtmlIds
  62. );
  63. $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  64. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  65. }
  66. public function getJsClick()
  67. {
  68. return $this->getJsObjectName() . '.report(this)';
  69. }
  70. public function getCounter($aParams = array())
  71. {
  72. $sJsObject = $this->getJsObjectName();
  73. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  74. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  75. $aReport = $this->_oQuery->getReport($this->getId());
  76. $sClass = $this->_sStylePrefix . '-counter';
  77. if($bShowDoReportAsButtonSmall)
  78. $sClass .= ' bx-btn-small-height';
  79. if($bShowDoReportAsButton)
  80. $sClass .= ' bx-btn-height';
  81. return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aReport['count'] > 0 ? $this->_getCounterLabel($aReport['count']) : '', array(
  82. 'id' => $this->_aHtmlIds['counter'],
  83. 'class' => $sClass,
  84. 'title' => _t('_report_do_report_by'),
  85. 'onclick' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'
  86. ));
  87. }
  88. public function getElementBlock($aParams = array())
  89. {
  90. $aParams['usage'] = BX_DOL_REPORT_USAGE_BLOCK;
  91. return $this->getElement($aParams);
  92. }
  93. public function getElementInline($aParams = array())
  94. {
  95. $aParams['usage'] = BX_DOL_REPORT_USAGE_INLINE;
  96. return $this->getElement($aParams);
  97. }
  98. public function getElement($aParams = array())
  99. {
  100. $aParams = array_merge($this->_aElementDefaults, $aParams);
  101. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  102. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  103. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  104. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedReportView();
  105. $iObjectId = $this->getId();
  106. $iAuthorId = $this->_getAuthorId();
  107. $aReport = $this->_oQuery->getReport($iObjectId);
  108. if(!$this->isAllowedReport() && (!$this->isAllowedReportView() || (int)$aReport['count'] == 0))
  109. return '';
  110. $aParams['is_reported'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  111. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_REPORT_USAGE_DEFAULT)}();
  112. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  113. 'style_prefix' => $this->_sStylePrefix,
  114. 'html_id' => $this->_aHtmlIds['main'],
  115. 'class' => $this->_sStylePrefix . ($bShowDoReportAsButton ? '-button' : '') . ($bShowDoReportAsButtonSmall ? '-button-small' : ''),
  116. 'count' => $aReport['count'],
  117. 'do_report' => $this->_getDoReport($aParams),
  118. 'bx_if:show_counter' => array(
  119. 'condition' => $bShowCounter,
  120. 'content' => array(
  121. 'style_prefix' => $this->_sStylePrefix,
  122. 'bx_if:show_hidden' => array(
  123. 'condition' => (int)$aReport['count'] == 0,
  124. 'content' => array()
  125. ),
  126. 'counter' => $this->getCounter($aParams)
  127. )
  128. ),
  129. 'script' => $this->getJsScript($bDynamicMode)
  130. ));
  131. }
  132. protected function _getDoReport($aParams = array())
  133. {
  134. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  135. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  136. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  137. $bDisabled = !$this->isAllowedReport() || $bReported;
  138. $sClass = '';
  139. if($bShowDoReportAsButton)
  140. $sClass = 'bx-btn';
  141. else if ($bShowDoReportAsButtonSmall)
  142. $sClass = 'bx-btn bx-btn-small';
  143. if($bDisabled)
  144. $sClass .= $bShowDoReportAsButton || $bShowDoReportAsButtonSmall ? ' bx-btn-disabled' : 'bx-report-disabled';
  145. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoAction(), array(
  146. 'style_prefix' => $this->_sStylePrefix,
  147. 'html_id' => $this->_aHtmlIds['do_link'],
  148. 'class' => $sClass,
  149. 'title' => _t('_report_do_report'),
  150. 'bx_if:show_onclick' => array(
  151. 'condition' => !$bDisabled,
  152. 'content' => array(
  153. 'js_object' => $this->getJsObjectName()
  154. )
  155. ),
  156. 'do_report' => $this->_getLabelDoReport($aParams),
  157. ));
  158. }
  159. protected function _getCounterLabel($iCount)
  160. {
  161. return _t('_report_counter', $iCount);
  162. }
  163. protected function _getLabelDoReport($aParams = array())
  164. {
  165. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  166. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  167. 'style_prefix' => $this->_sStylePrefix,
  168. 'bx_if:show_icon' => array(
  169. 'condition' => isset($aParams['show_do_report_icon']) && $aParams['show_do_report_icon'] == true,
  170. 'content' => array(
  171. 'style_prefix' => $this->_sStylePrefix,
  172. 'name' => $this->_getIconDoReport($bReported)
  173. )
  174. ),
  175. 'bx_if:show_text' => array(
  176. 'condition' => isset($aParams['show_do_report_label']) && $aParams['show_do_report_label'] == true,
  177. 'content' => array(
  178. 'style_prefix' => $this->_sStylePrefix,
  179. 'text' => _t($this->_getTitleDoReport($bReported))
  180. )
  181. )
  182. ));
  183. }
  184. protected function _getReport()
  185. {
  186. if (!$this->isEnabled())
  187. return array('code' => 1, 'message' => _t('_report_err_not_enabled'));
  188. if(!$this->isAllowedReport())
  189. return array('code' => 2, 'message' => $this->msgErrAllowedReport());
  190. $oForm = $this->_getFormObject();
  191. $oForm->setId($this->_aHtmlIds['do_form']);
  192. $oForm->setName($this->_aHtmlIds['do_form']);
  193. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  194. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  195. $oForm->aInputs['object_id']['value'] = $this->_iId;
  196. $oForm->aInputs['action']['value'] = 'Report';
  197. $oForm->initChecker();
  198. if($oForm->isSubmittedAndValid()) {
  199. $iObjectId = $oForm->getCleanValue('object_id');
  200. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  201. $iAuthorId = $this->_getAuthorId();
  202. $iAuthorNip = ip2long($this->_getAuthorIp());
  203. if(!$this->isAllowedReport(true))
  204. return array('code' => 2, 'message' => $this->msgErrAllowedReport());
  205. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  206. if($bPerformed)
  207. return array('code' => 4, 'message' => _t('_report_err_duplicate_report'));
  208. $sType = $oForm->getCleanValue('type');
  209. if(!in_array($sType, $this->_aTypes))
  210. return array('code' => 5, 'message' => _t('_report_err_wrong_type'));
  211. $sText = $oForm->getCleanValue('text');
  212. $sText = bx_process_input($sText, BX_DATA_TEXT_MULTILINE);
  213. $oForm->setSubmittedValue('text', $sText, $oForm->aFormAttrs['method']);
  214. $iId = (int)$oForm->insert(array('author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'date' => time()));
  215. if($iId != 0 && $this->_oQuery->putReport($iObjectId)) {
  216. $this->_trigger();
  217. if(!empty($this->_sObjectCmts) && ($oCmts = BxDolCmts::getObjectInstance($this->_sObjectCmts, $this->getId()))) {
  218. $sCmtText = '_report_comment';
  219. if(!empty($sText))
  220. $sCmtText = '_report_comment_with_note';
  221. $aTypes = BxDolForm::getDataItems('sys_report_types');
  222. $oCmts->add(array(
  223. 'cmt_author_id' => $iAuthorId,
  224. 'cmt_parent_id' => 0,
  225. 'cmt_text' => _t($sCmtText, $aTypes[$sType], $sText)
  226. ));
  227. }
  228. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', array(
  229. 'report_type' => $sType,
  230. 'report_text' => $sText,
  231. 'report_url' => $this->getBaseUrl(),
  232. ));
  233. if($aTemplate)
  234. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  235. bx_alert($this->_sSystem, 'doReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  236. bx_alert('report', 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  237. $mixedParams = $this->_prepareAuditParams($iObjectId, array('type' => $sType, 'text' => $sText));
  238. if($mixedParams){
  239. $sActionName = $mixedParams['action_name'];
  240. $iAuditObjectId = $mixedParams['object_id'];
  241. unset($mixedParams['action_name']);
  242. unset($mixedParams['object_id']);
  243. bx_audit($iAuditObjectId, $this->_sSystem, $sActionName, $mixedParams);
  244. }
  245. $aReport = $this->_oQuery->getReport($iObjectId);
  246. $iBlockContentAfter = (int)getParam('sys_security_block_content_after_n_reports');
  247. if ($iBlockContentAfter > 0 && $aReport['count'] >= $iBlockContentAfter){
  248. $oModule = BxDolModule::getInstance($this->_sSystem);
  249. if(!$oModule)
  250. return;
  251. $oModule = $oModule->_oDb->updateStatusAdmin($iObjectId, false);
  252. }
  253. return array(
  254. 'eval' => $this->getJsObjectName() . '.onReport(oData, oElement)',
  255. 'code' => 0,
  256. 'id' => $iId,
  257. 'count' => $aReport['count'],
  258. 'countf' => (int)$aReport['count'] > 0 ? $this->_getCounterLabel($aReport['count']) : '',
  259. 'label_icon' => $this->_getIconDoReport(!$bPerformed),
  260. 'label_title' => _t($this->_getTitleDoReport(!$bPerformed)),
  261. 'disabled' => !$bPerformed
  262. );
  263. }
  264. return array('code' => 3, 'message' => _t('_report_err_cannot_perform_action'));
  265. }
  266. $sPopupId = $this->_aHtmlIds['do_popup'];
  267. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('report_do_report_form.html', array(
  268. 'style_prefix' => $this->_sStylePrefix,
  269. 'js_object' => $this->getJsObjectName(),
  270. 'form' => $oForm->getCode(),
  271. 'form_id' => $oForm->id,
  272. )));
  273. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  274. }
  275. protected function _getReportedBy()
  276. {
  277. $aTmplReports = array();
  278. $aTypes = BxDolForm::getDataItems('sys_report_types');
  279. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  280. foreach($aReports as $aReport) {
  281. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  282. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  283. $aTmplReports[] = array(
  284. 'style_prefix' => $this->_sStylePrefix,
  285. 'user_unit' => $sUserUnit,
  286. 'type' => $aTypes[$aReport['type']],
  287. 'bx_if:show_text' => array(
  288. 'condition' => strlen($sText) > 0,
  289. 'content' => array(
  290. 'text' => $sText
  291. )
  292. )
  293. );
  294. }
  295. if(empty($aTmplReports))
  296. $aTmplReports = MsgBox(_t('_Empty'));
  297. return $this->_oTemplate->parseHtmlByName('report_by_list.html', array(
  298. 'style_prefix' => $this->_sStylePrefix,
  299. 'bx_repeat:list' => $aTmplReports
  300. ));
  301. }
  302. private function _prepareAuditParams($iObjectId, $aData)
  303. {
  304. $sModule = $this->_sSystem;
  305. $sKeyObjectContentInfo = 'OBJECT_CONTENT_INFO';
  306. $sActionName = '_sys_audit_action_report';
  307. if ($this->_sSystem == 'sys_cmts'){
  308. $aCommentData = BxDolCmtsQuery::getCommentByUniq($iObjectId);
  309. $oComment = BxDolCmts::getObjectInstance($aCommentData['system_name'], $aCommentData['cmt_object_id']);
  310. $aComment = $oComment->getCommentSimple($aCommentData['cmt_id']);
  311. $aSystem = $oComment->getSystemInfo();
  312. $sModule = $aSystem['module'];
  313. $aData['comment_id'] = $aCommentData['cmt_id'];
  314. $aData['comment_author_id'] = $aComment['cmt_author_id'];
  315. $aData['comment_text'] = $aComment['cmt_text'];
  316. $iObjectId = $aCommentData['cmt_object_id'];
  317. $sKeyObjectContentInfo = 'OBJECT_CMTS_CONTENT_INFO';
  318. $sActionName = '_sys_audit_action_report_comment';
  319. }
  320. $oModule = BxDolModule::getInstance($sModule);
  321. if ($oModule){
  322. $CNF = $oModule->_oConfig->CNF;
  323. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iObjectId))) : array();
  324. $AuditParams = array(
  325. 'content_title' => isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  326. 'content_info_object' => isset($CNF[$sKeyObjectContentInfo]) ? $CNF[$sKeyObjectContentInfo] : '',
  327. 'data' => $aData,
  328. 'action_name' => $sActionName,
  329. 'object_id' => $iObjectId
  330. );
  331. return $AuditParams;
  332. }
  333. return false;
  334. }
  335. }
  336. /** @} */