BxDolView.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * Track any object views automatically
  15. *
  16. * Add record to sys_object_view table to track object views,
  17. * to record view just create this class instance with your object id,
  18. * for example:
  19. * @code
  20. * BxDolView::getObjectInstance('my_system', 25); // 25 - is object id
  21. * @endcode
  22. *
  23. * Description of sys_object_view table fields:
  24. * @code
  25. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  26. * `table_track` - table to track views
  27. * `period` - period in secs to update next views, default is 86400(1 day)
  28. * `is_on` - is the system activated
  29. * `trigger_table` - table where you need to update views field
  30. * `trigger_field_id` - table field id to unique determine object
  31. * `trigger_field_count` - table field where total view number is stored
  32. * `class_name` - your custom class name, if you overrride default class
  33. * `class_file` - your custom class path
  34. * @endcode
  35. *
  36. * Structure of the track table is the following:
  37. * @code
  38. * CREATE TABLE `my_views_track` (
  39. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  40. * `viewer_id` int(11) NOT NULL default '0', -- viewer profile id
  41. * `viewer_nip` int(11) unsigned NOT NULL default '0', -- viewer ip address to track guest views
  42. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  43. * KEY `id` (`object_id`,`viewer_id`,`viewer_nip`)
  44. * );
  45. * @endcode
  46. *
  47. */
  48. class BxDolView extends BxDolObject
  49. {
  50. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  51. {
  52. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  53. if(empty($this->_sSystem))
  54. return;
  55. $this->_aSystem['per_page_default'] = 20;
  56. $this->_oQuery = new BxDolViewQuery($this);
  57. }
  58. /**
  59. * get votes object instanse
  60. * @param $sSys view object name
  61. * @param $iId associated content id, where vote is available
  62. * @param $iInit perform initialization
  63. * @return null on error, or ready to use class instance
  64. */
  65. public static function getObjectInstance($sSys, $iId, $iInit = true)
  66. {
  67. $sKey = 'BxDolView!' . $sSys . $iId;
  68. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  69. return $GLOBALS['bxDolClasses'][$sKey];
  70. $aSystems = self::getSystems();
  71. if(!isset($aSystems[$sSys]))
  72. return null;
  73. $sClassName = 'BxTemplView';
  74. if(!empty($aSystems[$sSys]['class_name'])) {
  75. $sClassName = $aSystems[$sSys]['class_name'];
  76. if(!empty($aSystems[$sSys]['class_file']))
  77. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  78. }
  79. $o = new $sClassName($sSys, $iId, $iInit);
  80. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  81. }
  82. public static function &getSystems()
  83. {
  84. $sKey = 'bx_dol_cache_memory_view_systems';
  85. if(!isset($GLOBALS[$sKey]))
  86. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_view', 'getAllWithKey', '
  87. SELECT
  88. `id` as `id`,
  89. `name` AS `name`,
  90. `module` AS `module`,
  91. `table_track` AS `table_track`,
  92. `period` AS `period`,
  93. `is_on` AS `is_on`,
  94. `trigger_table` AS `trigger_table`,
  95. `trigger_field_id` AS `trigger_field_id`,
  96. `trigger_field_author` AS `trigger_field_author`,
  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[$sKey];
  102. }
  103. public static function onAuthorDelete ($iAuthorId)
  104. {
  105. $aSystems = self::getSystems();
  106. foreach($aSystems as $sSystem => $aSystem)
  107. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  108. return true;
  109. }
  110. public function actionGetViewedBy()
  111. {
  112. if (!$this->isEnabled())
  113. return '';
  114. return $this->_getViewedBy();
  115. }
  116. public function actionGetUsers()
  117. {
  118. if (!$this->isEnabled())
  119. return echoJson(array());
  120. $iStart = (int)bx_get('start');
  121. $iPerPage = (int)bx_get('per_page');
  122. return echoJson(array(
  123. 'content' => $this->_getViewedBy($iStart, $iPerPage),
  124. 'eval' => $this->getJsObjectName() . '.onGetUsers(oData)'
  125. ));
  126. }
  127. public 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->_trigger();
  136. $oZ = new BxDolAlerts($this->_sSystem, 'view', $iObjectId, $iAuthorId);
  137. $oZ->alert();
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * Permissions functions
  144. *
  145. * Note. The 'view' action is performed automatically and therefore the manual action is not allowed.
  146. */
  147. public function isAllowedView($isPerformAction = false)
  148. {
  149. return false;
  150. }
  151. public function isAllowedViewView($isPerformAction = false)
  152. {
  153. if(isAdmin())
  154. return true;
  155. return $this->checkAction('view_view', $isPerformAction);
  156. }
  157. public function isAllowedViewViewViewers($isPerformAction = false)
  158. {
  159. $oAcl = BxDolAcl::getInstance();
  160. if(isAdmin() || $oAcl->isMemberLevelInSet(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR)))
  161. return true;
  162. if(!empty($this->_aSystem['module']) && BxDolRequest::serviceExists($this->_aSystem['module'], 'act_as_profile') && BxDolService::call($this->_aSystem['module'], 'act_as_profile') === true)
  163. $iObjectAuthorId = BxDolProfile::getInstanceByContentAndType($this->_iId, $this->_aSystem['module'])->id();
  164. else
  165. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($this->_iId);
  166. return $iObjectAuthorId != 0 && $iObjectAuthorId == $this->_getAuthorId() && $this->checkAction('view_view_viewers_own', $isPerformAction);
  167. }
  168. /**
  169. * Internal functions
  170. */
  171. protected function _getIconDo()
  172. {
  173. return 'eye';
  174. }
  175. protected function _getTitleDo()
  176. {
  177. return '_view_do_view';
  178. }
  179. }
  180. /** @} */