BxBaseReport.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. );
  63. $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  64. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  65. }
  66. public function getJsClick()
  67. {
  68. return $this->getJsObjectName() . '.report(this)';
  69. }
  70. public function getCounter($aParams = array())
  71. {
  72. $sJsObject = $this->getJsObjectName();
  73. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  74. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  75. $aReport = $this->_oQuery->getReport($this->getId());
  76. $sClass = $this->_sStylePrefix . '-counter';
  77. if($bShowDoReportAsButtonSmall)
  78. $sClass .= ' bx-btn-small-height';
  79. if($bShowDoReportAsButton)
  80. $sClass .= ' bx-btn-height';
  81. return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aReport['count'] > 0 ? $this->_getCounterLabel($aReport['count']) : '', array(
  82. 'id' => $this->_aHtmlIds['counter'],
  83. 'class' => $sClass,
  84. 'title' => _t('_report_do_report_by'),
  85. 'onclick' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'
  86. ));
  87. }
  88. public function getElementBlock($aParams = array())
  89. {
  90. $aParams['usage'] = BX_DOL_REPORT_USAGE_BLOCK;
  91. return $this->getElement($aParams);
  92. }
  93. public function getElementInline($aParams = array())
  94. {
  95. $aParams['usage'] = BX_DOL_REPORT_USAGE_INLINE;
  96. return $this->getElement($aParams);
  97. }
  98. public function getElement($aParams = array())
  99. {
  100. $aParams = array_merge($this->_aElementDefaults, $aParams);
  101. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  102. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  103. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  104. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedReportView();
  105. $iObjectId = $this->getId();
  106. $iAuthorId = $this->_getAuthorId();
  107. $aReport = $this->_oQuery->getReport($iObjectId);
  108. if(!$this->isAllowedReport() && (!$this->isAllowedReportView() || (int)$aReport['count'] == 0))
  109. return '';
  110. $aParams['is_reported'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  111. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_REPORT_USAGE_DEFAULT)}();
  112. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  113. 'style_prefix' => $this->_sStylePrefix,
  114. 'html_id' => $this->_aHtmlIds['main'],
  115. 'class' => $this->_sStylePrefix . ($bShowDoReportAsButton ? '-button' : '') . ($bShowDoReportAsButtonSmall ? '-button-small' : ''),
  116. 'count' => $aReport['count'],
  117. 'do_report' => $this->_getDoReport($aParams),
  118. 'bx_if:show_counter' => array(
  119. 'condition' => $bShowCounter,
  120. 'content' => array(
  121. 'style_prefix' => $this->_sStylePrefix,
  122. 'bx_if:show_hidden' => array(
  123. 'condition' => (int)$aReport['count'] == 0,
  124. 'content' => array()
  125. ),
  126. 'counter' => $this->getCounter($aParams)
  127. )
  128. ),
  129. 'script' => $this->getJsScript($bDynamicMode)
  130. ));
  131. }
  132. protected function _getDoReport($aParams = array())
  133. {
  134. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  135. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  136. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  137. $bDisabled = !$this->isAllowedReport() || $bReported;
  138. $sClass = '';
  139. if($bShowDoReportAsButton)
  140. $sClass = 'bx-btn';
  141. else if ($bShowDoReportAsButtonSmall)
  142. $sClass = 'bx-btn bx-btn-small';
  143. if($bDisabled)
  144. $sClass .= $bShowDoReportAsButton || $bShowDoReportAsButtonSmall ? ' bx-btn-disabled' : 'bx-report-disabled';
  145. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoAction(), array(
  146. 'style_prefix' => $this->_sStylePrefix,
  147. 'html_id' => $this->_aHtmlIds['do_link'],
  148. 'class' => $sClass,
  149. 'title' => _t('_report_do_report'),
  150. 'bx_if:show_onclick' => array(
  151. 'condition' => !$bDisabled,
  152. 'content' => array(
  153. 'js_object' => $this->getJsObjectName()
  154. )
  155. ),
  156. 'do_report' => $this->_getLabelDoReport($aParams),
  157. ));
  158. }
  159. protected function _getCounterLabel($iCount)
  160. {
  161. return _t('_report_counter', $iCount);
  162. }
  163. protected function _getLabelDoReport($aParams = array())
  164. {
  165. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  166. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  167. 'style_prefix' => $this->_sStylePrefix,
  168. 'bx_if:show_icon' => array(
  169. 'condition' => isset($aParams['show_do_report_icon']) && $aParams['show_do_report_icon'] == true,
  170. 'content' => array(
  171. 'style_prefix' => $this->_sStylePrefix,
  172. 'name' => $this->_getIconDoReport($bReported)
  173. )
  174. ),
  175. 'bx_if:show_text' => array(
  176. 'condition' => isset($aParams['show_do_report_label']) && $aParams['show_do_report_label'] == true,
  177. 'content' => array(
  178. 'style_prefix' => $this->_sStylePrefix,
  179. 'text' => _t($this->_getTitleDoReport($bReported))
  180. )
  181. )
  182. ));
  183. }
  184. protected function _getReport()
  185. {
  186. if (!$this->isEnabled())
  187. return array('code' => 1, 'message' => _t('_report_err_not_enabled'));
  188. if(!$this->isAllowedReport())
  189. return array('code' => 2, 'message' => $this->msgErrAllowedReport());
  190. $oForm = $this->_getFormObject();
  191. $oForm->setId($this->_aHtmlIds['do_form']);
  192. $oForm->setName($this->_aHtmlIds['do_form']);
  193. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  194. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  195. $oForm->aInputs['object_id']['value'] = $this->_iId;
  196. $oForm->aInputs['action']['value'] = 'Report';
  197. $oForm->initChecker();
  198. if($oForm->isSubmittedAndValid()) {
  199. $iObjectId = $oForm->getCleanValue('object_id');
  200. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  201. $iAuthorId = $this->_getAuthorId();
  202. $iAuthorNip = ip2long($this->_getAuthorIp());
  203. if(!$this->isAllowedReport(true))
  204. return array('code' => 2, 'message' => $this->msgErrAllowedReport());
  205. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  206. if($bPerformed)
  207. return array('code' => 4, 'message' => _t('_report_err_duplicate_report'));
  208. $sType = $oForm->getCleanValue('type');
  209. if(!in_array($sType, $this->_aTypes))
  210. return array('code' => 5, 'message' => _t('_report_err_wrong_type'));
  211. $sText = $oForm->getCleanValue('text');
  212. $sText = bx_process_input($sText, BX_DATA_TEXT_MULTILINE);
  213. $oForm->setSubmittedValue('text', $sText, $oForm->aFormAttrs['method']);
  214. $iId = (int)$oForm->insert(array('author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'date' => time()));
  215. if($iId != 0 && $this->_oQuery->putReport($iObjectId)) {
  216. $this->_trigger();
  217. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', array(
  218. 'report_type' => $sType,
  219. 'report_text' => $sText,
  220. 'report_url' => $this->getBaseUrl(),
  221. ));
  222. if($aTemplate)
  223. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  224. $oZ = new BxDolAlerts($this->_sSystem, 'doReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  225. $oZ->alert();
  226. $oZ = new BxDolAlerts('report', 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  227. $oZ->alert();
  228. bx_audit(
  229. $iObjectId,
  230. $this->_sSystem,
  231. '_sys_audit_action_report',
  232. $this->_prepareAuditParams($iObjectId, array('type' => $sType, 'text' => $sText))
  233. );
  234. $aReport = $this->_oQuery->getReport($iObjectId);
  235. return array(
  236. 'eval' => $this->getJsObjectName() . '.onReport(oData, oElement)',
  237. 'code' => 0,
  238. 'id' => $iId,
  239. 'count' => $aReport['count'],
  240. 'countf' => (int)$aReport['count'] > 0 ? $this->_getCounterLabel($aReport['count']) : '',
  241. 'label_icon' => $this->_getIconDoReport(!$bPerformed),
  242. 'label_title' => _t($this->_getTitleDoReport(!$bPerformed)),
  243. 'disabled' => !$bPerformed
  244. );
  245. }
  246. return array('code' => 3, 'message' => _t('_report_err_cannot_perform_action'));
  247. }
  248. $sPopupId = $this->_aHtmlIds['do_popup'];
  249. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('report_do_report_form.html', array(
  250. 'style_prefix' => $this->_sStylePrefix,
  251. 'js_object' => $this->getJsObjectName(),
  252. 'form' => $oForm->getCode(),
  253. 'form_id' => $oForm->id,
  254. )));
  255. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  256. }
  257. protected function _getReportedBy()
  258. {
  259. $aTmplReports = array();
  260. $aTypes = BxDolForm::getDataItems('sys_report_types');
  261. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  262. foreach($aReports as $aReport) {
  263. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  264. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  265. $aTmplReports[] = array(
  266. 'style_prefix' => $this->_sStylePrefix,
  267. 'user_unit' => $sUserUnit,
  268. 'type' => $aTypes[$aReport['type']],
  269. 'bx_if:show_text' => array(
  270. 'condition' => strlen($sText) > 0,
  271. 'content' => array(
  272. 'text' => $sText
  273. )
  274. )
  275. );
  276. }
  277. if(empty($aTmplReports))
  278. $aTmplReports = MsgBox(_t('_Empty'));
  279. return $this->_oTemplate->parseHtmlByName('report_by_list.html', array(
  280. 'style_prefix' => $this->_sStylePrefix,
  281. 'bx_repeat:list' => $aTmplReports
  282. ));
  283. }
  284. private function _prepareAuditParams($iObjectId, $aData)
  285. {
  286. $sModule = $this->_sSystem;
  287. $oModule = BxDolModule::getInstance($sModule);
  288. $CNF = $oModule->_oConfig->CNF;
  289. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iObjectId))) : array();
  290. $AuditParams = array(
  291. 'content_title' => isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  292. 'content_info_object' => isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
  293. 'data' => $aData
  294. );
  295. return $AuditParams;
  296. }
  297. }
  298. /** @} */