BxDolView.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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->_oQuery = new BxDolViewQuery($this);
  56. }
  57. /**
  58. * get votes object instanse
  59. * @param $sSys view object name
  60. * @param $iId associated content id, where vote is available
  61. * @param $iInit perform initialization
  62. * @return null on error, or ready to use class instance
  63. */
  64. public static function getObjectInstance($sSys, $iId, $iInit = true)
  65. {
  66. $sKey = 'BxDolView!' . $sSys . $iId;
  67. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  68. return $GLOBALS['bxDolClasses'][$sKey];
  69. $aSystems = self::getSystems();
  70. if(!isset($aSystems[$sSys]))
  71. return null;
  72. $sClassName = 'BxTemplView';
  73. if(!empty($aSystems[$sSys]['class_name'])) {
  74. $sClassName = $aSystems[$sSys]['class_name'];
  75. if(!empty($aSystems[$sSys]['class_file']))
  76. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  77. }
  78. $o = new $sClassName($sSys, $iId, $iInit);
  79. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  80. }
  81. public static function &getSystems()
  82. {
  83. $sKey = 'bx_dol_cache_memory_view_systems';
  84. if(!isset($GLOBALS[$sKey]))
  85. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_view', 'getAllWithKey', '
  86. SELECT
  87. `id` as `id`,
  88. `name` AS `name`,
  89. `module` AS `module`,
  90. `table_track` AS `table_track`,
  91. `period` AS `period`,
  92. `is_on` AS `is_on`,
  93. `trigger_table` AS `trigger_table`,
  94. `trigger_field_id` AS `trigger_field_id`,
  95. `trigger_field_author` AS `trigger_field_author`,
  96. `trigger_field_count` AS `trigger_field_count`,
  97. `class_name` AS `class_name`,
  98. `class_file` AS `class_file`
  99. FROM `sys_objects_view`', 'name');
  100. return $GLOBALS[$sKey];
  101. }
  102. public static function onAuthorDelete ($iAuthorId)
  103. {
  104. $aSystems = self::getSystems();
  105. foreach($aSystems as $sSystem => $aSystem)
  106. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  107. return true;
  108. }
  109. public function actionGetViewedBy()
  110. {
  111. if (!$this->isEnabled())
  112. return '';
  113. return $this->_getViewedBy();
  114. }
  115. public function doView()
  116. {
  117. if(!$this->isEnabled())
  118. return false;
  119. $iObjectId = $this->getId();
  120. $iAuthorId = $this->_getAuthorId();
  121. $sAuthorIp = $this->_getAuthorIp();
  122. if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
  123. $this->_trigger();
  124. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  125. $oZ->alert();
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * Permissions functions
  132. *
  133. * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
  134. */
  135. public function isAllowedView($isPerformAction = false)
  136. {
  137. return false;
  138. }
  139. public function isAllowedViewView($isPerformAction = false)
  140. {
  141. if(isAdmin())
  142. return true;
  143. return $this->checkAction('view_view', $isPerformAction);
  144. }
  145. public function isAllowedViewViewViewers($isPerformAction = false)
  146. {
  147. $oAcl = BxDolAcl::getInstance();
  148. if(isAdmin() || $oAcl->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  149. return true;
  150. if(!empty($this->_aSystem['module']) && BxDolRequest::serviceExists($this->_aSystem['module'], 'act_as_profile') && BxDolService::call($this->_aSystem['module'], 'act_as_profile') === true)
  151. $iObjectAuthorId = BxDolProfile::getInstanceByContentAndType($this->_iId, $this->_aSystem['module'])->id();
  152. else
  153. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($this->_iId);
  154. return $iObjectAuthorId != 0 && $iObjectAuthorId == $this->_getAuthorId() && $this->checkAction('view_view_viewers_own', $isPerformAction);
  155. }
  156. /**
  157. * Internal functions
  158. */
  159. protected function _getIconDo()
  160. {
  161. return 'eye';
  162. }
  163. protected function _getTitleDo()
  164. {
  165. return '_view_do_view';
  166. }
  167. }
  168. /** @} */