BxBaseReport.php 14 KB

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