BxDolView.php 6.5 KB

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