BxBaseModGroupsDb.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 ($iGroupProfileId, $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` = ?, `role` = ?", $iGroupProfileId, $iFanId, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR);
  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. $CNF = &$this->_oConfig->CNF;
  76. if(isset($aDataEntry[$CNF['FIELD_AUTHOR']]) && $iFanId == $aDataEntry[$CNF['FIELD_AUTHOR']])
  77. return true;
  78. if(!isset($CNF['TABLE_ADMINS']))
  79. return false;
  80. $aBindings = array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iFanId);
  81. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id";
  82. if($this->_oConfig->isPaidJoin()) {
  83. $aBindings['role'] = 'BX_BASE_MOD_GROUPS_ROLE_COMMON';
  84. $sWhereClause .= " AND `role` <> :role";
  85. }
  86. return $this->getOne("SELECT `id` FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause, $aBindings) ? true : false;
  87. }
  88. public function getAdmin ($iGroupProfileId, $iProfileId, $bRoleOnly = false)
  89. {
  90. $CNF = &$this->_oConfig->CNF;
  91. $sMethod = 'getRow';
  92. $sSelectClause = '*';
  93. if($bRoleOnly) {
  94. $sMethod = 'getOne';
  95. $sSelectClause = '`role`';
  96. }
  97. $aBindings = array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId, 'role' => BX_BASE_MOD_GROUPS_ROLE_COMMON);
  98. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id AND `role` <> :role";
  99. return $this->$sMethod("SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause . " LIMIT 1", $aBindings);
  100. }
  101. public function getAdmins ($iGroupProfileId, $iStart = 0, $iLimit = 0)
  102. {
  103. $CNF = &$this->_oConfig->CNF;
  104. $aBindings = array('group_profile_id' => $iGroupProfileId, 'role' => BX_BASE_MOD_GROUPS_ROLE_COMMON);
  105. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `role` <> :role";
  106. $sLimitClause = "";
  107. if($iLimit > 0)
  108. $sLimitClause = " LIMIT " . (int)$iStart . ", " . (int)$iLimit;
  109. return $this->getColumn("SELECT `fan_id` FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause . $sLimitClause, $aBindings);
  110. }
  111. public function getRole($iGroupProfileId, $iProfileId)
  112. {
  113. return (int)$this->getRoles(array('type' => 'by_gf_id', 'group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId, 'role_only' => true));
  114. }
  115. public function getRoles($aParams)
  116. {
  117. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  118. $sSelectClause = "`ta`.*";
  119. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  120. switch($aParams['type']) {
  121. case 'by_id':
  122. $aMethod['name'] = 'getRow';
  123. $aMethod['params'][1] = array(
  124. 'id' => $aParams['value']
  125. );
  126. $sWhereClause = "AND `ta`.`id`=:id";
  127. break;
  128. case 'by_gf_id':
  129. $aMethod['name'] = 'getRow';
  130. $aMethod['params'][1] = array(
  131. 'group_profile_id' => $aParams['group_profile_id'],
  132. 'fan_id' => $aParams['fan_id'],
  133. );
  134. $sWhereClause = "AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id";
  135. $sLimitClause = 1;
  136. if(!empty($aParams['role_only'])) {
  137. $aMethod['name'] = 'getOne';
  138. $sSelectClause = "`ta`.`role`";
  139. }
  140. break;
  141. case 'expired':
  142. $sWhereClause .= "AND `ta`.`added` < UNIX_TIMESTAMP() AND `ta`.`expired` <> 0 AND `ta`.`expired` < UNIX_TIMESTAMP()";
  143. break;
  144. }
  145. if(!empty($sOrderClause))
  146. $sOrderClause = "ORDER BY " . $sOrderClause;
  147. if(!empty($sLimitClause))
  148. $sLimitClause = "LIMIT " . $sLimitClause;
  149. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` AS `ta` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  150. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  151. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  152. }
  153. public function updateRoles($aSet, $aWhere)
  154. {
  155. $sWhereClause = 1;
  156. if(!empty($aWhere))
  157. $sWhereClause = $this->arrayToSQL($aWhere, ' AND ');
  158. return (int)$this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . $sWhereClause) > 0;
  159. }
  160. public function deleteRoles($aWhere)
  161. {
  162. if(empty($aWhere))
  163. return false;
  164. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  165. }
  166. public function setRole ($iGroupProfileId, $iProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  167. {
  168. $CNF = &$this->_oConfig->CNF;
  169. if($this->_oConfig->isMultiRoles() && is_array($mixedRole)) {
  170. if(count($mixedRole) != 1 || current($mixedRole) != 0) {
  171. $iProfileRole = 0;
  172. foreach($mixedRole as $iRole)
  173. $iProfileRole = $iProfileRole | pow(2, ($iRole - 1));
  174. $mixedRole = $iProfileRole;
  175. }
  176. else
  177. $mixedRole = 0;
  178. }
  179. $aBindings = array(
  180. 'group_profile_id' => $iGroupProfileId,
  181. 'fan_id' => $iProfileId,
  182. 'role' => $mixedRole,
  183. 'added' => time()
  184. );
  185. $sSetClause = "`group_profile_id` = :group_profile_id, `fan_id` = :fan_id, `role` = :role, `added` = :added";
  186. if(!empty($mixedPeriod)) {
  187. if(is_numeric($mixedPeriod))
  188. $mixedPeriod = array('period' => (int)$mixedPeriod, 'period_unit' => BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY);
  189. $aBindings['period'] = (int)$mixedPeriod['period'];
  190. $sSetExpired = "";
  191. switch($mixedPeriod['period_unit']) {
  192. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY:
  193. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK:
  194. if($mixedPeriod['period_unit'] == BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK)
  195. $aBindings['period'] *= 7;
  196. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period DAY)";
  197. break;
  198. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH:
  199. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period MONTH)";
  200. break;
  201. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR:
  202. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period YEAR)";
  203. break;
  204. }
  205. if(!empty($sSetExpired) && !empty($mixedPeriod['period_reserve'])) {
  206. $aBindings['reserve'] = (int)$mixedPeriod['period_reserve'];
  207. $sSetExpired = "DATE_ADD(" . $sSetExpired . ", INTERVAL :reserve DAY)";
  208. }
  209. if(!empty($sSetExpired))
  210. $sSetClause .= ", `expired` = UNIX_TIMESTAMP(" . $sSetExpired . ")";
  211. }
  212. if(!empty($sOrder)) {
  213. $aBindings['order'] = $sOrder;
  214. $sSetClause .= ", `order` = :order";
  215. }
  216. return !$this->query("REPLACE INTO `" . $CNF['TABLE_ADMINS'] . "` SET " . $sSetClause, $aBindings) ? false : true;
  217. }
  218. public function unsetRole($iGroupProfileId, $iProfileId)
  219. {
  220. return $this->deleteRoles(array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId));
  221. }
  222. public function getPrices($aParams)
  223. {
  224. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  225. $sSelectClause = "`tp`.*";
  226. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  227. if(!isset($aParams['order']) || empty($aParams['order']))
  228. $sOrderClause = "ORDER BY `tp`.`Order` ASC";
  229. switch($aParams['type']) {
  230. case 'by_id':
  231. $aMethod['name'] = 'getRow';
  232. $aMethod['params'][1] = array(
  233. 'id' => $aParams['value']
  234. );
  235. $sWhereClause .= "AND `tp`.`id`=:id";
  236. break;
  237. case 'by_name':
  238. $aMethod['name'] = 'getRow';
  239. $aMethod['params'][1] = array(
  240. 'id' => $aParams['value']
  241. );
  242. $sWhereClause .= "AND `tp`.`name`=:name";
  243. break;
  244. case 'by_profile_id':
  245. $aMethod['params'][1] = array(
  246. 'profile_id' => $aParams['profile_id']
  247. );
  248. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id";
  249. break;
  250. case 'by_prpp':
  251. $aMethod['name'] = 'getRow';
  252. $aMethod['params'][1] = array(
  253. 'profile_id' => $aParams['profile_id'],
  254. 'role_id' => $aParams['role_id'],
  255. 'period' => $aParams['period'],
  256. 'period_unit' => $aParams['period_unit'],
  257. );
  258. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id AND `tp`.`role_id`=:role_id AND `tp`.`period`=:period AND `tp`.`period_unit`=:period_unit";
  259. break;
  260. }
  261. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  262. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  263. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  264. }
  265. public function getPriceOrderMax($iRoleId)
  266. {
  267. return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `role_id`=:role_id", array(
  268. 'role_id' => $iRoleId
  269. ));
  270. }
  271. public function deletePrices($aWhere)
  272. {
  273. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ")) > 0;
  274. }
  275. public function insertInvite($sKey, $sGroupProfileId, $iAuthorProfileId, $iInvitedProfileId)
  276. {
  277. $aBindings = array(
  278. 'key' => $sKey,
  279. 'group_profile_id' => $sGroupProfileId,
  280. 'author_profile_id' => $iAuthorProfileId,
  281. 'invited_profile_id' => $iInvitedProfileId,
  282. 'added' => time()
  283. );
  284. $CNF = $this->_oConfig->CNF;
  285. $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);
  286. return (int)$this->lastId();
  287. }
  288. public function getInviteByKey($sKey, $iGroupProfileId)
  289. {
  290. $aBindings = array(
  291. 'key' => $sKey,
  292. 'group_profile_id' => $iGroupProfileId
  293. );
  294. $CNF = $this->_oConfig->CNF;
  295. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  296. }
  297. public function getInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  298. {
  299. $aBindings = array(
  300. 'invited_profile_id' => $iInvitedProfileId,
  301. 'group_profile_id' => $iGroupProfileId
  302. );
  303. $CNF = $this->_oConfig->CNF;
  304. return $this->getOne("SELECT COUNT(*) FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `invited_profile_id` = :invited_profile_id AND group_profile_id = :group_profile_id", $aBindings);
  305. }
  306. public function updateInviteByKey($sKey, $iGroupProfileId, $sColumn, $sValue)
  307. {
  308. $aBindings = array(
  309. 'key' => $sKey,
  310. 'value' => $sValue,
  311. 'group_profile_id' => $iGroupProfileId
  312. );
  313. $CNF = $this->_oConfig->CNF;
  314. return $this->query("UPDATE `" . $CNF["TABLE_INVITES"] . "` SET `" . $sColumn . "` = :value WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  315. }
  316. public function deleteInviteByKey($sKey, $iGroupProfileId)
  317. {
  318. $aBindings = array(
  319. 'key' => $sKey,
  320. 'group_profile_id' => $iGroupProfileId
  321. );
  322. $CNF = $this->_oConfig->CNF;
  323. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  324. }
  325. public function deleteInvite($iId)
  326. {
  327. $aBindings = array(
  328. 'id' => $iId
  329. );
  330. $CNF = $this->_oConfig->CNF;
  331. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `id` = :id", $aBindings);
  332. }
  333. }
  334. /** @} */