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