BxDolReport.php 8.7 KB

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