BxBaseReport.php 20 KB

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