BxDolReport.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 $_sObjectCmts;
  70. protected $_sFormObject;
  71. protected $_sFormDisplayPost;
  72. protected $_aTypes;
  73. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  74. {
  75. parent::__construct($sSystem, $iId, $iInit);
  76. if(empty($this->_sSystem))
  77. return;
  78. $this->_oQuery = new BxDolReportQuery($this);
  79. if ($oTemplate)
  80. $this->_oTemplate = $oTemplate;
  81. else
  82. $this->_oTemplate = BxDolTemplate::getInstance();
  83. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  84. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  85. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  86. $this->_sObjectCmts = $this->_aSystem['object_comment'];
  87. $this->_sFormObject = 'sys_report';
  88. $this->_sFormDisplayPost = 'sys_report_post';
  89. $this->_aTypes = array('spam', 'scam', 'fraud', 'nude', 'other');
  90. }
  91. /**
  92. * get reports object instanse
  93. * @param $sSys report object name
  94. * @param $iId associated content id, where report is available
  95. * @param $iInit perform initialization
  96. * @return null on error, or ready to use class instance
  97. */
  98. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  99. {
  100. $sKey = 'BxDolReport!' . $sSys . $iId . ($oTemplate ? $oTemplate->getClassName() : '');
  101. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  102. return $GLOBALS['bxDolClasses'][$sKey];
  103. $aSystems = self::getSystems();
  104. if(!isset($aSystems[$sSys]))
  105. return null;
  106. $sClassName = 'BxTemplReport';
  107. if(!empty($aSystems[$sSys]['class_name'])) {
  108. $sClassName = $aSystems[$sSys]['class_name'];
  109. if(!empty($aSystems[$sSys]['class_file']))
  110. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  111. }
  112. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  113. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  114. }
  115. public static function &getSystems()
  116. {
  117. $sKey = 'bx_dol_cache_memory_report_systems';
  118. if(!isset($GLOBALS[$sKey]))
  119. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_report', 'getAllWithKey', '
  120. SELECT
  121. `id` as `id`,
  122. `name` AS `name`,
  123. `module` AS `module_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. `object_comment` AS `object_comment`,
  129. `trigger_table` AS `trigger_table`,
  130. `trigger_field_id` AS `trigger_field_id`,
  131. `trigger_field_author` AS `trigger_field_author`,
  132. `trigger_field_count` AS `trigger_field_count`,
  133. `class_name` AS `class_name`,
  134. `class_file` AS `class_file`
  135. FROM `sys_objects_report`', 'name');
  136. return $GLOBALS[$sKey];
  137. }
  138. public static function onAuthorDelete ($iAuthorId)
  139. {
  140. $aSystems = self::getSystems();
  141. foreach($aSystems as $sSystem => $aSystem)
  142. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  143. return true;
  144. }
  145. /**
  146. * Interface functions for outer usage
  147. */
  148. public function getBaseUrl()
  149. {
  150. return $this->_replaceMarkers($this->_sBaseUrl);
  151. }
  152. public function getStatCounter()
  153. {
  154. $aReport = $this->_oQuery->getReport($this->getId());
  155. return $aReport['count'];
  156. }
  157. /**
  158. * Actions functions
  159. */
  160. public function actionReport()
  161. {
  162. return echoJson($this->_getReport());
  163. }
  164. public function actionClearReport()
  165. {
  166. $this->_oQuery->clearReports($this->getId());
  167. }
  168. public function actionGetReportedBy()
  169. {
  170. if (!$this->isEnabled())
  171. return '';
  172. if(!$this->isAllowedReportView(true))
  173. return $this->msgErrAllowedReportView();
  174. return $this->_getReportedBy();
  175. }
  176. /**
  177. * Permissions functions
  178. */
  179. public function isAllowedReport($isPerformAction = false)
  180. {
  181. if(isAdmin())
  182. return true;
  183. return $this->checkAction('report', $isPerformAction);
  184. }
  185. public function msgErrAllowedReport()
  186. {
  187. return $this->checkActionErrorMsg('report');
  188. }
  189. public function isAllowedReportView($isPerformAction = false)
  190. {
  191. if(isAdmin())
  192. return true;
  193. return $this->checkAction('report_view', $isPerformAction);
  194. }
  195. public function msgErrAllowedReportView()
  196. {
  197. return $this->checkActionErrorMsg('report_view');
  198. }
  199. /**
  200. * Internal functions
  201. */
  202. protected function _getIconDoReport($bPerformed)
  203. {
  204. return 'exclamation-circle';
  205. }
  206. protected function _getTitleDoReport($bPerformed)
  207. {
  208. return '_report_do_report';
  209. }
  210. protected function _getFormObject()
  211. {
  212. return BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  213. }
  214. }
  215. /** @} */