BxBaseModGroupsDb.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Groups module database queries
  13. */
  14. class BxBaseModGroupsDb extends BxBaseModProfileDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function updateAuthorById ($iContentId, $iProfileId)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_AUTHOR'] . "` = :author WHERE `" . $CNF['FIELD_ID'] . "` = :id";
  24. return $this->query($sQuery, array(
  25. 'id' => $iContentId,
  26. 'author' => $iProfileId,
  27. ));
  28. }
  29. public function toAdmins ($iGroupProfileId, $mixedFansIds)
  30. {
  31. if (is_array($mixedFansIds)) {
  32. foreach ($mixedFansIds as $iFanId)
  33. $this->toAdmins ($iFanId);
  34. return true;
  35. }
  36. $iFanId = (int)$mixedFansIds;
  37. $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET `group_profile_id` = ?, `fan_id` = ?", $iGroupProfileId, $iFanId);
  38. if (!$this->res($sQuery))
  39. return false;
  40. $oModule = BxDolModule::getInstance($this->_oConfig->getName());
  41. if ($oModule && method_exists($oModule, 'onFanAddedToAdmins'))
  42. $oModule->onFanAddedToAdmins($iGroupProfileId, $iFanId);
  43. $oModule->doAudit($iGroupProfileId, $iFanId, '_sys_audit_action_group_to_admins');
  44. return true;
  45. }
  46. public function fromAdmins ($iGroupProfileId, $mixedFansIds)
  47. {
  48. if (is_array($mixedFansIds)) {
  49. foreach ($mixedFansIds as $iFanId)
  50. $this->fromAdmins ($iGroupProfileId, $iFanId);
  51. return true;
  52. }
  53. $iFanId = (int)$mixedFansIds;
  54. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iFanId);
  55. if (!$this->res($sQuery))
  56. return false;
  57. $oModule = BxDolModule::getInstance($this->_oConfig->getName());
  58. if ($oModule && method_exists($oModule, 'onFanRemovedFromAdmins'))
  59. $oModule->onFanRemovedFromAdmins($iGroupProfileId, $iFanId);
  60. $oModule->doAudit($iGroupProfileId, $iFanId, '_sys_audit_action_group_from_admins');
  61. return true;
  62. }
  63. public function deleteAdminsByGroupId ($iGroupProfileId)
  64. {
  65. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ?", $iGroupProfileId);
  66. return $this->res($sQuery);
  67. }
  68. public function deleteAdminsByProfileId ($iProfileId)
  69. {
  70. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `fan_id` = ?", $iProfileId);
  71. return $this->res($sQuery);
  72. }
  73. public function isAdmin ($iGroupProfileId, $iFanId, $aDataEntry = array())
  74. {
  75. if (isset($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']]) && $iFanId == $aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']])
  76. return true;
  77. if (!isset($this->_oConfig->CNF['TABLE_ADMINS']))
  78. return false;
  79. $sQuery = $this->prepare("SELECT `id` FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iFanId);
  80. return $this->getOne($sQuery) ? true : false;
  81. }
  82. public function getAdmin ($iGroupProfileId, $iProfileId, $bRoleOnly = false)
  83. {
  84. $sMethod = 'getRow';
  85. $sSelectClause = '*';
  86. if($bRoleOnly) {
  87. $sMethod = 'getOne';
  88. $sSelectClause = '`role`';
  89. }
  90. return $this->$sMethod("SELECT " . $sSelectClause . " FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id LIMIT 1", array(
  91. 'group_profile_id' => $iGroupProfileId,
  92. 'fan_id' => $iProfileId
  93. ));
  94. }
  95. public function getAdmins ($iGroupProfileId, $iStart = 0, $iLimit = 0)
  96. {
  97. if ($iLimit > 0)
  98. $sQuery = $this->prepare("SELECT `fan_id` FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? LIMIT ?, ?", $iGroupProfileId, $iStart, $iLimit);
  99. else
  100. $sQuery = $this->prepare("SELECT `fan_id` FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ?", $iGroupProfileId);
  101. return $this->getColumn($sQuery);
  102. }
  103. public function getRole ($iGroupProfileId, $iProfileId)
  104. {
  105. return (int)$this->getOne("SELECT `role` FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id LIMIT 1", array(
  106. 'group_profile_id' => $iGroupProfileId,
  107. 'fan_id' => $iProfileId
  108. ));
  109. }
  110. public function setRole ($iGroupProfileId, $iProfileId, $iRole)
  111. {
  112. if($iRole == 0)
  113. $mixedResult = $this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id", array(
  114. 'group_profile_id' => $iGroupProfileId,
  115. 'fan_id' => $iProfileId,
  116. ));
  117. else
  118. $mixedResult = $this->query("REPLACE INTO `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET `group_profile_id` = :group_profile_id, `fan_id` = :fan_id, `role` = :role", array(
  119. 'group_profile_id' => $iGroupProfileId,
  120. 'fan_id' => $iProfileId,
  121. 'role' => $iRole
  122. ));
  123. return !$mixedResult ? false : true;
  124. }
  125. public function insertInvite($sKey, $sGroupProfileId, $iAuthorProfileId, $iInvitedProfileId)
  126. {
  127. $aBindings = array(
  128. 'key' => $sKey,
  129. 'group_profile_id' => $sGroupProfileId,
  130. 'author_profile_id' => $iAuthorProfileId,
  131. 'invited_profile_id' => $iInvitedProfileId,
  132. 'added' => time()
  133. );
  134. $CNF = $this->_oConfig->CNF;
  135. $this->query("INSERT `" . $CNF["TABLE_INVITES"] . "` (`key`, `group_profile_id`, `author_profile_id`, `invited_profile_id`, `added`) VALUES (:key, :group_profile_id, :author_profile_id, :invited_profile_id, :added)", $aBindings);
  136. return (int)$this->lastId();
  137. }
  138. public function getInviteByKey($sKey, $iGroupProfileId)
  139. {
  140. $aBindings = array(
  141. 'key' => $sKey,
  142. 'group_profile_id' => $iGroupProfileId
  143. );
  144. $CNF = $this->_oConfig->CNF;
  145. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  146. }
  147. public function getInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  148. {
  149. $aBindings = array(
  150. 'invited_profile_id' => $iInvitedProfileId,
  151. 'group_profile_id' => $iGroupProfileId
  152. );
  153. $CNF = $this->_oConfig->CNF;
  154. return $this->getOne("SELECT COUNT(*) FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `invited_profile_id` = :invited_profile_id AND group_profile_id = :group_profile_id", $aBindings);
  155. }
  156. public function updateInviteByKey($sKey, $iGroupProfileId, $sColumn, $sValue)
  157. {
  158. $aBindings = array(
  159. 'key' => $sKey,
  160. 'value' => $sValue,
  161. 'group_profile_id' => $iGroupProfileId
  162. );
  163. $CNF = $this->_oConfig->CNF;
  164. return $this->query("UPDATE `" . $CNF["TABLE_INVITES"] . "` SET `" . $sColumn . "` = :value WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  165. }
  166. public function deleteInviteByKey($sKey, $iGroupProfileId)
  167. {
  168. $aBindings = array(
  169. 'key' => $sKey,
  170. 'group_profile_id' => $iGroupProfileId
  171. );
  172. $CNF = $this->_oConfig->CNF;
  173. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  174. }
  175. public function deleteInvite($iId)
  176. {
  177. $aBindings = array(
  178. 'id' => $iId
  179. );
  180. $CNF = $this->_oConfig->CNF;
  181. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `id` = :id", $aBindings);
  182. }
  183. }
  184. /** @} */