BxBaseReport.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. protected $_aElementDefaultsApi;
  21. protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
  22. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  23. {
  24. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  25. $this->_bCssJsAdded = false;
  26. $this->_sJsObjClass = 'BxDolReport';
  27. $this->_sJsObjName = 'oReport' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  28. $this->_sStylePrefix = 'bx-report';
  29. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  30. $this->_aHtmlIds = array(
  31. 'main' => 'bx-report-' . $sHtmlId,
  32. 'counter' => 'bx-report-counter-' . $sHtmlId,
  33. 'do_link' => 'bx-report-do-link-' . $sHtmlId,
  34. 'do_popup' => 'bx-report-do-popup-' . $sHtmlId,
  35. 'do_form' => 'bx-report-do-form-' . $sHtmlId,
  36. 'by_popup' => 'bx-report-by-popup-' . $sHtmlId
  37. );
  38. $this->_aElementDefaults = array(
  39. 'show_do_report_as_button' => false,
  40. 'show_do_report_as_button_small' => false,
  41. 'show_do_report_icon' => true,
  42. 'show_do_report_label' => false,
  43. 'show_counter' => true,
  44. 'show_counter_only' => true
  45. );
  46. if($this->_bApi) {
  47. $aTypes = BxDolFormQuery::getDataItems($this->_sTypesPreList);
  48. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  49. 'show_counter' => true,
  50. 'types' => array_map(function($sType) use ($aTypes) {
  51. return ['name' => $sType, 'title' => $aTypes[$sType]];
  52. }, $this->_aTypes)
  53. ]);
  54. $this->_aElementParamsApi = ['is_reported', 'types'];
  55. }
  56. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('report_element_block.html');
  57. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('report_element_inline.html');
  58. $this->_sTmplContentDoAction = $this->_oTemplate->getHtml('report_do_report.html');
  59. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('report_do_report_label.html');
  60. }
  61. public function report($aParams = [])
  62. {
  63. if (!$this->isEnabled())
  64. return ['code' => 1, 'message' => _t('_report_err_not_enabled')];
  65. if(!$this->isAllowedReport())
  66. return ['code' => 2, 'message' => $this->msgErrAllowedReport()];
  67. $iAuthorId = $this->_getAuthorId();
  68. $iObjectId = $this->_iId;
  69. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  70. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  71. if($bPerformed) {
  72. if(!$this->isUndo())
  73. return ['code' => 4, 'message' => _t('_report_err_duplicate_report')];
  74. if(($iId = $this->_oQuery->putReport($iObjectId, $iAuthorId, true)) !== false) {
  75. $this->_trigger();
  76. bx_alert($this->_sSystem, 'undoReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
  77. /**
  78. * @hooks
  79. * @hookdef hook-report-undo 'report', 'undo' - hook on cancel report
  80. * - $unit_name - equals `report`
  81. * - $action - equals `undo`
  82. * - $object_id - report id
  83. * - $sender_id - profile_id for report's author
  84. * - $extra_params - array of additional params with the following array keys:
  85. * - `object_system` - [string] system name, ex: bx_posts
  86. * - `object_id` - [int] reported object id
  87. * - `object_author_id` - [int] author's profile_id for reported object_id
  88. * @hook @ref hook-report-undo
  89. */
  90. bx_alert('report', 'undo', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  91. $aReport = $this->_getReport($iObjectId, true);
  92. $aResult = $this->_returnReportData($iObjectId, $iAuthorId, $iId, $aReport, !$bPerformed);
  93. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  94. $oSockets->sendEvent($this->getSocketName(), $iObjectId, 'reported', json_encode($this->_returnReportDataForSocket($aResult)));
  95. return $aResult;
  96. }
  97. }
  98. $oForm = $this->_getFormObject();
  99. $oForm->setId($this->_aHtmlIds['do_form']);
  100. $oForm->setName($this->_aHtmlIds['do_form']);
  101. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  102. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  103. $oForm->aInputs['object_id']['value'] = $this->_iId;
  104. $oForm->aInputs['action']['value'] = 'Report';
  105. $oForm->initChecker();
  106. if($oForm->isSubmittedAndValid() || ($this->_bApi && !empty($aParams) && is_array($aParams)))
  107. return $this->_report($bPerformed, $aParams, $oForm);
  108. $sPopupId = $this->_aHtmlIds['do_popup'];
  109. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('report_do_report_form.html', array(
  110. 'style_prefix' => $this->_sStylePrefix,
  111. 'js_object' => $this->getJsObjectName(),
  112. 'form' => $oForm->getCode(),
  113. 'form_id' => $oForm->id,
  114. )));
  115. return ['popup' => $sPopupContent, 'popup_id' => $sPopupId];
  116. }
  117. public function getJsObjectName()
  118. {
  119. return $this->_sJsObjName;
  120. }
  121. public function getJsScript($bDynamicMode = false)
  122. {
  123. $aParams = array(
  124. 'sObjName' => $this->_sJsObjName,
  125. 'sSystem' => $this->getSystemName(),
  126. 'iAuthorId' => $this->_getAuthorId(),
  127. 'iObjId' => $this->getId(),
  128. 'sRootUrl' => BX_DOL_URL_ROOT,
  129. 'sSocket' => $this->getSocketName(),
  130. 'sStylePrefix' => $this->_sStylePrefix,
  131. 'aHtmlIds' => $this->_aHtmlIds,
  132. 'sUnreportConfirm' => bx_js_string(_t('_report_do_unreport_confirm'))
  133. );
  134. $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  135. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  136. }
  137. public function getJsClick()
  138. {
  139. return $this->getJsObjectName() . '.report(this)';
  140. }
  141. public function getCounter($aParams = [])
  142. {
  143. $aParams = array_merge($this->_aElementDefaults, $aParams);
  144. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  145. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  146. $aReport = $this->_getReport($this->getId());
  147. $sClass = 'sys-action-counter';
  148. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  149. $sClass .= ' sys-ac-only';
  150. $sClass .= ' ' . $this->_sStylePrefix . '-counter';
  151. if($bShowDoReportAsButtonSmall)
  152. $sClass .= ' bx-btn-small-height';
  153. if($bShowDoReportAsButton)
  154. $sClass .= ' bx-btn-height';
  155. return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aReport['count'] > 0 ? $this->_getCounterLabel($aReport['count']) : '', array(
  156. 'id' => $this->_aHtmlIds['counter'],
  157. 'class' => $sClass,
  158. 'title' => _t('_report_do_report_by'),
  159. 'onclick' => 'javascript:' . $this->getJsObjectName() . '.toggleByPopup(this)'
  160. ));
  161. }
  162. public function getCounterAPI($aParams = [])
  163. {
  164. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  165. return $this->_getReport();
  166. }
  167. public function getElementBlock($aParams = array())
  168. {
  169. $aParams['usage'] = BX_DOL_REPORT_USAGE_BLOCK;
  170. return $this->getElement($aParams);
  171. }
  172. public function getElementInline($aParams = array())
  173. {
  174. $aParams['usage'] = BX_DOL_REPORT_USAGE_INLINE;
  175. return $this->getElement($aParams);
  176. }
  177. public function getElement($aParams = array())
  178. {
  179. $aParams = array_merge($this->_aElementDefaults, $aParams);
  180. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  181. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  182. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  183. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedReportView();
  184. $iObjectId = $this->getId();
  185. $iAuthorId = $this->_getAuthorId();
  186. $aReport = $this->_getReport($iObjectId);
  187. if(!$this->isAllowedReport() && (!$this->isAllowedReportView() || (int)$aReport['count'] == 0))
  188. return '';
  189. $aParams['is_reported'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  190. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_REPORT_USAGE_DEFAULT)}();
  191. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  192. 'style_prefix' => $this->_sStylePrefix,
  193. 'html_id' => $this->_aHtmlIds['main'],
  194. 'class' => $this->_sStylePrefix . ($bShowDoReportAsButton ? '-button' : '') . ($bShowDoReportAsButtonSmall ? '-button-small' : ''),
  195. 'count' => $aReport['count'],
  196. 'do_report' => $this->_getDoReport($aParams),
  197. 'bx_if:show_counter' => array(
  198. 'condition' => $bShowCounter,
  199. 'content' => array(
  200. 'style_prefix' => $this->_sStylePrefix,
  201. 'bx_if:show_hidden' => array(
  202. 'condition' => (int)$aReport['count'] == 0,
  203. 'content' => array()
  204. ),
  205. 'counter' => $this->getCounter(array_merge($aParams, [
  206. 'show_counter_only' => false
  207. ]))
  208. )
  209. ),
  210. 'script' => $this->getJsScript($bDynamicMode)
  211. ));
  212. }
  213. public function getElementAPI($aParams = [])
  214. {
  215. if(!$this->_bApi)
  216. return;
  217. if(!$this->isEnabled())
  218. return bx_api_get_msg('_report_err_not_enabled');
  219. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  220. $iObjectId = $this->getId();
  221. $iAuthorId = $this->_getAuthorId();
  222. $bCount = $this->_isCount();
  223. $isAllowedReport = $this->isAllowedReport();
  224. $isAllowedReportView = $this->isAllowedReportView();
  225. $aParams['is_reported'] = $this->isPerformed($iObjectId, $iAuthorId);
  226. $aParams['track'] = $aParams['is_reported'] ? $this->_getTrack($iObjectId, $iAuthorId) : [];
  227. //--- Do Report
  228. $bDoReport = $this->_isShowDoReport($aParams, $isAllowedReport, $isAllowedReportView, $bCount);
  229. $aDoReport = $bDoReport ? $this->_getDoReport($aParams, $isAllowedReport) : [];
  230. //--- Counter
  231. $bCounter = $this->_isShowCounter($aParams, $isAllowedReport, $isAllowedReportView, $bCount);
  232. $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
  233. 'show_counter_only' => false,
  234. 'show_script' => false
  235. ])) : [];
  236. if(!$bDoReport && !$bCounter)
  237. return bx_api_get_msg('');
  238. return [
  239. 'type' => 'reports',
  240. 'system' => $this->_sSystem,
  241. 'object_id' => $this->_iId,
  242. 'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
  243. 'action' => $aDoReport,
  244. 'counter' => $aCounter
  245. ];
  246. }
  247. public function getReportedByWithComments($sObjectNotes)
  248. {
  249. $aTmplReports = array();
  250. $aTypes = BxDolForm::getDataItems('sys_report_types');
  251. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  252. foreach($aReports as $aReport) {
  253. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  254. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  255. $oCmtsNotes = BxDolCmts::getObjectInstance($sObjectNotes, -$aReport['id'], true, $this->_oTemplate);
  256. $aCmtsNotes = $oCmtsNotes->getCommentsBlock(array(), array('in_designbox' => false));
  257. $iCmtsNotesCount = $oCmtsNotes->getCommentsCount();
  258. $aTmplReports[] = array(
  259. 'style_prefix' => $this->_sStylePrefix,
  260. 'user_name' => $sUserName,
  261. 'id' => $aReport['id'],
  262. 'user_url' => $sUserUrl,
  263. 'type' => $aTypes[$aReport['type']],
  264. 'comments_count' => _t('_report_comments_count', $iCmtsNotesCount),
  265. 'comments' => $aCmtsNotes['content'],
  266. 'date' => bx_time_js($aReport['date']),
  267. 'bx_if:show_text' => array(
  268. 'condition' => strlen($sText) > 0,
  269. 'content' => array(
  270. 'text' => $sText
  271. )
  272. )
  273. );
  274. }
  275. if(empty($aTmplReports))
  276. return '';
  277. return $this->_oTemplate->parseHtmlByName('report_by_list_with_comments.html', array(
  278. 'style_prefix' => $this->_sStylePrefix,
  279. 'bx_repeat:list' => $aTmplReports
  280. ));
  281. }
  282. public function getPerformedByAPI($aParams = [])
  283. {
  284. $aValues = $this->_oQuery->getPerformedBy($this->getId());
  285. $aTmplUsers = [];
  286. foreach($aValues as $mValue) {
  287. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  288. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  289. }
  290. return [
  291. 'performed_by' => $aTmplUsers
  292. ];
  293. }
  294. /**
  295. * Internal methods.
  296. */
  297. protected function _report($bPerformed, $aParams, &$oForm)
  298. {
  299. $iAuthorId = $this->_getAuthorId();
  300. $iAuthorNip = bx_get_ip_hash($this->_getAuthorIp());
  301. $iObjectId = $this->_bApi ? $this->_iId : $oForm->getCleanValue('object_id');
  302. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  303. if(!$this->isAllowedReport(true))
  304. return ['code' => 2, 'message' => $this->msgErrAllowedReport()];
  305. $sType = $this->_bApi ? $aParams['type'] : $oForm->getCleanValue('type');
  306. if(!in_array($sType, $this->_aTypes))
  307. return ['code' => 5, 'message' => _t('_report_err_wrong_type')];
  308. $sText = $this->_bApi ? $aParams['text'] : $oForm->getCleanValue('text');
  309. $sText = bx_process_input($sText, BX_DATA_TEXT_MULTILINE);
  310. $iId = (int)$oForm->insert(['object_id' => $iObjectId, 'author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'type' => $sType, 'text' => $sText, 'date' => time()]);
  311. if($iId != 0 && $this->_oQuery->putReport($iObjectId, $iAuthorId)) {
  312. if(!empty($this->_sObjectCmts) && ($oCmts = BxDolCmts::getObjectInstance($this->_sObjectCmts, $this->getId()))) {
  313. $sCmtText = '_report_comment';
  314. if(!empty($sText))
  315. $sCmtText = '_report_comment_with_note';
  316. $aTypes = BxDolForm::getDataItems('sys_report_types');
  317. $oCmts->add([
  318. 'cmt_author_id' => $iAuthorId,
  319. 'cmt_parent_id' => 0,
  320. 'cmt_text' => _t($sCmtText, $aTypes[$sType], $sText)
  321. ]);
  322. }
  323. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', [
  324. 'report_type' => $sType,
  325. 'report_text' => $sText,
  326. 'report_url' => $this->getBaseUrl(),
  327. ]);
  328. if($aTemplate)
  329. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  330. if(($mixedParams = $this->_prepareAuditParams($iObjectId, ['type' => $sType, 'text' => $sText]))) {
  331. $iAuditObjectId = (int)$mixedParams['object_id'];
  332. $sAuditActionName = $mixedParams['action_name'];
  333. unset($mixedParams['object_id'], $mixedParams['action_name']);
  334. bx_audit($iAuditObjectId, $this->_sSystem, $sAuditActionName, $mixedParams);
  335. }
  336. $aReport = $this->_getReport($iObjectId, true);
  337. $iBlockContentAfter = (int)getParam('sys_security_block_content_after_n_reports');
  338. if ($iBlockContentAfter > 0 && $aReport['count'] >= $iBlockContentAfter){
  339. $oModule = BxDolModule::getInstance($this->_aSystem['module_name']);
  340. if($oModule)
  341. $oModule->_oDb->updateStatusAdmin($iObjectId, false);
  342. }
  343. $this->_trigger();
  344. bx_alert($this->_sSystem, 'doReport', $iObjectId, $iAuthorId, ['report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText]);
  345. /**
  346. * @hooks
  347. * @hookdef hook-report-do 'report', 'do' - hook on create new report
  348. * - $unit_name - equals `report`
  349. * - $action - equals `do`
  350. * - $object_id - report id
  351. * - $sender_id - profile_id for report's author
  352. * - $extra_params - array of additional params with the following array keys:
  353. * - `object_system` - [string] system name, ex: bx_posts
  354. * - `object_id` - [int] reported object id
  355. * - `object_author_id` - [int] author's profile_id for reported object_id
  356. * - `type` - [string] report type
  357. * - `text` - [bool] report text
  358. * @hook @ref hook-report-do
  359. */
  360. bx_alert('report', 'do', $iId, $iAuthorId, ['object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText]);
  361. $aResult = $this->_returnReportData($iObjectId, $iAuthorId, $iId, $aReport, !$bPerformed);
  362. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  363. $oSockets->sendEvent($this->getSocketName(), $iObjectId, 'reported', json_encode($this->_returnReportDataForSocket($aResult)));
  364. return $aResult;
  365. }
  366. return ['code' => 3, 'message' => _t('_report_err_cannot_perform_action')];
  367. }
  368. protected function _getDoReport($aParams = array())
  369. {
  370. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  371. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  372. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  373. $bDisabled = !$this->isAllowedReport() || ($bReported && !$this->isUndo());
  374. $sTitle = call_user_func_array('_t', $this->_getTitleDoReport($bReported));
  375. if($this->_bApi)
  376. return [
  377. 'is_undo' => $this->isUndo(),
  378. 'is_reported' => $bReported,
  379. 'is_disabled' => $bDisabled,
  380. 'title' => $sTitle,
  381. ];
  382. $sClass = '';
  383. if($bShowDoReportAsButton)
  384. $sClass = 'bx-btn';
  385. else if ($bShowDoReportAsButtonSmall)
  386. $sClass = 'bx-btn bx-btn-small';
  387. if($bDisabled)
  388. $sClass .= $bShowDoReportAsButton || $bShowDoReportAsButtonSmall ? ' bx-btn-disabled' : 'bx-report-disabled';
  389. if($bReported)
  390. $sClass .= ' bx-report-reported';
  391. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoAction(), array(
  392. 'style_prefix' => $this->_sStylePrefix,
  393. 'html_id' => $this->_aHtmlIds['do_link'],
  394. 'class' => $sClass,
  395. 'title' => bx_html_attribute($sTitle),
  396. 'bx_if:show_onclick' => array(
  397. 'condition' => !$bDisabled,
  398. 'content' => array(
  399. 'js_object' => $this->getJsObjectName()
  400. )
  401. ),
  402. 'do_report' => $this->_getLabelDoReport($aParams),
  403. ));
  404. }
  405. protected function _getCounterLabel($iCount)
  406. {
  407. return _t('_report_counter', $iCount);
  408. }
  409. protected function _getLabelDoReport($aParams = array())
  410. {
  411. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  412. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  413. 'style_prefix' => $this->_sStylePrefix,
  414. 'bx_if:show_icon' => array(
  415. 'condition' => isset($aParams['show_do_report_icon']) && $aParams['show_do_report_icon'] == true,
  416. 'content' => array(
  417. 'style_prefix' => $this->_sStylePrefix,
  418. 'name' => $this->_getIconDoReport($bReported)
  419. )
  420. ),
  421. 'bx_if:show_text' => array(
  422. 'condition' => isset($aParams['show_do_report_label']) && $aParams['show_do_report_label'] == true,
  423. 'content' => array(
  424. 'style_prefix' => $this->_sStylePrefix,
  425. 'text' => call_user_func_array('_t', $this->_getTitleDoReport($bReported))
  426. )
  427. )
  428. ));
  429. }
  430. protected function _getReportedBy()
  431. {
  432. $aTmplReports = array();
  433. $aTypes = BxDolForm::getDataItems('sys_report_types');
  434. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  435. foreach($aReports as $aReport) {
  436. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  437. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  438. $aTmplReports[] = array(
  439. 'style_prefix' => $this->_sStylePrefix,
  440. 'user_unit' => $sUserUnit,
  441. 'type' => $aTypes[$aReport['type']],
  442. 'bx_if:show_text' => array(
  443. 'condition' => strlen($sText) > 0,
  444. 'content' => array(
  445. 'text' => $sText
  446. )
  447. )
  448. );
  449. }
  450. if(empty($aTmplReports))
  451. $aTmplReports = MsgBox(_t('_Empty'));
  452. return $this->_oTemplate->parseHtmlByName('report_by_list.html', array(
  453. 'style_prefix' => $this->_sStylePrefix,
  454. 'bx_repeat:list' => $aTmplReports
  455. ));
  456. }
  457. protected function _returnReportData($iObjectId, $iAuthorId, $iReportId, $aData, $bPerformed)
  458. {
  459. $bUndo = $this->isUndo();
  460. if(empty($aData) || !is_array($aData))
  461. $aData = $this->_getReport($iObjectId);
  462. $aResult = array(
  463. 'eval' => $this->getJsObjectName() . '.onReport(oData, oElement)',
  464. 'code' => 0,
  465. 'id' => $iReportId,
  466. 'count' => $aData['count'],
  467. 'countf' => (int)$aData['count'] > 0 ? $this->_getCounterLabel($aData['count']) : '',
  468. 'label_icon' => $this->_getIconDoReport($bPerformed),
  469. 'label_title' => call_user_func_array('_t', $this->_getTitleDoReport($bPerformed)),
  470. 'reported' => $bPerformed,
  471. 'disabled' => $bPerformed && !$bUndo,
  472. );
  473. $aResult['api'] = [
  474. 'performer_id' => $iAuthorId,
  475. 'is_reported' => $aResult['reported'],
  476. 'is_disabled' => $aResult['disabled'],
  477. 'icon' => $aResult['label_icon'],
  478. 'title' => $aResult['label_title'],
  479. 'counter' => $this->_getReport()
  480. ];
  481. return $aResult;
  482. }
  483. protected function _returnReportDataForSocket($aData, $aMask = [])
  484. {
  485. if(empty($aMask) || !is_array($aMask))
  486. $aMask = ['code', 'count', 'countf', 'api'];
  487. return array_intersect_key($aData, array_flip($aMask));
  488. }
  489. private function _prepareAuditParams($iObjectId, $aData)
  490. {
  491. $sModule = $this->_sSystem;
  492. $sKeyObjectContentInfo = 'OBJECT_CONTENT_INFO';
  493. $sActionName = '_sys_audit_action_report';
  494. if ($this->_sSystem == 'sys_cmts'){
  495. $aCommentData = BxDolCmtsQuery::getInfoByUniqId($iObjectId);
  496. $oComment = BxDolCmts::getObjectInstance($aCommentData['system_name'], $aCommentData['cmt_object_id']);
  497. $aComment = $oComment->getCommentSimple($aCommentData['cmt_id']);
  498. $aSystem = $oComment->getSystemInfo();
  499. $sModule = $aSystem['module'];
  500. $aData['comment_id'] = $aCommentData['cmt_id'];
  501. $aData['comment_author_id'] = $aComment['cmt_author_id'];
  502. $aData['comment_text'] = $aComment['cmt_text'];
  503. $iObjectId = $aCommentData['cmt_object_id'];
  504. $sKeyObjectContentInfo = 'OBJECT_CMTS_CONTENT_INFO';
  505. $sActionName = '_sys_audit_action_report_comment';
  506. }
  507. $oModule = BxDolModule::getInstance($sModule);
  508. if ($oModule) {
  509. $CNF = $oModule->_oConfig->CNF;
  510. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iObjectId))) : array();
  511. return [
  512. 'content_title' => !empty($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  513. 'content_info_object' => !empty($CNF[$sKeyObjectContentInfo]) ? $CNF[$sKeyObjectContentInfo] : '',
  514. 'data' => $aData,
  515. 'action_name' => $sActionName,
  516. 'object_id' => $iObjectId
  517. ];
  518. }
  519. return false;
  520. }
  521. protected function _isShowDoReport($aParams, $isAllowedReport, $isAllowedReportView, $bCount)
  522. {
  523. $bShowDoReport = !isset($aParams['show_do_report']) || (bool)$aParams['show_do_report'] === true;
  524. return $bShowDoReport && ($isAllowedReport || ($isAllowedReportView && $bCount));
  525. }
  526. protected function _isShowCounter($aParams, $isAllowedReport, $isAllowedReportView, $bCount)
  527. {
  528. $bShowCounter = isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true;
  529. return $bShowCounter && $isAllowedReportView && ($isAllowedReport || $bCount);
  530. }
  531. }
  532. /** @} */