BxDolReport.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. bx_import('BxDolAcl');
  10. define('BX_DOL_REPORT_USAGE_BLOCK', 'block');
  11. define('BX_DOL_REPORT_USAGE_INLINE', 'inline');
  12. define('BX_DOL_REPORT_USAGE_DEFAULT', BX_DOL_REPORT_USAGE_BLOCK);
  13. /**
  14. * @page objects
  15. * @section reports Reports
  16. * @ref BxDolReport
  17. */
  18. /**
  19. * Report any content
  20. *
  21. * Related classes:
  22. * - BxDolReportQuery - report database queries
  23. * - BxBaseReport - report base representation
  24. * - BxTemplReport - custom template representation
  25. *
  26. * AJAX report for any content.
  27. *
  28. * To add report section to your feature you need to add a record to 'sys_objects_report' table:
  29. *
  30. * - id - autoincremented id for internal usage
  31. * - name - your unique module name, with vendor prefix, lowercase and spaces are underscored
  32. * - table_main - table name where summary reports are stored
  33. * - table_track - table name where each report is stored
  34. * - is_on - is this report object enabled
  35. * - base_url - base URL for reported object
  36. * - trigger_table - table to be updated upon each report
  37. * - trigger_field_id - trigger_table table field with unique record id, primary key
  38. * - trigger_field_author - trigger_table table field with author
  39. * - trigger_field_count - trigger_table table field with reports count
  40. * - class_name - your custom class name, if you overrride default class
  41. * - class_file - your custom class path
  42. *
  43. * You can refer to BoonEx modules for sample record in this table.
  44. *
  45. *
  46. *
  47. * @section example Example of usage:
  48. * You can show report in any place, using the following code:
  49. * @code
  50. * $o = BxDolReport::getObjectInstance('system object name', $iYourEntryId);
  51. * if (!$o->isEnabled())
  52. * return '';
  53. * echo $o->getElementBlock();
  54. * @endcode
  55. *
  56. *
  57. * @section acl Memberships/ACL:
  58. * - report
  59. * - report view
  60. *
  61. *
  62. * @section alerts Alerts:
  63. * Alerts type/unit - every module has own type/unit, it equals to ObjectName.
  64. * The following alerts are rised:
  65. *
  66. * - type: 'object name', action: doReport
  67. * - type: report, action: do
  68. *
  69. */
  70. class BxDolReport extends BxDolObject
  71. {
  72. protected $_oTemplate;
  73. protected $_sBaseUrl;
  74. protected $_sFormObject;
  75. protected $_sFormDisplayPost;
  76. protected $_aTypes;
  77. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  78. {
  79. parent::__construct($sSystem, $iId, $iInit);
  80. if(empty($this->_sSystem))
  81. return;
  82. $this->_oQuery = new BxDolReportQuery($this);
  83. if ($oTemplate)
  84. $this->_oTemplate = $oTemplate;
  85. else
  86. $this->_oTemplate = BxDolTemplate::getInstance();
  87. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  88. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  89. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  90. $this->_sFormObject = 'sys_report';
  91. $this->_sFormDisplayPost = 'sys_report_post';
  92. $this->_aTypes = array('spam', 'scam', 'fraud', 'nude', 'other');
  93. }
  94. /**
  95. * get reports object instanse
  96. * @param $sSys report object name
  97. * @param $iId associated content id, where report is available
  98. * @param $iInit perform initialization
  99. * @return null on error, or ready to use class instance
  100. */
  101. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  102. {
  103. if(isset($GLOBALS['bxDolClasses']['BxDolReport!' . $sSys . $iId]))
  104. return $GLOBALS['bxDolClasses']['BxDolReport!' . $sSys . $iId];
  105. $aSystems = self::getSystems();
  106. if(!isset($aSystems[$sSys]))
  107. return null;
  108. $sClassName = 'BxTemplReport';
  109. if(!empty($aSystems[$sSys]['class_name'])) {
  110. $sClassName = $aSystems[$sSys]['class_name'];
  111. if(!empty($aSystems[$sSys]['class_file']))
  112. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  113. }
  114. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  115. return ($GLOBALS['bxDolClasses']['BxDolReport!' . $sSys . $iId] = $o);
  116. }
  117. public static function &getSystems()
  118. {
  119. if(!isset($GLOBALS['bx_dol_report_systems']))
  120. $GLOBALS['bx_dol_report_systems'] = BxDolDb::getInstance()->fromCache('sys_objects_report', 'getAllWithKey', '
  121. SELECT
  122. `id` as `id`,
  123. `name` AS `name`,
  124. `table_main` AS `table_main`,
  125. `table_track` AS `table_track`,
  126. `is_on` AS `is_on`,
  127. `base_url` AS `base_url`,
  128. `trigger_table` AS `trigger_table`,
  129. `trigger_field_id` AS `trigger_field_id`,
  130. `trigger_field_author` AS `trigger_field_author`,
  131. `trigger_field_count` AS `trigger_field_count`,
  132. `class_name` AS `class_name`,
  133. `class_file` AS `class_file`
  134. FROM `sys_objects_report`', 'name');
  135. return $GLOBALS['bx_dol_report_systems'];
  136. }
  137. /**
  138. * Interface functions for outer usage
  139. */
  140. public function getBaseUrl()
  141. {
  142. return $this->_replaceMarkers($this->_sBaseUrl);
  143. }
  144. public function getStatCounter()
  145. {
  146. $aReport = $this->_oQuery->getReport($this->getId());
  147. return $aReport['count'];
  148. }
  149. /**
  150. * Actions functions
  151. */
  152. public function actionReport()
  153. {
  154. return echoJson($this->_getReport());
  155. }
  156. public function actionGetReportedBy()
  157. {
  158. if (!$this->isEnabled())
  159. return '';
  160. if(!$this->isAllowedReportView(true))
  161. return $this->msgErrAllowedReportView();
  162. return $this->_getReportedBy();
  163. }
  164. /**
  165. * Permissions functions
  166. */
  167. public function isAllowedReport($isPerformAction = false)
  168. {
  169. if(isAdmin())
  170. return true;
  171. return $this->checkAction('report', $isPerformAction);
  172. }
  173. public function msgErrAllowedReport()
  174. {
  175. return $this->checkActionErrorMsg('report');
  176. }
  177. public function isAllowedReportView($isPerformAction = false)
  178. {
  179. if(isAdmin())
  180. return true;
  181. return $this->checkAction('report_view', $isPerformAction);
  182. }
  183. public function msgErrAllowedReportView()
  184. {
  185. return $this->checkActionErrorMsg('report_view');
  186. }
  187. /**
  188. * Internal functions
  189. */
  190. protected function _getIconDoReport($bPerformed)
  191. {
  192. return 'exclamation-circle';
  193. }
  194. protected function _getTitleDoReport($bPerformed)
  195. {
  196. return '_report_do_report';
  197. }
  198. protected function _getFormObject()
  199. {
  200. return BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  201. }
  202. }
  203. /** @} */