BxDolReport.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $sKey = 'BxDolReport!' . $sSys . $iId . ($oTemplate ? $oTemplate->getClassName() : '');
  99. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  100. return $GLOBALS['bxDolClasses'][$sKey];
  101. $aSystems = self::getSystems();
  102. if(!isset($aSystems[$sSys]))
  103. return null;
  104. $sClassName = 'BxTemplReport';
  105. if(!empty($aSystems[$sSys]['class_name'])) {
  106. $sClassName = $aSystems[$sSys]['class_name'];
  107. if(!empty($aSystems[$sSys]['class_file']))
  108. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  109. }
  110. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  111. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  112. }
  113. public static function &getSystems()
  114. {
  115. $sKey = 'bx_dol_cache_memory_report_systems';
  116. if(!isset($GLOBALS[$sKey]))
  117. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_report', 'getAllWithKey', '
  118. SELECT
  119. `id` as `id`,
  120. `name` AS `name`,
  121. `table_main` AS `table_main`,
  122. `table_track` AS `table_track`,
  123. `is_on` AS `is_on`,
  124. `base_url` AS `base_url`,
  125. `trigger_table` AS `trigger_table`,
  126. `trigger_field_id` AS `trigger_field_id`,
  127. `trigger_field_author` AS `trigger_field_author`,
  128. `trigger_field_count` AS `trigger_field_count`,
  129. `class_name` AS `class_name`,
  130. `class_file` AS `class_file`
  131. FROM `sys_objects_report`', 'name');
  132. return $GLOBALS[$sKey];
  133. }
  134. public static function onAuthorDelete ($iAuthorId)
  135. {
  136. $aSystems = self::getSystems();
  137. foreach($aSystems as $sSystem => $aSystem)
  138. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  139. return true;
  140. }
  141. /**
  142. * Interface functions for outer usage
  143. */
  144. public function getBaseUrl()
  145. {
  146. return $this->_replaceMarkers($this->_sBaseUrl);
  147. }
  148. public function getStatCounter()
  149. {
  150. $aReport = $this->_oQuery->getReport($this->getId());
  151. return $aReport['count'];
  152. }
  153. /**
  154. * Actions functions
  155. */
  156. public function actionReport()
  157. {
  158. return echoJson($this->_getReport());
  159. }
  160. public function actionGetReportedBy()
  161. {
  162. if (!$this->isEnabled())
  163. return '';
  164. if(!$this->isAllowedReportView(true))
  165. return $this->msgErrAllowedReportView();
  166. return $this->_getReportedBy();
  167. }
  168. /**
  169. * Permissions functions
  170. */
  171. public function isAllowedReport($isPerformAction = false)
  172. {
  173. if(isAdmin())
  174. return true;
  175. return $this->checkAction('report', $isPerformAction);
  176. }
  177. public function msgErrAllowedReport()
  178. {
  179. return $this->checkActionErrorMsg('report');
  180. }
  181. public function isAllowedReportView($isPerformAction = false)
  182. {
  183. if(isAdmin())
  184. return true;
  185. return $this->checkAction('report_view', $isPerformAction);
  186. }
  187. public function msgErrAllowedReportView()
  188. {
  189. return $this->checkActionErrorMsg('report_view');
  190. }
  191. /**
  192. * Internal functions
  193. */
  194. protected function _getIconDoReport($bPerformed)
  195. {
  196. return 'exclamation-circle';
  197. }
  198. protected function _getTitleDoReport($bPerformed)
  199. {
  200. return '_report_do_report';
  201. }
  202. protected function _getFormObject()
  203. {
  204. return BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  205. }
  206. }
  207. /** @} */