1
0

BxBaseReport.php 13 KB

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