BxDolView.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 TridentCore Trident Core
  7. * @{
  8. */
  9. define ('BX_DOL_VIEW_OLD_VIEWS', 3 * 86400); ///< views older than this number of seconds will be deleted automatically
  10. /**
  11. * @page objects
  12. * @section views Views
  13. * @ref BxDolView
  14. */
  15. /**
  16. * Track any object views automatically
  17. *
  18. * Add record to sys_object_view table to track object views,
  19. * to record view just create this class instance with your object id,
  20. * for example:
  21. * @code
  22. * BxDolView::getObjectInstance('my_system', 25); // 25 - is object id
  23. * @endcode
  24. *
  25. * Description of sys_object_view table fields:
  26. * @code
  27. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  28. * `table_track` - table to track views
  29. * `period` - period in secs to update next views, default is 86400(1 day)
  30. * `is_on` - is the system activated
  31. * `trigger_table` - table where you need to update views field
  32. * `trigger_field_id` - table field id to unique determine object
  33. * `trigger_field_count` - table field where total view number is stored
  34. * `class_name` - your custom class name, if you overrride default class
  35. * `class_file` - your custom class path
  36. * @endcode
  37. *
  38. * Structure of the track table is the following:
  39. * @code
  40. * CREATE TABLE `my_views_track` (
  41. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  42. * `viewer_id` int(11) NOT NULL default '0', -- viewer profile id
  43. * `viewer_nip` int(11) unsigned NOT NULL default '0', -- viewer ip address to track guest views
  44. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  45. * KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
  46. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  47. * @endcode
  48. *
  49. */
  50. class BxDolView extends BxDolObject
  51. {
  52. /**
  53. * Constructor
  54. */
  55. function __construct($sSystem, $iId, $iInit = true)
  56. {
  57. parent::__construct($sSystem, $iId, $iInit);
  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_vote_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. function doView()
  123. {
  124. if(!$this->isEnabled())
  125. return false;
  126. $iObjectId = $this->getId();
  127. $iAuthorId = $this->_getAuthorId();
  128. $sAuthorIp = $this->_getAuthorIp();
  129. if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
  130. $this->_triggerView();
  131. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  132. $oZ->alert();
  133. return true;
  134. }
  135. return false;
  136. }
  137. function onObjectDelete($iObjectId = 0)
  138. {
  139. $this->_oQuery->deleteObjectViews($iObjectId ? $iObjectId : $this->getId());
  140. }
  141. protected function _getAuthorId ()
  142. {
  143. return isMember() ? bx_get_logged_profile_id() : 0;
  144. }
  145. protected function _getAuthorIp ()
  146. {
  147. return getVisitorIP();
  148. }
  149. protected function _triggerView()
  150. {
  151. if(!$this->_aSystem['trigger_table'])
  152. return false;
  153. $iId = $this->getId();
  154. if(!$iId)
  155. return false;
  156. return $this->_oQuery->updateTriggerTable($iId);
  157. }
  158. }
  159. /** @} */