BxBaseModGroupsDb.php 17 KB

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