BxDolView.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolObject');
  10. bx_import('BxDolViewQuery');
  11. define ('BX_DOL_VIEW_OLD_VIEWS', 3 * 86400); ///< views older than this number of seconds will be deleted automatically
  12. /**
  13. * @page objects
  14. * @section views Views
  15. * @ref BxDolView
  16. */
  17. /**
  18. * Track any object views automatically
  19. *
  20. * Add record to sys_object_view table to track object views,
  21. * to record view just create this class instance with your object id,
  22. * for example:
  23. * @code
  24. * BxDolView::getObjectInstance('my_system', 25); // 25 - is object id
  25. * @endcode
  26. *
  27. * Description of sys_object_view table fields:
  28. * @code
  29. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  30. * `table_track` - table to track views
  31. * `period` - period in secs to update next views, default is 86400(1 day)
  32. * `is_on` - is the system activated
  33. * `trigger_table` - table where you need to update views field
  34. * `trigger_field_id` - table field id to unique determine object
  35. * `trigger_field_count` - table field where total view number is stored
  36. * `class_name` - your custom class name, if you overrride default class
  37. * `class_file` - your custom class path
  38. * @endcode
  39. *
  40. * Structure of the track table is the following:
  41. * @code
  42. * CREATE TABLE `my_views_track` (
  43. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  44. * `viewer_id` int(11) NOT NULL default '0', -- viewer profile id
  45. * `viewer_nip` int(11) unsigned NOT NULL default '0', -- viewer ip address to track guest views
  46. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  47. * KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
  48. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  49. * @endcode
  50. *
  51. */
  52. class BxDolView extends BxDolObject
  53. {
  54. /**
  55. * Constructor
  56. */
  57. function __construct($sSystem, $iId, $iInit = true)
  58. {
  59. parent::__construct($sSystem, $iId, $iInit);
  60. if(empty($this->_sSystem))
  61. return;
  62. $this->_oQuery = new BxDolViewQuery($this);
  63. }
  64. /**
  65. * get votes object instanse
  66. * @param $sSys view object name
  67. * @param $iId associated content id, where vote is available
  68. * @param $iInit perform initialization
  69. * @return null on error, or ready to use class instance
  70. */
  71. public static function getObjectInstance($sSys, $iId, $iInit = true)
  72. {
  73. if(isset($GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId]))
  74. return $GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId];
  75. $aSystems = self::getSystems();
  76. if(!isset($aSystems[$sSys]))
  77. return null;
  78. bx_import('BxTemplView');
  79. $sClassName = 'BxTemplView';
  80. if(!empty($aSystems[$sSys]['class_name'])) {
  81. $sClassName = $aSystems[$sSys]['class_name'];
  82. if(!empty($aSystems[$sSys]['class_file']))
  83. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  84. else
  85. bx_import($sClassName);
  86. }
  87. $o = new $sClassName($sSys, $iId, $iInit);
  88. return ($GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId] = $o);
  89. }
  90. public static function &getSystems()
  91. {
  92. if(!isset($GLOBALS['bx_dol_vote_systems']))
  93. $GLOBALS['bx_dol_view_systems'] = BxDolDb::getInstance()->fromCache('sys_objects_view', 'getAllWithKey', '
  94. SELECT
  95. `id` as `id`,
  96. `name` AS `name`,
  97. `table_track` AS `table_track`,
  98. `period` AS `period`,
  99. `is_on` AS `is_on`,
  100. `trigger_table` AS `trigger_table`,
  101. `trigger_field_id` AS `trigger_field_id`,
  102. `trigger_field_count` AS `trigger_field_count`,
  103. `class_name` AS `class_name`,
  104. `class_file` AS `class_file`
  105. FROM `sys_objects_view`', 'name');
  106. return $GLOBALS['bx_dol_view_systems'];
  107. }
  108. /**
  109. * it is called on cron every day or similar period to clean old votes
  110. */
  111. public static function maintenance()
  112. {
  113. $iResult = 0;
  114. $oDb = BxDolDb::getInstance();
  115. $aSystems = self::getSystems();
  116. foreach($aSystems as $aSystem) {
  117. if(!$aSystem['is_on'])
  118. continue;
  119. $sQuery = $oDb->prepare("DELETE FROM `{$aSystem['table_track']}` WHERE `date` < (UNIX_TIMESTAMP() - ?)", BX_DOL_VIEW_OLD_VIEWS);
  120. $iDeleted = (int)$oDb->query($sQuery);
  121. if($iDeleted > 0)
  122. $oDb->query("OPTIMIZE TABLE `{$aSystem['table_track']}`");
  123. $iResult += $iDeleted;
  124. }
  125. return $iResult;
  126. }
  127. function doView()
  128. {
  129. if(!$this->isEnabled())
  130. return false;
  131. $iObjectId = $this->getId();
  132. $iAuthorId = $this->_getAuthorId();
  133. $sAuthorIp = $this->_getAuthorIp();
  134. if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
  135. $this->_triggerView();
  136. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  137. $oZ->alert();
  138. return true;
  139. }
  140. return false;
  141. }
  142. function onObjectDelete($iObjectId = 0)
  143. {
  144. $this->_oQuery->deleteObjectViews($iObjectId ? $iObjectId : $this->getId());
  145. }
  146. protected function _getAuthorId ()
  147. {
  148. return isMember() ? bx_get_logged_profile_id() : 0;
  149. }
  150. protected function _getAuthorIp ()
  151. {
  152. return getVisitorIP();
  153. }
  154. protected function _triggerView()
  155. {
  156. if(!$this->_aSystem['trigger_table'])
  157. return false;
  158. $iId = $this->getId();
  159. if(!$iId)
  160. return false;
  161. return $this->_oQuery->updateTriggerTable($iId);
  162. }
  163. }
  164. /** @} */