BxDolView.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. define('BX_DOL_VIEW_OLD_VIEWS', 3 * 86400); ///< views older than this number of seconds will be deleted automatically
  10. define('BX_DOL_VIEW_USAGE_BLOCK', 'block');
  11. define('BX_DOL_VIEW_USAGE_INLINE', 'inline');
  12. define('BX_DOL_VIEW_USAGE_DEFAULT', BX_DOL_VIEW_USAGE_BLOCK);
  13. /**
  14. * Track any object views automatically
  15. *
  16. * Add record to sys_object_view table to track object views,
  17. * to record view just create this class instance with your object id,
  18. * for example:
  19. * @code
  20. * BxDolView::getObjectInstance('my_system', 25); // 25 - is object id
  21. * @endcode
  22. *
  23. * Description of sys_object_view table fields:
  24. * @code
  25. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  26. * `table_track` - table to track views
  27. * `period` - period in secs to update next views, default is 86400(1 day)
  28. * `is_on` - is the system activated
  29. * `trigger_table` - table where you need to update views field
  30. * `trigger_field_id` - table field id to unique determine object
  31. * `trigger_field_count` - table field where total view number is stored
  32. * `class_name` - your custom class name, if you overrride default class
  33. * `class_file` - your custom class path
  34. * @endcode
  35. *
  36. * Structure of the track table is the following:
  37. * @code
  38. * CREATE TABLE `my_views_track` (
  39. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  40. * `viewer_id` int(11) NOT NULL default '0', -- viewer profile id
  41. * `viewer_nip` int(11) unsigned NOT NULL default '0', -- viewer ip address to track guest views
  42. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  43. * KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
  44. * );
  45. * @endcode
  46. *
  47. */
  48. class BxDolView extends BxDolObject
  49. {
  50. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  51. {
  52. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  53. if(empty($this->_sSystem))
  54. return;
  55. $this->_aSystem['per_page_default'] = 20;
  56. $this->_oQuery = new BxDolViewQuery($this);
  57. }
  58. /**
  59. * get votes object instanse
  60. * @param $sSys view object name
  61. * @param $iId associated content id, where vote is available
  62. * @param $iInit perform initialization
  63. * @return null on error, or ready to use class instance
  64. */
  65. public static function getObjectInstance($sSys, $iId, $iInit = true)
  66. {
  67. $sKey = 'BxDolView!' . $sSys . $iId;
  68. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  69. return $GLOBALS['bxDolClasses'][$sKey];
  70. $aSystems = self::getSystems();
  71. if(!isset($aSystems[$sSys]))
  72. return null;
  73. $sClassName = 'BxTemplView';
  74. if(!empty($aSystems[$sSys]['class_name'])) {
  75. $sClassName = $aSystems[$sSys]['class_name'];
  76. if(!empty($aSystems[$sSys]['class_file']))
  77. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  78. }
  79. $o = new $sClassName($sSys, $iId, $iInit);
  80. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  81. }
  82. public static function &getSystems()
  83. {
  84. $sKey = 'bx_dol_cache_memory_view_systems';
  85. if(!isset($GLOBALS[$sKey]))
  86. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_view', 'getAllWithKey', '
  87. SELECT
  88. `id` as `id`,
  89. `name` AS `name`,
  90. `module` AS `module`,
  91. `table_track` AS `table_track`,
  92. `period` AS `period`,
  93. `pruning` AS `pruning`,
  94. `is_on` AS `is_on`,
  95. `trigger_table` AS `trigger_table`,
  96. `trigger_field_id` AS `trigger_field_id`,
  97. `trigger_field_author` AS `trigger_field_author`,
  98. `trigger_field_count` AS `trigger_field_count`,
  99. `class_name` AS `class_name`,
  100. `class_file` AS `class_file`
  101. FROM `sys_objects_view`', 'name');
  102. return $GLOBALS[$sKey];
  103. }
  104. public static function onAuthorDelete ($iAuthorId)
  105. {
  106. $aSystems = self::getSystems();
  107. foreach($aSystems as $sSystem => $aSystem)
  108. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  109. return true;
  110. }
  111. public function actionGetViewedBy()
  112. {
  113. if (!$this->isEnabled())
  114. return '';
  115. return $this->_getViewedBy();
  116. }
  117. public function actionGetUsers()
  118. {
  119. if (!$this->isEnabled())
  120. return echoJson(array());
  121. $iStart = (int)bx_get('start');
  122. $iPerPage = (int)bx_get('per_page');
  123. return echoJson(array(
  124. 'content' => $this->_getViewedBy($iStart, $iPerPage),
  125. 'eval' => $this->getJsObjectName() . '.onGetUsers(oData)'
  126. ));
  127. }
  128. public function doView()
  129. {
  130. if(!$this->isEnabled())
  131. return false;
  132. $iObjectId = $this->getId();
  133. $iAuthorId = $this->_getAuthorId();
  134. $sAuthorIp = $this->_getAuthorIp();
  135. if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
  136. $this->_trigger();
  137. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  138. $oZ->alert();
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * Permissions functions
  145. *
  146. * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
  147. */
  148. public function isAllowedView($isPerformAction = false)
  149. {
  150. return false;
  151. }
  152. public function isAllowedViewView($isPerformAction = false)
  153. {
  154. if(isAdmin())
  155. return true;
  156. return $this->checkAction('view_view', $isPerformAction);
  157. }
  158. public function isAllowedViewViewViewers($isPerformAction = false)
  159. {
  160. $oAcl = BxDolAcl::getInstance();
  161. if(isAdmin() || $oAcl->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  162. return true;
  163. if(!empty($this->_aSystem['module']) && BxDolRequest::serviceExists($this->_aSystem['module'], 'act_as_profile') && BxDolService::call($this->_aSystem['module'], 'act_as_profile') === true)
  164. $iObjectAuthorId = BxDolProfile::getInstanceByContentAndType($this->_iId, $this->_aSystem['module'])->id();
  165. else
  166. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($this->_iId);
  167. return $iObjectAuthorId != 0 && $iObjectAuthorId == $this->_getAuthorId() && $this->checkAction('view_view_viewers_own', $isPerformAction);
  168. }
  169. /**
  170. * Internal functions
  171. */
  172. protected function _getIconDo()
  173. {
  174. return 'eye';
  175. }
  176. protected function _getTitleDo()
  177. {
  178. return '_view_do_view';
  179. }
  180. }
  181. /** @} */