BxDolReport.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 $_aReport;
  79. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  80. {
  81. parent::__construct($sSystem, $iId, $iInit);
  82. if(empty($this->_sSystem))
  83. return;
  84. $this->_oQuery = new BxDolReportQuery($this);
  85. if ($oTemplate)
  86. $this->_oTemplate = $oTemplate;
  87. else
  88. $this->_oTemplate = BxDolTemplate::getInstance();
  89. $this->_bUndo = true;
  90. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  91. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  92. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  93. $this->_sObjectCmts = $this->_aSystem['object_comment'];
  94. $this->_sFormObject = 'sys_report';
  95. $this->_sFormDisplayPost = 'sys_report_post';
  96. $this->_sTypesPreList = 'sys_report_types';
  97. $aTypes = BxDolFormQuery::getDataItems($this->_sTypesPreList);
  98. if(!empty($aTypes) && is_array($aTypes))
  99. $this->_aTypes = array_keys($aTypes);
  100. else
  101. $this->_aTypes = array('spam', 'scam', 'fraud', 'nude', 'other');
  102. $this->_aReport = [];
  103. }
  104. /**
  105. * get reports object instanse
  106. * @param $sSys report object name
  107. * @param $iId associated content id, where report is available
  108. * @param $iInit perform initialization
  109. * @return null on error, or ready to use class instance
  110. */
  111. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  112. {
  113. $sKey = 'BxDolReport!' . $sSys . $iId . ($oTemplate ? $oTemplate->getClassName() : '');
  114. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  115. return $GLOBALS['bxDolClasses'][$sKey];
  116. $aSystems = self::getSystems();
  117. if(!isset($aSystems[$sSys]))
  118. return null;
  119. $sClassName = 'BxTemplReport';
  120. if(!empty($aSystems[$sSys]['class_name'])) {
  121. $sClassName = $aSystems[$sSys]['class_name'];
  122. if(!empty($aSystems[$sSys]['class_file']))
  123. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  124. }
  125. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  126. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  127. }
  128. public static function &getSystems()
  129. {
  130. $sKey = 'bx_dol_cache_memory_report_systems';
  131. if(!isset($GLOBALS[$sKey]))
  132. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_report', 'getAllWithKey', '
  133. SELECT
  134. `id` as `id`,
  135. `name` AS `name`,
  136. `module` AS `module_name`,
  137. `table_main` AS `table_main`,
  138. `table_track` AS `table_track`,
  139. `pruning` AS `pruning`,
  140. `is_on` AS `is_on`,
  141. `base_url` AS `base_url`,
  142. `object_comment` AS `object_comment`,
  143. `trigger_table` AS `trigger_table`,
  144. `trigger_field_id` AS `trigger_field_id`,
  145. `trigger_field_author` AS `trigger_field_author`,
  146. `trigger_field_count` AS `trigger_field_count`,
  147. `class_name` AS `class_name`,
  148. `class_file` AS `class_file`
  149. FROM `sys_objects_report`', 'name');
  150. return $GLOBALS[$sKey];
  151. }
  152. public static function onAuthorDelete ($iAuthorId)
  153. {
  154. $aSystems = self::getSystems();
  155. foreach($aSystems as $sSystem => $aSystem)
  156. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  157. return true;
  158. }
  159. /**
  160. * Interface functions for outer usage
  161. */
  162. public function isUndo()
  163. {
  164. return (int)$this->_bUndo == 1;
  165. }
  166. public function getBaseUrl()
  167. {
  168. return $this->_replaceMarkers($this->_sBaseUrl);
  169. }
  170. public function getStatCounter()
  171. {
  172. $aReport = $this->_oQuery->getReport($this->getId());
  173. return $aReport['count'];
  174. }
  175. public function getCountByStatus($iStatus)
  176. {
  177. return $this->_oQuery->getReportsCountByStatus($iStatus);
  178. }
  179. public function getSocketName()
  180. {
  181. return $this->_sSystem . '_reports';
  182. }
  183. /**
  184. * Actions functions
  185. */
  186. public function actionReport()
  187. {
  188. return echoJson($this->report());
  189. }
  190. public function actionClearReport()
  191. {
  192. $this->_oQuery->clearReports($this->getId());
  193. }
  194. public function actionGetReportedBy()
  195. {
  196. if (!$this->isEnabled())
  197. return '';
  198. if(!$this->isAllowedReportView(true))
  199. return $this->msgErrAllowedReportView();
  200. return $this->_getReportedBy();
  201. }
  202. /**
  203. * Permissions functions
  204. */
  205. public function isAllowedReport($isPerformAction = false)
  206. {
  207. if(isAdmin())
  208. return true;
  209. return $this->checkAction('report', $isPerformAction);
  210. }
  211. public function msgErrAllowedReport()
  212. {
  213. return $this->checkActionErrorMsg('report');
  214. }
  215. public function isAllowedReportView($isPerformAction = false)
  216. {
  217. if(isAdmin())
  218. return true;
  219. return $this->checkAction('report_view', $isPerformAction);
  220. }
  221. public function changeStatusReport($iStatus, $iAuthorId, $sCmtText)
  222. {
  223. $iId = $this->getId();
  224. $aReport = $this->_oQuery->getDataById($iId);
  225. if(!empty($this->_sObjectCmts) && ($oCmts = BxDolCmts::getObjectInstance($this->_sObjectCmts, $aReport['object_id']))) {
  226. $oCmts->add(array(
  227. 'cmt_author_id' => $iAuthorId,
  228. 'cmt_parent_id' => 0,
  229. 'cmt_text' => $sCmtText
  230. ));
  231. }
  232. $this->_oQuery->changeStatusReport($iId, $iStatus, $iAuthorId);
  233. }
  234. public function msgErrAllowedReportView()
  235. {
  236. return $this->checkActionErrorMsg('report_view');
  237. }
  238. /**
  239. * Internal functions
  240. */
  241. protected function _isCount($aVote = array())
  242. {
  243. if(empty($aReport))
  244. $aReport = $this->_getReport();
  245. return isset($aReport['count']) && (int)$aReport['count'] != 0;
  246. }
  247. protected function _getReport($iObjectId = 0, $bForceGet = false)
  248. {
  249. if(!empty($this->_aReport) && !$bForceGet)
  250. return $this->_aReport;
  251. if(empty($iObjectId))
  252. $iObjectId = $this->getId();
  253. $this->_aReport = $this->_oQuery->getReport($iObjectId);
  254. return $this->_aReport;
  255. }
  256. protected function _isReport($iObjectId = 0, $bForceGet = false)
  257. {
  258. $aReport = $this->_getReport($iObjectId, $bForceGet);
  259. return (int)$aReport['count'] > 0;
  260. }
  261. protected function _getTrack($iObjectId, $iAuthorId)
  262. {
  263. return $this->_oQuery->getTrack($iObjectId, $iAuthorId);
  264. }
  265. protected function _getIconDoReport($bPerformed)
  266. {
  267. return $bPerformed && $this->isUndo() ? 'exclamation-circle' : 'exclamation-circle';
  268. }
  269. protected function _getTitleDoReport($bPerformed)
  270. {
  271. if($bPerformed && $this->isUndo())
  272. return ['_report_do_unreport'];
  273. $sSample = '_' . $this->_sSystem . '_txt_sample_single';
  274. if(($$sSample = _t($sSample)) && strcmp($sSample, $$sSample) != 0)
  275. return ['_report_do_report_with_sample', $$sSample];
  276. return ['_report_do_report'];
  277. }
  278. protected function _getFormObject()
  279. {
  280. return BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  281. }
  282. }
  283. /** @} */