BxDolView.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * @page objects
  15. * @section views Views
  16. * @ref BxDolView
  17. */
  18. /**
  19. * Track any object views automatically
  20. *
  21. * Add record to sys_object_view table to track object views,
  22. * to record view just create this class instance with your object id,
  23. * for example:
  24. * @code
  25. * BxDolView::getObjectInstance('my_system', 25); // 25 - is object id
  26. * @endcode
  27. *
  28. * Description of sys_object_view table fields:
  29. * @code
  30. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  31. * `table_track` - table to track views
  32. * `period` - period in secs to update next views, default is 86400(1 day)
  33. * `is_on` - is the system activated
  34. * `trigger_table` - table where you need to update views field
  35. * `trigger_field_id` - table field id to unique determine object
  36. * `trigger_field_count` - table field where total view number is stored
  37. * `class_name` - your custom class name, if you overrride default class
  38. * `class_file` - your custom class path
  39. * @endcode
  40. *
  41. * Structure of the track table is the following:
  42. * @code
  43. * CREATE TABLE `my_views_track` (
  44. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  45. * `viewer_id` int(11) NOT NULL default '0', -- viewer profile id
  46. * `viewer_nip` int(11) unsigned NOT NULL default '0', -- viewer ip address to track guest views
  47. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  48. * KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
  49. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  50. * @endcode
  51. *
  52. */
  53. class BxDolView extends BxDolObject
  54. {
  55. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  56. {
  57. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  58. if(empty($this->_sSystem))
  59. return;
  60. $this->_oQuery = new BxDolViewQuery($this);
  61. }
  62. /**
  63. * get votes object instanse
  64. * @param $sSys view object name
  65. * @param $iId associated content id, where vote is available
  66. * @param $iInit perform initialization
  67. * @return null on error, or ready to use class instance
  68. */
  69. public static function getObjectInstance($sSys, $iId, $iInit = true)
  70. {
  71. if(isset($GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId]))
  72. return $GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId];
  73. $aSystems = self::getSystems();
  74. if(!isset($aSystems[$sSys]))
  75. return null;
  76. $sClassName = 'BxTemplView';
  77. if(!empty($aSystems[$sSys]['class_name'])) {
  78. $sClassName = $aSystems[$sSys]['class_name'];
  79. if(!empty($aSystems[$sSys]['class_file']))
  80. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  81. }
  82. $o = new $sClassName($sSys, $iId, $iInit);
  83. return ($GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId] = $o);
  84. }
  85. public static function &getSystems()
  86. {
  87. if(!isset($GLOBALS['bx_dol_view_systems']))
  88. $GLOBALS['bx_dol_view_systems'] = BxDolDb::getInstance()->fromCache('sys_objects_view', 'getAllWithKey', '
  89. SELECT
  90. `id` as `id`,
  91. `name` AS `name`,
  92. `table_track` AS `table_track`,
  93. `period` AS `period`,
  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['bx_dol_view_systems'];
  103. }
  104. public function actionGetViewedBy()
  105. {
  106. if (!$this->isEnabled())
  107. return '';
  108. return $this->_getViewedBy();
  109. }
  110. public function doView()
  111. {
  112. if(!$this->isEnabled())
  113. return false;
  114. $iObjectId = $this->getId();
  115. $iAuthorId = $this->_getAuthorId();
  116. $sAuthorIp = $this->_getAuthorIp();
  117. if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
  118. $this->_trigger();
  119. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  120. $oZ->alert();
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. * Permissions functions
  127. *
  128. * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
  129. */
  130. public function isAllowedView($isPerformAction = false)
  131. {
  132. return false;
  133. }
  134. public function isAllowedViewView($isPerformAction = false)
  135. {
  136. if(isAdmin())
  137. return true;
  138. return $this->checkAction('view_view', $isPerformAction);
  139. }
  140. public function isAllowedViewViewViewers($isPerformAction = false)
  141. {
  142. $oAcl = BxDolAcl::getInstance();
  143. if(isAdmin() || $oAcl->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  144. return true;
  145. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($this->_iId);
  146. return $iObjectAuthorId != 0 && $iObjectAuthorId == $this->_getAuthorId() && $this->checkAction('view_view_viewers', $isPerformAction);
  147. }
  148. /**
  149. * Internal functions
  150. */
  151. protected function _getIconDo()
  152. {
  153. return 'eye';
  154. }
  155. protected function _getTitleDo()
  156. {
  157. return '_view_do_view';
  158. }
  159. }
  160. /** @} */