BxDolPrivacyQuery.php 6.0 KB

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