Cheetah
ChWsbViews.php
Go to the documentation of this file.
1 <?php
2 
8 define ('CH_OLD_VIEWS', 3*86400); // views older than this number of seconds will be deleted automatically
9 
38 {
39  var $_iId = 0; // item id to be viewed
40  var $_sSystem = ''; // current view system name
41  var $_aSystem = array (); // current view system array
42 
46  function __construct($sSystem, $iId, $isMakeView = true)
47  {
48  $aSystems = $this->getAllSystems ();
49  if (!isset($aSystems[$sSystem]))
50  return;
51  $this->_aSystem = $aSystems[$sSystem];
52  $this->_sSystem = $sSystem;
53  if (!$this->isEnabled())
54  return;
55  $this->_iId = $iId;
56  if ($isMakeView)
57  $this->makeView();
58  }
59 
60  function makeView ()
61  {
62  if (!$this->isEnabled()) return false;
63 
65  $sIp = $_SERVER['REMOTE_ADDR'];
66  $iTime = time();
67 
68  if ($iMemberId)
69  $sWhere = " AND `viewer` = '$iMemberId' ";
70  else
71  $sWhere = " AND `viewer` = '0' AND `ip` = INET_ATON('$sIp') ";
72  $iTs = (int)($GLOBALS['MySQL']->getOne("SELECT `ts` FROM `{$this->_aSystem['table_track']}` WHERE `id` = '" . $this->getId() . "' $sWhere"));
73 
74  $iRet = 0;
75  if (!$iTs) {
76  $iRet = $GLOBALS['MySQL']->query("INSERT IGNORE INTO `{$this->_aSystem['table_track']}` SET `id` = '" . $this->getId() . "', `viewer` = '$iMemberId', `ip` = INET_ATON('$sIp'), `ts` = $iTime");
77  } elseif (($iTime - $iTs) > $this->_aSystem['period']) {
78  $iRet = $GLOBALS['MySQL']->query("UPDATE `{$this->_aSystem['table_track']}` SET `ts` = $iTime WHERE `id` = '" . $this->getId() . "' AND `viewer` = '$iMemberId' AND `ip` = INET_ATON('$sIp')");
79  }
80 
81  if ($iRet) {
82  $this->_triggerView();
83 
84  $oZ = new ChWsbAlerts($this->_sSystem, 'view', $this->getId(), $iMemberId);
85  $oZ->alert();
86 
87  return true;
88  }
89 
90  return false;
91  }
92 
93  function getId ()
94  {
95  return $this->_iId;
96  }
97 
98  function isEnabled ()
99  {
100  return $this->_aSystem && $this->_aSystem['is_on'];
101  }
102 
103  function getSystemName()
104  {
105  return $this->_sSystem;
106  }
107 
108  function getAllSystems ()
109  {
110  return $GLOBALS['MySQL']->fromCache('sys_objects_views', 'getAllWithKey', 'SELECT * FROM `sys_objects_views`', 'name');
111  }
112 
113  // call this function when associated object is deleted
114  function onObjectDelete ($iId = 0)
115  {
116  $iId = (int) $iId;
117  if( $GLOBALS['MySQL']->query("DELETE FROM `{$this->_aSystem['table_track']}`
118  WHERE `id` = '" . ($iId ? $iId : $this->getId()) . "'") ) {
119  $GLOBALS['MySQL']->query("OPTIMIZE TABLE `{$this->_aSystem['table_track']}`");
120  }
121  }
122 
123  // it is called on cron every day or similar period
124  function maintenance ()
125  {
126  $iTime = time() - CH_OLD_VIEWS;
127  $aSystems = $this->getAllSystems ();
128  $iDeletedRecords = 0;
129  foreach ($aSystems as $aSystem) {
130  if (!$aSystem['is_on'])
131  continue;
132  $iDeletedRecords += $GLOBALS['MySQL']->query("DELETE FROM `{$aSystem['table_track']}` WHERE `ts` < $iTime");
133  $GLOBALS['MySQL']->query("OPTIMIZE TABLE `{$aSystem['table_track']}`");
134  }
135  return $iDeletedRecords;
136  }
137 
138  function _triggerView()
139  {
140  return $GLOBALS['MySQL']->query("UPDATE `{$this->_aSystem['trigger_table']}` SET `{$this->_aSystem['trigger_field_views']}` = `{$this->_aSystem['trigger_field_views']}` + 1 WHERE `{$this->_aSystem['trigger_field_id']}` = '" . $this->getId() . "'");
141  }
142 }
ChWsbViews\$_aSystem
$_aSystem
Definition: ChWsbViews.php:41
$iMemberId
$iMemberId
Definition: profile.php:91
ChWsbViews\$_sSystem
$_sSystem
Definition: ChWsbViews.php:40
ChWsbViews\getAllSystems
getAllSystems()
Definition: ChWsbViews.php:108
$aSystems
$aSystems
Definition: cmts.php:20
ChWsbViews\getId
getId()
Definition: ChWsbViews.php:93
php
$oZ
$oZ
Definition: db.php:20
$iId
$iId
Definition: license.php:15
ChWsbViews\onObjectDelete
onObjectDelete($iId=0)
Definition: ChWsbViews.php:114
ChWsbViews\maintenance
maintenance()
Definition: ChWsbViews.php:124
ChWsbAlerts
Definition: ChWsbAlerts.php:39
getLoggedId
getLoggedId()
Definition: profiles.inc.php:32
ChWsbViews
Definition: ChWsbViews.php:38
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
ChWsbViews\__construct
__construct($sSystem, $iId, $isMakeView=true)
Definition: ChWsbViews.php:46
ChWsbViews\isEnabled
isEnabled()
Definition: ChWsbViews.php:98
ChWsbViews\getSystemName
getSystemName()
Definition: ChWsbViews.php:103
ChWsbViews\_triggerView
_triggerView()
Definition: ChWsbViews.php:138
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
CH_OLD_VIEWS
const CH_OLD_VIEWS
Definition: ChWsbViews.php:8
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ChWsbViews\$_iId
$_iId
Definition: ChWsbViews.php:39
ChWsbViews\makeView
makeView()
Definition: ChWsbViews.php:60