BxDolView.php 5.9 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. * @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_count` AS `trigger_field_count`,
  98. `class_name` AS `class_name`,
  99. `class_file` AS `class_file`
  100. FROM `sys_objects_view`', 'name');
  101. return $GLOBALS['bx_dol_view_systems'];
  102. }
  103. /**
  104. * it is called on cron every day or similar period to clean old votes
  105. */
  106. public static function maintenance()
  107. {
  108. $iResult = 0;
  109. $oDb = BxDolDb::getInstance();
  110. $aSystems = self::getSystems();
  111. foreach($aSystems as $aSystem) {
  112. if(!$aSystem['is_on'])
  113. continue;
  114. $sQuery = $oDb->prepare("DELETE FROM `{$aSystem['table_track']}` WHERE `date` < (UNIX_TIMESTAMP() - ?)", BX_DOL_VIEW_OLD_VIEWS);
  115. $iDeleted = (int)$oDb->query($sQuery);
  116. if($iDeleted > 0)
  117. $oDb->query("OPTIMIZE TABLE `{$aSystem['table_track']}`");
  118. $iResult += $iDeleted;
  119. }
  120. return $iResult;
  121. }
  122. public function actionGetViewedBy()
  123. {
  124. if (!$this->isEnabled())
  125. return '';
  126. return $this->_getViewedBy();
  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. /**
  153. * Internal functions
  154. */
  155. protected function _getIconDo()
  156. {
  157. return 'eye';
  158. }
  159. protected function _getTitleDo()
  160. {
  161. return '_view_do_view';
  162. }
  163. }
  164. /** @} */