BxDolView.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  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. `table_track` AS `table_track`,
  89. `period` AS `period`,
  90. `is_on` AS `is_on`,
  91. `trigger_table` AS `trigger_table`,
  92. `trigger_field_id` AS `trigger_field_id`,
  93. `trigger_field_author` AS `trigger_field_author`,
  94. `trigger_field_count` AS `trigger_field_count`,
  95. `class_name` AS `class_name`,
  96. `class_file` AS `class_file`
  97. FROM `sys_objects_view`', 'name');
  98. return $GLOBALS[$sKey];
  99. }
  100. public static function onAuthorDelete ($iAuthorId)
  101. {
  102. $aSystems = self::getSystems();
  103. foreach($aSystems as $sSystem => $aSystem)
  104. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  105. return true;
  106. }
  107. public function actionGetViewedBy()
  108. {
  109. if (!$this->isEnabled())
  110. return '';
  111. return $this->_getViewedBy();
  112. }
  113. public function doView()
  114. {
  115. if(!$this->isEnabled())
  116. return false;
  117. $iObjectId = $this->getId();
  118. $iAuthorId = $this->_getAuthorId();
  119. $sAuthorIp = $this->_getAuthorIp();
  120. if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
  121. $this->_trigger();
  122. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  123. $oZ->alert();
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * Permissions functions
  130. *
  131. * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
  132. */
  133. public function isAllowedView($isPerformAction = false)
  134. {
  135. return false;
  136. }
  137. public function isAllowedViewView($isPerformAction = false)
  138. {
  139. if(isAdmin())
  140. return true;
  141. return $this->checkAction('view_view', $isPerformAction);
  142. }
  143. public function isAllowedViewViewViewers($isPerformAction = false)
  144. {
  145. $oAcl = BxDolAcl::getInstance();
  146. if(isAdmin() || $oAcl->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  147. return true;
  148. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($this->_iId);
  149. return $iObjectAuthorId != 0 && $iObjectAuthorId == $this->_getAuthorId() && $this->checkAction('view_view_viewers', $isPerformAction);
  150. }
  151. /**
  152. * Internal functions
  153. */
  154. protected function _getIconDo()
  155. {
  156. return 'eye';
  157. }
  158. protected function _getTitleDo()
  159. {
  160. return '_view_do_view';
  161. }
  162. }
  163. /** @} */