BxDolReport.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. `is_on` AS `is_on`,
  138. `base_url` AS `base_url`,
  139. `object_comment` AS `object_comment`,
  140. `trigger_table` AS `trigger_table`,
  141. `trigger_field_id` AS `trigger_field_id`,
  142. `trigger_field_author` AS `trigger_field_author`,
  143. `trigger_field_count` AS `trigger_field_count`,
  144. `class_name` AS `class_name`,
  145. `class_file` AS `class_file`
  146. FROM `sys_objects_report`', 'name');
  147. return $GLOBALS[$sKey];
  148. }
  149. public static function onAuthorDelete ($iAuthorId)
  150. {
  151. $aSystems = self::getSystems();
  152. foreach($aSystems as $sSystem => $aSystem)
  153. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  154. return true;
  155. }
  156. /**
  157. * Interface functions for outer usage
  158. */
  159. public function isUndo()
  160. {
  161. return (int)$this->_bUndo;
  162. }
  163. public function getBaseUrl()
  164. {
  165. return $this->_replaceMarkers($this->_sBaseUrl);
  166. }
  167. public function getStatCounter()
  168. {
  169. $aReport = $this->_oQuery->getReport($this->getId());
  170. return $aReport['count'];
  171. }
  172. /**
  173. * Actions functions
  174. */
  175. public function actionReport()
  176. {
  177. return echoJson($this->_getReport());
  178. }
  179. public function actionClearReport()
  180. {
  181. $this->_oQuery->clearReports($this->getId());
  182. }
  183. public function actionGetReportedBy()
  184. {
  185. if (!$this->isEnabled())
  186. return '';
  187. if(!$this->isAllowedReportView(true))
  188. return $this->msgErrAllowedReportView();
  189. return $this->_getReportedBy();
  190. }
  191. /**
  192. * Permissions functions
  193. */
  194. public function isAllowedReport($isPerformAction = false)
  195. {
  196. if(isAdmin())
  197. return true;
  198. return $this->checkAction('report', $isPerformAction);
  199. }
  200. public function msgErrAllowedReport()
  201. {
  202. return $this->checkActionErrorMsg('report');
  203. }
  204. public function isAllowedReportView($isPerformAction = false)
  205. {
  206. if(isAdmin())
  207. return true;
  208. return $this->checkAction('report_view', $isPerformAction);
  209. }
  210. public function changeStatusReport($iStatus, $iAuthorId, $sCmtText)
  211. {
  212. $iId = $this->getId();
  213. $aReport = $this->_oQuery->getDataById($iId);
  214. if(!empty($this->_sObjectCmts) && ($oCmts = BxDolCmts::getObjectInstance($this->_sObjectCmts, $aReport['object_id']))) {
  215. $oCmts->add(array(
  216. 'cmt_author_id' => $iAuthorId,
  217. 'cmt_parent_id' => 0,
  218. 'cmt_text' => $sCmtText
  219. ));
  220. }
  221. $this->_oQuery->changeStatusReport($iId, $iStatus, $iAuthorId);
  222. }
  223. public function msgErrAllowedReportView()
  224. {
  225. return $this->checkActionErrorMsg('report_view');
  226. }
  227. /**
  228. * Internal functions
  229. */
  230. protected function _getIconDoReport($bPerformed)
  231. {
  232. return $bPerformed && $this->isUndo() ? 'exclamation-circle' : 'exclamation-circle';
  233. }
  234. protected function _getTitleDoReport($bPerformed)
  235. {
  236. return $bPerformed && $this->isUndo() ? '_report_do_unreport' : '_report_do_report';
  237. }
  238. protected function _getFormObject()
  239. {
  240. return BxDolForm::getObjectInstance($this->_sFormObject, $this->_sFormDisplayPost);
  241. }
  242. }
  243. /** @} */