1
0

BxBaseReport.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. protected function _getDoReport($aParams = array())
  134. {
  135. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  136. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  137. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  138. $bDisabled = !$this->isAllowedReport() || ($bReported && !$this->isUndo());
  139. $sClass = '';
  140. if($bShowDoReportAsButton)
  141. $sClass = 'bx-btn';
  142. else if ($bShowDoReportAsButtonSmall)
  143. $sClass = 'bx-btn bx-btn-small';
  144. if($bDisabled)
  145. $sClass .= $bShowDoReportAsButton || $bShowDoReportAsButtonSmall ? ' bx-btn-disabled' : 'bx-report-disabled';
  146. if($bReported)
  147. $sClass .= ' bx-report-reported';
  148. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoAction(), array(
  149. 'style_prefix' => $this->_sStylePrefix,
  150. 'html_id' => $this->_aHtmlIds['do_link'],
  151. 'class' => $sClass,
  152. 'title' => bx_html_attribute(_t($this->_getTitleDoReport($bReported))),
  153. 'bx_if:show_onclick' => array(
  154. 'condition' => !$bDisabled,
  155. 'content' => array(
  156. 'js_object' => $this->getJsObjectName()
  157. )
  158. ),
  159. 'do_report' => $this->_getLabelDoReport($aParams),
  160. ));
  161. }
  162. protected function _getCounterLabel($iCount)
  163. {
  164. return _t('_report_counter', $iCount);
  165. }
  166. protected function _getLabelDoReport($aParams = array())
  167. {
  168. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  169. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  170. 'style_prefix' => $this->_sStylePrefix,
  171. 'bx_if:show_icon' => array(
  172. 'condition' => isset($aParams['show_do_report_icon']) && $aParams['show_do_report_icon'] == true,
  173. 'content' => array(
  174. 'style_prefix' => $this->_sStylePrefix,
  175. 'name' => $this->_getIconDoReport($bReported)
  176. )
  177. ),
  178. 'bx_if:show_text' => array(
  179. 'condition' => isset($aParams['show_do_report_label']) && $aParams['show_do_report_label'] == true,
  180. 'content' => array(
  181. 'style_prefix' => $this->_sStylePrefix,
  182. 'text' => _t($this->_getTitleDoReport($bReported))
  183. )
  184. )
  185. ));
  186. }
  187. protected function _getReport()
  188. {
  189. if (!$this->isEnabled())
  190. return array('code' => 1, 'message' => _t('_report_err_not_enabled'));
  191. if(!$this->isAllowedReport())
  192. return array('code' => 2, 'message' => $this->msgErrAllowedReport());
  193. $iAuthorId = $this->_getAuthorId();
  194. $iAuthorNip = ip2long($this->_getAuthorIp());
  195. $iObjectId = $this->_iId;
  196. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  197. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  198. if($bPerformed) {
  199. if(!$this->isUndo())
  200. return array('code' => 4, 'message' => _t('_report_err_duplicate_report'));
  201. if(($iId = $this->_oQuery->putReport($iObjectId, $iAuthorId, true)) !== false) {
  202. $this->_trigger();
  203. bx_alert($this->_sSystem, 'undoReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
  204. bx_alert('report', 'undo', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  205. return $this->_returnReportData($iObjectId, $iId, !$bPerformed);
  206. }
  207. }
  208. $oForm = $this->_getFormObject();
  209. $oForm->setId($this->_aHtmlIds['do_form']);
  210. $oForm->setName($this->_aHtmlIds['do_form']);
  211. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  212. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  213. $oForm->aInputs['object_id']['value'] = $this->_iId;
  214. $oForm->aInputs['action']['value'] = 'Report';
  215. $oForm->initChecker();
  216. if($oForm->isSubmittedAndValid()) {
  217. $iObjectId = $oForm->getCleanValue('object_id');
  218. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  219. if(!$this->isAllowedReport(true))
  220. return array('code' => 2, 'message' => $this->msgErrAllowedReport());
  221. $sType = $oForm->getCleanValue('type');
  222. if(!in_array($sType, $this->_aTypes))
  223. return array('code' => 5, 'message' => _t('_report_err_wrong_type'));
  224. $sText = $oForm->getCleanValue('text');
  225. $sText = bx_process_input($sText, BX_DATA_TEXT_MULTILINE);
  226. $oForm->setSubmittedValue('text', $sText, $oForm->aFormAttrs['method']);
  227. $iId = (int)$oForm->insert(array('author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'date' => time()));
  228. if($iId != 0 && $this->_oQuery->putReport($iObjectId, $iAuthorId)) {
  229. if(!empty($this->_sObjectCmts) && ($oCmts = BxDolCmts::getObjectInstance($this->_sObjectCmts, $this->getId()))) {
  230. $sCmtText = '_report_comment';
  231. if(!empty($sText))
  232. $sCmtText = '_report_comment_with_note';
  233. $aTypes = BxDolForm::getDataItems('sys_report_types');
  234. $oCmts->add(array(
  235. 'cmt_author_id' => $iAuthorId,
  236. 'cmt_parent_id' => 0,
  237. 'cmt_text' => _t($sCmtText, $aTypes[$sType], $sText)
  238. ));
  239. }
  240. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', array(
  241. 'report_type' => $sType,
  242. 'report_text' => $sText,
  243. 'report_url' => $this->getBaseUrl(),
  244. ));
  245. if($aTemplate)
  246. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  247. $mixedParams = $this->_prepareAuditParams($iObjectId, array('type' => $sType, 'text' => $sText));
  248. if($mixedParams) {
  249. $sActionName = $mixedParams['action_name'];
  250. $iAuditObjectId = $mixedParams['object_id'];
  251. unset($mixedParams['action_name']);
  252. unset($mixedParams['object_id']);
  253. bx_audit($iAuditObjectId, $this->_sSystem, $sActionName, $mixedParams);
  254. }
  255. $aReport = $this->_oQuery->getReport($iObjectId);
  256. $iBlockContentAfter = (int)getParam('sys_security_block_content_after_n_reports');
  257. if ($iBlockContentAfter > 0 && $aReport['count'] >= $iBlockContentAfter){
  258. $oModule = BxDolModule::getInstance($this->_aSystem['module_name']);
  259. if($oModule)
  260. $oModule->_oDb->updateStatusAdmin($iObjectId, false);
  261. }
  262. $this->_trigger();
  263. bx_alert($this->_sSystem, 'doReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  264. bx_alert('report', 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  265. return $this->_returnReportData($iObjectId, $iId, !$bPerformed, $aReport);
  266. }
  267. return array('code' => 3, 'message' => _t('_report_err_cannot_perform_action'));
  268. }
  269. $sPopupId = $this->_aHtmlIds['do_popup'];
  270. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('report_do_report_form.html', array(
  271. 'style_prefix' => $this->_sStylePrefix,
  272. 'js_object' => $this->getJsObjectName(),
  273. 'form' => $oForm->getCode(),
  274. 'form_id' => $oForm->id,
  275. )));
  276. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  277. }
  278. protected function _getReportedBy()
  279. {
  280. $aTmplReports = array();
  281. $aTypes = BxDolForm::getDataItems('sys_report_types');
  282. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  283. foreach($aReports as $aReport) {
  284. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  285. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  286. $aTmplReports[] = array(
  287. 'style_prefix' => $this->_sStylePrefix,
  288. 'user_unit' => $sUserUnit,
  289. 'type' => $aTypes[$aReport['type']],
  290. 'bx_if:show_text' => array(
  291. 'condition' => strlen($sText) > 0,
  292. 'content' => array(
  293. 'text' => $sText
  294. )
  295. )
  296. );
  297. }
  298. if(empty($aTmplReports))
  299. $aTmplReports = MsgBox(_t('_Empty'));
  300. return $this->_oTemplate->parseHtmlByName('report_by_list.html', array(
  301. 'style_prefix' => $this->_sStylePrefix,
  302. 'bx_repeat:list' => $aTmplReports
  303. ));
  304. }
  305. protected function _returnReportData($iObjectId, $iReportId, $bPerformed, $aData = array())
  306. {
  307. $bUndo = $this->isUndo();
  308. if(empty($aData) || !is_array($aData))
  309. $aData = $this->_oQuery->getReport($iObjectId);
  310. return array(
  311. 'eval' => $this->getJsObjectName() . '.onReport(oData, oElement)',
  312. 'code' => 0,
  313. 'id' => $iReportId,
  314. 'count' => $aData['count'],
  315. 'countf' => (int)$aData['count'] > 0 ? $this->_getCounterLabel($aData['count']) : '',
  316. 'label_icon' => $this->_getIconDoReport($bPerformed),
  317. 'label_title' => _t($this->_getTitleDoReport($bPerformed)),
  318. 'disabled' => $bPerformed && !$bUndo,
  319. );
  320. }
  321. private function _prepareAuditParams($iObjectId, $aData)
  322. {
  323. $sModule = $this->_sSystem;
  324. $sKeyObjectContentInfo = 'OBJECT_CONTENT_INFO';
  325. $sActionName = '_sys_audit_action_report';
  326. if ($this->_sSystem == 'sys_cmts'){
  327. $aCommentData = BxDolCmtsQuery::getInfoByUniqId($iObjectId);
  328. $oComment = BxDolCmts::getObjectInstance($aCommentData['system_name'], $aCommentData['cmt_object_id']);
  329. $aComment = $oComment->getCommentSimple($aCommentData['cmt_id']);
  330. $aSystem = $oComment->getSystemInfo();
  331. $sModule = $aSystem['module'];
  332. $aData['comment_id'] = $aCommentData['cmt_id'];
  333. $aData['comment_author_id'] = $aComment['cmt_author_id'];
  334. $aData['comment_text'] = $aComment['cmt_text'];
  335. $iObjectId = $aCommentData['cmt_object_id'];
  336. $sKeyObjectContentInfo = 'OBJECT_CMTS_CONTENT_INFO';
  337. $sActionName = '_sys_audit_action_report_comment';
  338. }
  339. $oModule = BxDolModule::getInstance($sModule);
  340. if ($oModule){
  341. $CNF = $oModule->_oConfig->CNF;
  342. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iObjectId))) : array();
  343. $AuditParams = array(
  344. 'content_title' => isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  345. 'content_info_object' => isset($CNF[$sKeyObjectContentInfo]) ? $CNF[$sKeyObjectContentInfo] : '',
  346. 'data' => $aData,
  347. 'action_name' => $sActionName,
  348. 'object_id' => $iObjectId
  349. );
  350. return $AuditParams;
  351. }
  352. return false;
  353. }
  354. }
  355. /** @} */