BxDolReport.php 11 KB

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