BxBaseReport.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 static $_sTmplContentElementBlock;
  15. protected static $_sTmplContentElementInline;
  16. protected static $_sTmplContentDoReport;
  17. protected static $_sTmplContentDoReportLabel;
  18. protected $_bCssJsAdded;
  19. protected $_sJsObjClass;
  20. protected $_sJsObjName;
  21. protected $_sStylePrefix;
  22. protected $_aHtmlIds;
  23. protected $_aElementDefaults;
  24. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  25. {
  26. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  27. $this->_bCssJsAdded = false;
  28. $this->_sJsObjClass = 'BxDolReport';
  29. $this->_sJsObjName = 'oReport' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  30. $this->_sStylePrefix = 'bx-report';
  31. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  32. $this->_aHtmlIds = array(
  33. 'main' => 'bx-report-' . $sHtmlId,
  34. 'counter' => 'bx-report-counter-' . $sHtmlId,
  35. 'do_link' => 'bx-report-do-link-' . $sHtmlId,
  36. 'do_popup' => 'bx-report-do-popup-' . $sHtmlId,
  37. 'do_form' => 'bx-report-do-form-' . $sHtmlId,
  38. 'by_popup' => 'bx-report-by-popup-' . $sHtmlId
  39. );
  40. $this->_aElementDefaults = array(
  41. 'show_do_report_as_button' => false,
  42. 'show_do_report_as_button_small' => false,
  43. 'show_do_report_icon' => true,
  44. 'show_do_report_label' => false,
  45. 'show_counter' => true
  46. );
  47. if(empty(self::$_sTmplContentElementBlock))
  48. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('report_element_block.html');
  49. if(empty(self::$_sTmplContentElementInline))
  50. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('report_element_inline.html');
  51. if(empty(self::$_sTmplContentDoReport))
  52. self::$_sTmplContentDoReport = $this->_oTemplate->getHtml('report_do_report.html');
  53. if(empty(self::$_sTmplContentDoReportLabel))
  54. self::$_sTmplContentDoReportLabel = $this->_oTemplate->getHtml('report_do_report_label.html');
  55. }
  56. public function addCssJs($bDynamicMode = false)
  57. {
  58. if($bDynamicMode || $this->_bCssJsAdded)
  59. return;
  60. $this->_oTemplate->addJs(array('jquery.form.min.js', 'jquery.anim.js', 'BxDolReport.js'));
  61. $this->_oTemplate->addCss(array('report.css'));
  62. $this->_bCssJsAdded = true;
  63. }
  64. public function getJsObjectName()
  65. {
  66. return $this->_sJsObjName;
  67. }
  68. public function getJsScript($bDynamicMode = false)
  69. {
  70. $aParams = array(
  71. 'sObjName' => $this->_sJsObjName,
  72. 'sSystem' => $this->getSystemName(),
  73. 'iAuthorId' => $this->_getAuthorId(),
  74. 'iObjId' => $this->getId(),
  75. 'sRootUrl' => BX_DOL_URL_ROOT,
  76. 'sStylePrefix' => $this->_sStylePrefix,
  77. 'aHtmlIds' => $this->_aHtmlIds
  78. );
  79. $sCode = $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  80. if($bDynamicMode) {
  81. $sCode = "var " . $this->_sJsObjName . " = null;
  82. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolReport.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  83. bx_get_style('" . bx_js_string($this->_oTemplate->getCssUrl('report.css'), BX_ESCAPE_STR_APOS) . "');
  84. " . $sCode . "
  85. }); ";
  86. }
  87. else
  88. $sCode = "var " . $sCode;
  89. $this->addCssJs($bDynamicMode);
  90. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  91. }
  92. public function getJsClick()
  93. {
  94. return $this->getJsObjectName() . '.report(this)';
  95. }
  96. public function getCounter($aParams = array())
  97. {
  98. $sJsObject = $this->getJsObjectName();
  99. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  100. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  101. $aReport = $this->_oQuery->getReport($this->getId());
  102. $sClass = $this->_sStylePrefix . '-counter';
  103. if($bShowDoReportAsButtonSmall)
  104. $sClass .= ' bx-btn-small-height';
  105. if($bShowDoReportAsButton)
  106. $sClass .= ' bx-btn-height';
  107. return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aReport['count'] > 0 ? $this->_getLabelCounter($aReport['count']) : '', array(
  108. 'id' => $this->_aHtmlIds['counter'],
  109. 'class' => $sClass,
  110. 'title' => _t('_report_do_report_by'),
  111. 'onclick' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'
  112. ));
  113. }
  114. public function getElementBlock($aParams = array())
  115. {
  116. $aParams['usage'] = BX_DOL_REPORT_USAGE_BLOCK;
  117. return $this->getElement($aParams);
  118. }
  119. public function getElementInline($aParams = array())
  120. {
  121. $aParams['usage'] = BX_DOL_REPORT_USAGE_INLINE;
  122. return $this->getElement($aParams);
  123. }
  124. public function getElement($aParams = array())
  125. {
  126. $aParams = array_merge($this->_aElementDefaults, $aParams);
  127. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  128. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  129. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  130. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedReportView();
  131. $iObjectId = $this->getId();
  132. $iAuthorId = $this->_getAuthorId();
  133. $aReport = $this->_oQuery->getReport($iObjectId);
  134. if(!$this->isAllowedReport() && (!$this->isAllowedReportView() || (int)$aReport['count'] == 0))
  135. return '';
  136. $aParams['is_reported'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  137. $sTmplName = self::${'_sTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_REPORT_USAGE_DEFAULT)};
  138. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  139. 'style_prefix' => $this->_sStylePrefix,
  140. 'html_id' => $this->_aHtmlIds['main'],
  141. 'class' => $this->_sStylePrefix . ($bShowDoReportAsButton ? '-button' : '') . ($bShowDoReportAsButtonSmall ? '-button-small' : ''),
  142. 'count' => $aReport['count'],
  143. 'do_report' => $this->_getDoReport($aParams),
  144. 'bx_if:show_counter' => array(
  145. 'condition' => $bShowCounter,
  146. 'content' => array(
  147. 'style_prefix' => $this->_sStylePrefix,
  148. 'bx_if:show_hidden' => array(
  149. 'condition' => (int)$aReport['count'] == 0,
  150. 'content' => array()
  151. ),
  152. 'counter' => $this->getCounter($aParams)
  153. )
  154. ),
  155. 'script' => $this->getJsScript($bDynamicMode)
  156. ));
  157. }
  158. protected function _getDoReport($aParams = array())
  159. {
  160. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  161. $bShowDoReportAsButtonSmall = isset($aParams['show_do_report_as_button_small']) && $aParams['show_do_report_as_button_small'] == true;
  162. $bShowDoReportAsButton = !$bShowDoReportAsButtonSmall && isset($aParams['show_do_report_as_button']) && $aParams['show_do_report_as_button'] == true;
  163. $bDisabled = !$this->isAllowedReport() || $bReported;
  164. $sClass = '';
  165. if($bShowDoReportAsButton)
  166. $sClass = 'bx-btn';
  167. else if ($bShowDoReportAsButtonSmall)
  168. $sClass = 'bx-btn bx-btn-small';
  169. if($bDisabled)
  170. $sClass .= $bShowDoReportAsButton || $bShowDoReportAsButtonSmall ? ' bx-btn-disabled' : 'bx-report-disabled';
  171. return $this->_oTemplate->parseHtmlByContent(self::$_sTmplContentDoReport, array(
  172. 'style_prefix' => $this->_sStylePrefix,
  173. 'html_id' => $this->_aHtmlIds['do_link'],
  174. 'class' => $sClass,
  175. 'title' => _t('_report_do_report'),
  176. 'bx_if:show_onclick' => array(
  177. 'condition' => !$bDisabled,
  178. 'content' => array(
  179. 'js_object' => $this->getJsObjectName()
  180. )
  181. ),
  182. 'do_report' => $this->_getLabelDoReport($aParams),
  183. ));
  184. }
  185. protected function _getLabelCounter($iCount)
  186. {
  187. return _t('_report_counter', $iCount);
  188. }
  189. protected function _getLabelDoReport($aParams = array())
  190. {
  191. $bReported = isset($aParams['is_reported']) && $aParams['is_reported'] === true;
  192. return $this->_oTemplate->parseHtmlByContent(self::$_sTmplContentDoReportLabel, array(
  193. 'bx_if:show_icon' => array(
  194. 'condition' => isset($aParams['show_do_report_icon']) && $aParams['show_do_report_icon'] == true,
  195. 'content' => array(
  196. 'name' => $this->_getIconDoReport($bReported)
  197. )
  198. ),
  199. 'bx_if:show_text' => array(
  200. 'condition' => isset($aParams['show_do_report_label']) && $aParams['show_do_report_label'] == true,
  201. 'content' => array(
  202. 'text' => _t($this->_getTitleDoReport($bReported))
  203. )
  204. )
  205. ));
  206. }
  207. protected function _getReport()
  208. {
  209. if (!$this->isEnabled())
  210. return array('code' => 1, 'msg' => _t('_report_err_not_enabled'));
  211. if(!$this->isAllowedReport())
  212. return array('code' => 2, 'msg' => $this->msgErrAllowedReport());
  213. $oForm = $this->_getFormObject();
  214. $oForm->setId($this->_aHtmlIds['do_form']);
  215. $oForm->setName($this->_aHtmlIds['do_form']);
  216. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  217. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  218. $oForm->aInputs['object_id']['value'] = $this->_iId;
  219. $oForm->aInputs['action']['value'] = 'Report';
  220. $oForm->initChecker();
  221. if($oForm->isSubmittedAndValid()) {
  222. $iObjectId = $oForm->getCleanValue('object_id');
  223. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  224. $iAuthorId = $this->_getAuthorId();
  225. $iAuthorNip = ip2long($this->_getAuthorIp());
  226. if(!$this->isAllowedReport(true))
  227. return array('code' => 2, 'msg' => $this->msgErrAllowedReport());
  228. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  229. if($bPerformed)
  230. return array('code' => 4, 'msg' => _t('_report_err_duplicate_report'));
  231. $sType = $oForm->getCleanValue('type');
  232. if(!in_array($sType, $this->_aTypes))
  233. return array('code' => 5, 'msg' => _t('_report_err_wrong_type'));
  234. $sText = $oForm->getCleanValue('text');
  235. $sText = bx_process_input($sText, BX_DATA_TEXT_MULTILINE);
  236. $oForm->setSubmittedValue('text', $sText, $oForm->aFormAttrs['method']);
  237. $iId = (int)$oForm->insert(array('author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'date' => time()));
  238. if($iId != 0 && $this->_oQuery->putReport($iObjectId)) {
  239. $this->_trigger();
  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. $oZ = new BxDolAlerts($this->_sSystem, 'doReport', $iObjectId, $iAuthorId, array('report_id' => $iId, 'report_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  248. $oZ->alert();
  249. $oZ = new BxDolAlerts('report', 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'type' => $sType, 'text' => $sText));
  250. $oZ->alert();
  251. $aReport = $this->_oQuery->getReport($iObjectId);
  252. return array(
  253. 'eval' => $this->getJsObjectName() . '.onReport(oData, oElement)',
  254. 'code' => 0,
  255. 'id' => $iId,
  256. 'count' => $aReport['count'],
  257. 'countf' => (int)$aReport['count'] > 0 ? $this->_getLabelCounter($aReport['count']) : '',
  258. 'label_icon' => $this->_getIconDoReport(!$bPerformed),
  259. 'label_title' => _t($this->_getTitleDoReport(!$bPerformed)),
  260. 'disabled' => !$bPerformed
  261. );
  262. }
  263. return array('code' => 3, 'msg' => _t('_report_err_cannot_perform_action'));
  264. }
  265. $sPopupId = $this->_aHtmlIds['do_popup'];
  266. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('report_do_report_form.html', array(
  267. 'style_prefix' => $this->_sStylePrefix,
  268. 'js_object' => $this->getJsObjectName(),
  269. 'form' => $oForm->getCode(),
  270. 'form_id' => $oForm->id,
  271. )));
  272. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  273. }
  274. protected function _getReportedBy()
  275. {
  276. $aTmplReports = array();
  277. $aTypes = BxDolForm::getDataItems('sys_report_types');
  278. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  279. foreach($aReports as $aReport) {
  280. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  281. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  282. $aTmplReports[] = array(
  283. 'style_prefix' => $this->_sStylePrefix,
  284. 'user_unit' => $sUserUnit,
  285. 'type' => $aTypes[$aReport['type']],
  286. 'bx_if:show_text' => array(
  287. 'condition' => strlen($sText) > 0,
  288. 'content' => array(
  289. 'text' => $sText
  290. )
  291. )
  292. );
  293. }
  294. if(empty($aTmplReports))
  295. $aTmplReports = MsgBox(_t('_Empty'));
  296. return $this->_oTemplate->parseHtmlByName('report_by_list.html', array(
  297. 'style_prefix' => $this->_sStylePrefix,
  298. 'bx_repeat:list' => $aTmplReports
  299. ));
  300. }
  301. }
  302. /** @} */