BxBaseReport.php 29 KB

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