BxBaseModGroupsDb.php 16 KB

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