1
0

BxBaseReport.php 20 KB

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