BxDolPrivacyQuery.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. bx_import('BxDolDb');
  10. class BxDolPrivacyQuery extends BxDolDb
  11. {
  12. protected $_aObject;
  13. protected $_sTable;
  14. protected $_sFieldId;
  15. protected $_sFieldOwnerId;
  16. protected $_sCachePrivacyObject;
  17. protected $_sCachePrivacyObjectDefault;
  18. protected $_sCacheGroup;
  19. protected $_sCacheGroupFriends;
  20. protected $_sCacheGroupsActive;
  21. protected $_sCacheTestedObject;
  22. public function __construct($aObject = array())
  23. {
  24. parent::__construct();
  25. $this->_aObject = $aObject;
  26. if(!empty($this->_aObject)) {
  27. $this->_sTable = $this->_aObject['table'];
  28. $this->_sFieldId = $this->_aObject['table_field_id'];
  29. $this->_sFieldOwnerId = $this->_aObject['table_field_author'];
  30. }
  31. $this->_sCachePrivacyObject = 'sys_privacy_object_';
  32. $this->_sCachePrivacyObjectDefault = 'sys_privacy_object_default_';
  33. $this->_sCacheGroup = 'sys_privacy_group_';
  34. $this->_sCacheGroupFriends = 'sys_privacy_group_friends_';
  35. $this->_sCacheGroupsActVis = 'sys_privacy_group_act_vis';
  36. $this->_sCacheTestedObject = 'sys_privacy_tested_object_';
  37. }
  38. static public function getPrivacyObject($sObject)
  39. {
  40. $oDb = BxDolDb::getInstance();
  41. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_privacy` WHERE `object` = ?", $sObject);
  42. $aObject = $oDb->getRow($sQuery);
  43. if(!$aObject || !is_array($aObject))
  44. return false;
  45. return $aObject;
  46. }
  47. function getObjectInfo($sAction, $iObjectId)
  48. {
  49. if(empty($this->_sTable) || empty($this->_sFieldId) || empty($this->_sFieldOwnerId))
  50. return array();
  51. $sQuery = $this->prepare("SELECT `" . $this->_sFieldOwnerId . "` AS `owner_id`, `" . $sAction . "` AS `group_id` FROM `" . $this->_sTable . "` WHERE `" . $this->_sFieldId . "`=? LIMIT 1", $iObjectId);
  52. $sCacheKey = $this->_sCacheTestedObject . $this->_sTable . '_' . $this->_aObject['action'] . '_' . $iObjectId;
  53. return $this->fromMemory($sCacheKey, 'getRow', $sQuery);
  54. }
  55. function getGroupsBy($aParams)
  56. {
  57. switch($aParams['type']) {
  58. case 'id':
  59. $sCacheFunction = 'fromCache';
  60. $sCacheName = $this->_sCacheGroup . $aParams['id'];
  61. $sMethod = 'getRow';
  62. $sWhereClause = $this->prepare("`id`=?", (int)$aParams['id']);
  63. break;
  64. case 'active':
  65. $sCacheFunction = 'fromMemory';
  66. $sCacheName = $this->_sCacheGroupsActVis;
  67. $sMethod = 'getAll';
  68. $sWhereClause = "`active`='1' AND `visible`='1'";
  69. break;
  70. }
  71. $sSql = "SELECT
  72. `id`,
  73. `title`,
  74. `check`,
  75. `active`
  76. FROM `sys_privacy_groups`
  77. WHERE " . $sWhereClause;
  78. return !empty($sCacheFunction) && !empty($sCacheName) ? $this->$sCacheFunction($sCacheName, $sMethod, $sSql) : $this->$sMethod($sSql);
  79. }
  80. function getActions($iOwnerId)
  81. {
  82. $sQuery = $this->prepare("SELECT
  83. `tm`.`uri` AS `module_uri`,
  84. `tm`.`title` AS `module_title`,
  85. `ta`.`id` AS `action_id`,
  86. `ta`.`title` AS `action_title`,
  87. `ta`.`default_group` AS `action_default_value`,
  88. `td`.`group_id` AS `default_value`
  89. FROM `sys_objects_privacy` AS `ta`
  90. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  91. INNER JOIN `sys_modules` AS `tm` ON `ta`.`module`=`tm`.`uri`
  92. WHERE 1
  93. ORDER BY `tm`.`title`", $iOwnerId);
  94. return $this->getAll($sQuery);
  95. }
  96. function getTitle($sModule, $sAction)
  97. {
  98. $aAction = $this->_getAction($sModule, $sAction);
  99. return !empty($aAction) && isset($aAction['title']) ? $aAction['title'] : '';
  100. }
  101. function getDefaultGroup($sModule, $sAction)
  102. {
  103. $aAction = $this->_getAction($sModule, $sAction);
  104. return !empty($aAction) && isset($aAction['default_group']) ? $aAction['default_group'] : BX_DOL_PG_DEFAULT;
  105. }
  106. function getDefaultGroupByUser($sModule, $sAction, $iOwnerId)
  107. {
  108. $sQuery = $this->prepare("SELECT
  109. `td`.`group_id`
  110. FROM `sys_objects_privacy` AS `ta`
  111. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  112. WHERE
  113. `ta`.`module`=? AND `ta`.`action`=?
  114. LIMIT 1", $iOwnerId, $sModule, $sAction);
  115. $sCacheKey = $this->_sCachePrivacyObjectDefault . $sModule . '_' . $sAction . '_' . $iOwnerId;
  116. return $this->fromMemory($sCacheKey, 'getOne', $sQuery);
  117. }
  118. function replaceDefaulfGroup($iActionId, $iOwnerId, $iGroupId)
  119. {
  120. $sSql = $this->prepare("REPLACE INTO
  121. `sys_privacy_defaults`
  122. SET
  123. `owner_id`=?,
  124. `action_id`=?,
  125. `group_id`=?", $iOwnerId, $iActionId, $iGroupId);
  126. return $this->query($sSql);
  127. }
  128. function getContentByGroupAsSQLPart($sField, $mixedGroupId)
  129. {
  130. if(is_array($mixedGroupId))
  131. $sWhere = " AND `" . $this->_sTable . "`.`" . $sField . "` IN (" . $this->implode_escape($mixedGroupId) . ")";
  132. else
  133. $sWhere = $this->prepare(" AND `" . $this->_sTable . "`.`" . $sField . "` = ?", $mixedGroupId);
  134. return array(
  135. 'where' => $sWhere
  136. );
  137. }
  138. protected function _getAction($sModule, $sAction)
  139. {
  140. $sQuery = $this->prepare("SELECT
  141. `id`,
  142. `module`,
  143. `action`,
  144. `title`,
  145. `default_group`
  146. FROM `sys_objects_privacy` AS `ta`
  147. WHERE `module`=? AND `action`=?
  148. LIMIT 1", $sModule, $sAction);
  149. $sCacheKey = $this->_sCachePrivacyObject . $sModule . '_' . $sAction;
  150. return $this->fromCache($sCacheKey, 'getRow', $sQuery);
  151. }
  152. }
  153. /** @} */