BxBaseModGroupsDb.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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, $iRole, $mixedPeriod = false, $sOrder = '')
  175. {
  176. $CNF = &$this->_oConfig->CNF;
  177. $iNow = time();
  178. $aBindings = array(
  179. 'group_profile_id' => $iGroupProfileId,
  180. 'fan_id' => $iProfileId,
  181. 'role' => $iRole,
  182. 'order' => $sOrder,
  183. 'added' => $iNow
  184. );
  185. $sSetClause = "`group_profile_id` = :group_profile_id, `fan_id` = :fan_id, `role` = :role, `added` = :added";
  186. $sSetExpired = "";
  187. if(!empty($mixedPeriod)) {
  188. if(is_numeric($mixedPeriod))
  189. $mixedPeriod = array('period' => (int)$mixedPeriod, 'period_unit' => BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY);
  190. $aBindings['period'] = (int)$mixedPeriod['period'];
  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. return !$this->query("REPLACE INTO `" . $CNF['TABLE_ADMINS'] . "` SET " . $sSetClause, $aBindings) ? false : true;
  213. }
  214. public function unsetRole($iGroupProfileId, $iProfileId)
  215. {
  216. return $this->deleteRoles(array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId));
  217. }
  218. public function getPrices($aParams)
  219. {
  220. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  221. $sSelectClause = "`tp`.*";
  222. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  223. if(!isset($aParams['order']) || empty($aParams['order']))
  224. $sOrderClause = "ORDER BY `tp`.`Order` ASC";
  225. switch($aParams['type']) {
  226. case 'by_id':
  227. $aMethod['name'] = 'getRow';
  228. $aMethod['params'][1] = array(
  229. 'id' => $aParams['value']
  230. );
  231. $sWhereClause .= "AND `tp`.`id`=:id";
  232. break;
  233. case 'by_name':
  234. $aMethod['name'] = 'getRow';
  235. $aMethod['params'][1] = array(
  236. 'id' => $aParams['value']
  237. );
  238. $sWhereClause .= "AND `tp`.`name`=:name";
  239. break;
  240. case 'by_profile_id':
  241. $aMethod['params'][1] = array(
  242. 'profile_id' => $aParams['profile_id']
  243. );
  244. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id";
  245. break;
  246. case 'by_prpp':
  247. $aMethod['name'] = 'getRow';
  248. $aMethod['params'][1] = array(
  249. 'profile_id' => $aParams['profile_id'],
  250. 'role_id' => $aParams['role_id'],
  251. 'period' => $aParams['period'],
  252. 'period_unit' => $aParams['period_unit'],
  253. );
  254. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id AND `tp`.`role_id`=:role_id AND `tp`.`period`=:period AND `tp`.`period_unit`=:period_unit";
  255. break;
  256. }
  257. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  258. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  259. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  260. }
  261. public function getPriceOrderMax($iRoleId)
  262. {
  263. return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `role_id`=:role_id", array(
  264. 'role_id' => $iRoleId
  265. ));
  266. }
  267. public function deletePrices($aWhere)
  268. {
  269. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ")) > 0;
  270. }
  271. public function insertInvite($sKey, $sGroupProfileId, $iAuthorProfileId, $iInvitedProfileId)
  272. {
  273. $aBindings = array(
  274. 'key' => $sKey,
  275. 'group_profile_id' => $sGroupProfileId,
  276. 'author_profile_id' => $iAuthorProfileId,
  277. 'invited_profile_id' => $iInvitedProfileId,
  278. 'added' => time()
  279. );
  280. $CNF = $this->_oConfig->CNF;
  281. $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);
  282. return (int)$this->lastId();
  283. }
  284. public function getInviteByKey($sKey, $iGroupProfileId)
  285. {
  286. $aBindings = array(
  287. 'key' => $sKey,
  288. 'group_profile_id' => $iGroupProfileId
  289. );
  290. $CNF = $this->_oConfig->CNF;
  291. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  292. }
  293. public function getInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  294. {
  295. $aBindings = array(
  296. 'invited_profile_id' => $iInvitedProfileId,
  297. 'group_profile_id' => $iGroupProfileId
  298. );
  299. $CNF = $this->_oConfig->CNF;
  300. return $this->getOne("SELECT COUNT(*) FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `invited_profile_id` = :invited_profile_id AND group_profile_id = :group_profile_id", $aBindings);
  301. }
  302. public function updateInviteByKey($sKey, $iGroupProfileId, $sColumn, $sValue)
  303. {
  304. $aBindings = array(
  305. 'key' => $sKey,
  306. 'value' => $sValue,
  307. 'group_profile_id' => $iGroupProfileId
  308. );
  309. $CNF = $this->_oConfig->CNF;
  310. return $this->query("UPDATE `" . $CNF["TABLE_INVITES"] . "` SET `" . $sColumn . "` = :value WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  311. }
  312. public function deleteInviteByKey($sKey, $iGroupProfileId)
  313. {
  314. $aBindings = array(
  315. 'key' => $sKey,
  316. 'group_profile_id' => $iGroupProfileId
  317. );
  318. $CNF = $this->_oConfig->CNF;
  319. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  320. }
  321. public function deleteInvite($iId)
  322. {
  323. $aBindings = array(
  324. 'id' => $iId
  325. );
  326. $CNF = $this->_oConfig->CNF;
  327. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `id` = :id", $aBindings);
  328. }
  329. }
  330. /** @} */