| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- define('BX_DOL_VIEW_OLD_VIEWS', 3 * 86400); ///< views older than this number of seconds will be deleted automatically
- define('BX_DOL_VIEW_USAGE_BLOCK', 'block');
- define('BX_DOL_VIEW_USAGE_INLINE', 'inline');
- define('BX_DOL_VIEW_USAGE_DEFAULT', BX_DOL_VIEW_USAGE_BLOCK);
- /**
- * @page objects
- * @section views Views
- * @ref BxDolView
- */
- /**
- * Track any object views automatically
- *
- * Add record to sys_object_view table to track object views,
- * to record view just create this class instance with your object id,
- * for example:
- * @code
- * BxDolView::getObjectInstance('my_system', 25); // 25 - is object id
- * @endcode
- *
- * Description of sys_object_view table fields:
- * @code
- * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
- * `table_track` - table to track views
- * `period` - period in secs to update next views, default is 86400(1 day)
- * `is_on` - is the system activated
- * `trigger_table` - table where you need to update views field
- * `trigger_field_id` - table field id to unique determine object
- * `trigger_field_count` - table field where total view number is stored
- * `class_name` - your custom class name, if you overrride default class
- * `class_file` - your custom class path
- * @endcode
- *
- * Structure of the track table is the following:
- * @code
- * CREATE TABLE `my_views_track` (
- * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
- * `viewer_id` int(11) NOT NULL default '0', -- viewer profile id
- * `viewer_nip` int(11) unsigned NOT NULL default '0', -- viewer ip address to track guest views
- * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
- * KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
- * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- * @endcode
- *
- */
- class BxDolView extends BxDolObject
- {
- protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
- {
- parent::__construct($sSystem, $iId, $iInit, $oTemplate);
- if(empty($this->_sSystem))
- return;
- $this->_oQuery = new BxDolViewQuery($this);
- }
- /**
- * get votes object instanse
- * @param $sSys view object name
- * @param $iId associated content id, where vote is available
- * @param $iInit perform initialization
- * @return null on error, or ready to use class instance
- */
- public static function getObjectInstance($sSys, $iId, $iInit = true)
- {
- if(isset($GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId]))
- return $GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId];
- $aSystems = self::getSystems();
- if(!isset($aSystems[$sSys]))
- return null;
- $sClassName = 'BxTemplView';
- if(!empty($aSystems[$sSys]['class_name'])) {
- $sClassName = $aSystems[$sSys]['class_name'];
- if(!empty($aSystems[$sSys]['class_file']))
- require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
- }
- $o = new $sClassName($sSys, $iId, $iInit);
- return ($GLOBALS['bxDolClasses']['BxDolView!' . $sSys . $iId] = $o);
- }
- public static function &getSystems()
- {
- if(!isset($GLOBALS['bx_dol_view_systems']))
- $GLOBALS['bx_dol_view_systems'] = BxDolDb::getInstance()->fromCache('sys_objects_view', 'getAllWithKey', '
- SELECT
- `id` as `id`,
- `name` AS `name`,
- `table_track` AS `table_track`,
- `period` AS `period`,
- `is_on` AS `is_on`,
- `trigger_table` AS `trigger_table`,
- `trigger_field_id` AS `trigger_field_id`,
- `trigger_field_count` AS `trigger_field_count`,
- `class_name` AS `class_name`,
- `class_file` AS `class_file`
- FROM `sys_objects_view`', 'name');
- return $GLOBALS['bx_dol_view_systems'];
- }
- /**
- * it is called on cron every day or similar period to clean old votes
- */
- public static function maintenance()
- {
- $iResult = 0;
- $oDb = BxDolDb::getInstance();
- $aSystems = self::getSystems();
- foreach($aSystems as $aSystem) {
- if(!$aSystem['is_on'])
- continue;
- $sQuery = $oDb->prepare("DELETE FROM `{$aSystem['table_track']}` WHERE `date` < (UNIX_TIMESTAMP() - ?)", BX_DOL_VIEW_OLD_VIEWS);
- $iDeleted = (int)$oDb->query($sQuery);
- if($iDeleted > 0)
- $oDb->query("OPTIMIZE TABLE `{$aSystem['table_track']}`");
- $iResult += $iDeleted;
- }
- return $iResult;
- }
- public function actionGetViewedBy()
- {
- if (!$this->isEnabled())
- return '';
- return $this->_getViewedBy();
- }
- public function doView()
- {
- if(!$this->isEnabled())
- return false;
- $iObjectId = $this->getId();
- $iAuthorId = $this->_getAuthorId();
- $sAuthorIp = $this->_getAuthorIp();
- if($this->_oQuery->doView($iObjectId, $iAuthorId, $sAuthorIp)) {
- $this->_trigger();
- $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
- $oZ->alert();
- return true;
- }
- return false;
- }
- /**
- * Permissions functions
- *
- * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
- */
- public function isAllowedView($isPerformAction = false)
- {
- return false;
- }
- /**
- * Internal functions
- */
- protected function _getIconDo()
- {
- return 'eye';
- }
- protected function _getTitleDo()
- {
- return '_view_do_view';
- }
- }
- /** @} */
|