BxDolReport.php 7.0 KB

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