1
0

BxBaseModGroupsDb.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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(
  81. 'group_profile_id' => $iGroupProfileId,
  82. 'fan_id' => $iFanId,
  83. 'role' => BX_BASE_MOD_GROUPS_ROLE_COMMON
  84. );
  85. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id AND `role` <> :role";
  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'] . "` INNER JOIN `sys_profiles` as `p` ON (`p`.`id` = `fan_id` AND `p`.`status` = 'active') 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 'group_pids_by_fan_id':
  142. $aMethod['name'] = 'getColumn';
  143. $aMethod['params'][1] = [
  144. 'fan_id' => $aParams['fan_id'],
  145. ];
  146. $sSelectClause = "`ta`.`group_profile_id`";
  147. $sWhereClause = "AND `ta`.`fan_id` = :fan_id";
  148. if(!empty($aParams['role'])) {
  149. $aMethod['params'][1]['role'] = $aParams['role'];
  150. $sWhereClause .= " AND `ta`.`role` = :role";
  151. }
  152. break;
  153. case 'expired':
  154. $sWhereClause .= "AND `ta`.`added` < UNIX_TIMESTAMP() AND `ta`.`expired` <> 0 AND `ta`.`expired` < UNIX_TIMESTAMP()";
  155. break;
  156. }
  157. if(!empty($sOrderClause))
  158. $sOrderClause = "ORDER BY " . $sOrderClause;
  159. if(!empty($sLimitClause))
  160. $sLimitClause = "LIMIT " . $sLimitClause;
  161. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` AS `ta` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  162. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  163. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  164. }
  165. public function updateRoles($aSet, $aWhere)
  166. {
  167. $sWhereClause = 1;
  168. if(!empty($aWhere))
  169. $sWhereClause = $this->arrayToSQL($aWhere, ' AND ');
  170. return (int)$this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . $sWhereClause) > 0;
  171. }
  172. public function deleteRoles($aWhere)
  173. {
  174. if(empty($aWhere))
  175. return false;
  176. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  177. }
  178. public function setRole ($iGroupProfileId, $iProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  179. {
  180. $CNF = &$this->_oConfig->CNF;
  181. if($this->_oConfig->isMultiRoles() && is_array($mixedRole)) {
  182. if(count($mixedRole) != 1 || current($mixedRole) != 0) {
  183. $iProfileRole = 0;
  184. foreach($mixedRole as $iRole)
  185. $iProfileRole = $iProfileRole | pow(2, ($iRole - 1));
  186. $mixedRole = $iProfileRole;
  187. }
  188. else
  189. $mixedRole = 0;
  190. }
  191. $aBindings = array(
  192. 'group_profile_id' => $iGroupProfileId,
  193. 'fan_id' => $iProfileId,
  194. 'role' => $mixedRole,
  195. 'added' => time()
  196. );
  197. $sSetClause = "`group_profile_id` = :group_profile_id, `fan_id` = :fan_id, `role` = :role, `added` = :added";
  198. if(!empty($mixedPeriod)) {
  199. if(is_numeric($mixedPeriod))
  200. $mixedPeriod = array('period' => (int)$mixedPeriod, 'period_unit' => BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY);
  201. $aBindings['period'] = (int)$mixedPeriod['period'];
  202. $sSetExpired = "";
  203. switch($mixedPeriod['period_unit']) {
  204. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY:
  205. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK:
  206. if($mixedPeriod['period_unit'] == BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK)
  207. $aBindings['period'] *= 7;
  208. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period DAY)";
  209. break;
  210. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH:
  211. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period MONTH)";
  212. break;
  213. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR:
  214. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period YEAR)";
  215. break;
  216. }
  217. if(!empty($sSetExpired) && !empty($mixedPeriod['period_reserve'])) {
  218. $aBindings['reserve'] = (int)$mixedPeriod['period_reserve'];
  219. $sSetExpired = "DATE_ADD(" . $sSetExpired . ", INTERVAL :reserve DAY)";
  220. }
  221. if(!empty($sSetExpired))
  222. $sSetClause .= ", `expired` = UNIX_TIMESTAMP(" . $sSetExpired . ")";
  223. }
  224. if(!empty($sOrder)) {
  225. $aBindings['order'] = $sOrder;
  226. $sSetClause .= ", `order` = :order";
  227. }
  228. return !$this->query("REPLACE INTO `" . $CNF['TABLE_ADMINS'] . "` SET " . $sSetClause, $aBindings) ? false : true;
  229. }
  230. public function unsetRole($iGroupProfileId, $iProfileId)
  231. {
  232. return $this->deleteRoles(array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId));
  233. }
  234. public function getPrices($aParams)
  235. {
  236. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  237. $sSelectClause = "`tp`.*";
  238. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  239. if(!isset($aParams['order']) || empty($aParams['order']))
  240. $sOrderClause = "ORDER BY `tp`.`Order` ASC";
  241. switch($aParams['type']) {
  242. case 'by_id':
  243. $aMethod['name'] = 'getRow';
  244. $aMethod['params'][1] = array(
  245. 'id' => $aParams['value']
  246. );
  247. $sWhereClause .= "AND `tp`.`id`=:id";
  248. break;
  249. case 'by_name':
  250. $aMethod['name'] = 'getRow';
  251. $aMethod['params'][1] = array(
  252. 'id' => $aParams['value']
  253. );
  254. $sWhereClause .= "AND `tp`.`name`=:name";
  255. break;
  256. case 'by_profile_id':
  257. $aMethod['params'][1] = array(
  258. 'profile_id' => $aParams['profile_id']
  259. );
  260. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id";
  261. break;
  262. case 'by_prpp':
  263. $aMethod['name'] = 'getRow';
  264. $aMethod['params'][1] = array(
  265. 'profile_id' => $aParams['profile_id'],
  266. 'role_id' => $aParams['role_id'],
  267. 'period' => $aParams['period'],
  268. 'period_unit' => $aParams['period_unit'],
  269. );
  270. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id AND `tp`.`role_id`=:role_id AND `tp`.`period`=:period AND `tp`.`period_unit`=:period_unit";
  271. break;
  272. }
  273. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  274. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  275. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  276. }
  277. public function getPriceOrderMax($iRoleId)
  278. {
  279. return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `role_id`=:role_id", array(
  280. 'role_id' => $iRoleId
  281. ));
  282. }
  283. public function deletePrices($aWhere)
  284. {
  285. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ")) > 0;
  286. }
  287. public function insertInvite($sKey, $sGroupProfileId, $iAuthorProfileId, $iInvitedProfileId)
  288. {
  289. $aBindings = array(
  290. 'key' => $sKey,
  291. 'group_profile_id' => $sGroupProfileId,
  292. 'author_profile_id' => $iAuthorProfileId,
  293. 'invited_profile_id' => $iInvitedProfileId,
  294. 'added' => time()
  295. );
  296. $CNF = $this->_oConfig->CNF;
  297. $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);
  298. return (int)$this->lastId();
  299. }
  300. public function getInviteByKey($sKey, $iGroupProfileId)
  301. {
  302. $CNF = &$this->_oConfig->CNF;
  303. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", [
  304. 'key' => $sKey,
  305. 'group_profile_id' => $iGroupProfileId
  306. ]);
  307. }
  308. public function getInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  309. {
  310. $CNF = &$this->_oConfig->CNF;
  311. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `invited_profile_id` = :invited_profile_id AND group_profile_id = :group_profile_id", [
  312. 'invited_profile_id' => $iInvitedProfileId,
  313. 'group_profile_id' => $iGroupProfileId
  314. ]);
  315. }
  316. public function isInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  317. {
  318. $aInvite = $this->getInviteByInvited($iInvitedProfileId, $iGroupProfileId);
  319. return !empty($aInvite) && is_array($aInvite);
  320. }
  321. public function updateInviteByKey($sKey, $iGroupProfileId, $sColumn, $sValue)
  322. {
  323. $aBindings = array(
  324. 'key' => $sKey,
  325. 'value' => $sValue,
  326. 'group_profile_id' => $iGroupProfileId
  327. );
  328. $CNF = $this->_oConfig->CNF;
  329. return $this->query("UPDATE `" . $CNF["TABLE_INVITES"] . "` SET `" . $sColumn . "` = :value WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  330. }
  331. public function deleteInviteByKey($sKey, $iGroupProfileId)
  332. {
  333. $aBindings = array(
  334. 'key' => $sKey,
  335. 'group_profile_id' => $iGroupProfileId
  336. );
  337. $CNF = $this->_oConfig->CNF;
  338. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  339. }
  340. public function deleteInvite($iId)
  341. {
  342. $aBindings = array(
  343. 'id' => $iId
  344. );
  345. $CNF = $this->_oConfig->CNF;
  346. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `id` = :id", $aBindings);
  347. }
  348. }
  349. /** @} */