BxBaseReport.php 13 KB

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