BxBaseReport.php 14 KB

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