| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup BaseGroups Base classes for groups modules
- * @ingroup UnaModules
- *
- * @{
- */
- /*
- * Groups module database queries
- */
- class BxBaseModGroupsDb extends BxBaseModProfileDb
- {
- public function __construct(&$oConfig)
- {
- parent::__construct($oConfig);
- }
- public function updateAuthorById ($iContentId, $iProfileId)
- {
- $CNF = &$this->_oConfig->CNF;
- $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_AUTHOR'] . "` = :author WHERE `" . $CNF['FIELD_ID'] . "` = :id";
- return $this->query($sQuery, array(
- 'id' => $iContentId,
- 'author' => $iProfileId,
- ));
- }
- public function toAdmins ($iGroupProfileId, $mixedFansIds)
- {
- if (is_array($mixedFansIds)) {
- foreach ($mixedFansIds as $iFanId)
- $this->toAdmins ($iFanId);
- return true;
- }
- $iFanId = (int)$mixedFansIds;
- $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET `group_profile_id` = ?, `fan_id` = ?", $iGroupProfileId, $iFanId);
- if (!$this->res($sQuery))
- return false;
- $oModule = BxDolModule::getInstance($this->_oConfig->getName());
- if ($oModule && method_exists($oModule, 'onFanAddedToAdmins'))
- $oModule->onFanAddedToAdmins($iGroupProfileId, $iFanId);
- $oModule->doAudit($iGroupProfileId, $iFanId, '_sys_audit_action_group_to_admins');
-
- return true;
- }
- public function fromAdmins ($iGroupProfileId, $mixedFansIds)
- {
- if (is_array($mixedFansIds)) {
- foreach ($mixedFansIds as $iFanId)
- $this->fromAdmins ($iGroupProfileId, $iFanId);
- return true;
- }
- $iFanId = (int)$mixedFansIds;
- $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iFanId);
- if (!$this->res($sQuery))
- return false;
- $oModule = BxDolModule::getInstance($this->_oConfig->getName());
- if ($oModule && method_exists($oModule, 'onFanRemovedFromAdmins'))
- $oModule->onFanRemovedFromAdmins($iGroupProfileId, $iFanId);
-
- $oModule->doAudit($iGroupProfileId, $iFanId, '_sys_audit_action_group_from_admins');
-
- return true;
- }
- public function deleteAdminsByGroupId ($iGroupProfileId)
- {
- $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ?", $iGroupProfileId);
- return $this->res($sQuery);
- }
- public function deleteAdminsByProfileId ($iProfileId)
- {
- $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `fan_id` = ?", $iProfileId);
- return $this->res($sQuery);
- }
- public function isAdmin ($iGroupProfileId, $iFanId, $aDataEntry = array())
- {
- $CNF = &$this->_oConfig->CNF;
- if(isset($aDataEntry[$CNF['FIELD_AUTHOR']]) && $iFanId == $aDataEntry[$CNF['FIELD_AUTHOR']])
- return true;
- if(!isset($CNF['TABLE_ADMINS']))
- return false;
- $aBindings = array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iFanId);
- $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id";
- if($this->_oConfig->isPaidJoin()) {
- $aBindings['role'] = 'BX_BASE_MOD_GROUPS_ROLE_COMMON';
- $sWhereClause .= " AND `role` <> :role";
- }
- return $this->getOne("SELECT `id` FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause, $aBindings) ? true : false;
- }
- public function getAdmin ($iGroupProfileId, $iProfileId, $bRoleOnly = false)
- {
- $CNF = &$this->_oConfig->CNF;
- $sMethod = 'getRow';
- $sSelectClause = '*';
- if($bRoleOnly) {
- $sMethod = 'getOne';
- $sSelectClause = '`role`';
- }
- $aBindings = array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId);
- $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id";
- if($this->_oConfig->isPaidJoin()) {
- $aBindings['role'] = 'BX_BASE_MOD_GROUPS_ROLE_COMMON';
- $sWhereClause .= " AND `role` <> :role";
- }
- return $this->$sMethod("SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause . " LIMIT 1", $aBindings);
- }
- public function getAdmins ($iGroupProfileId, $iStart = 0, $iLimit = 0)
- {
- $CNF = &$this->_oConfig->CNF;
- $aBindings = array('group_profile_id' => $iGroupProfileId);
- $sWhereClause = " AND `group_profile_id` = :group_profile_id";
- if($this->_oConfig->isPaidJoin()) {
- $aBindings['role'] = 'BX_BASE_MOD_GROUPS_ROLE_COMMON';
- $sWhereClause .= " AND `role` <> :role";
- }
- $sLimitClause = "";
- if($iLimit > 0)
- $sLimitClause = " LIMIT " . (int)$iStart . ", " . (int)$iLimit;
- return $this->getColumn("SELECT `fan_id` FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause . $sLimitClause, $aBindings);
- }
- public function getRole($iGroupProfileId, $iProfileId)
- {
- return (int)$this->getRoles(array('type' => 'by_gf_id', 'group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId, 'role_only' => true));
- }
- public function getRoles($aParams)
- {
- $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
- $sSelectClause = "`ta`.*";
- $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
- switch($aParams['type']) {
- case 'by_id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = array(
- 'id' => $aParams['value']
- );
- $sWhereClause = "AND `ta`.`id`=:id";
- break;
- case 'by_gf_id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = array(
- 'group_profile_id' => $aParams['group_profile_id'],
- 'fan_id' => $aParams['fan_id'],
- );
- $sWhereClause = "AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id";
- $sLimitClause = 1;
- if(!empty($aParams['role_only'])) {
- $aMethod['name'] = 'getOne';
- $sSelectClause = "`ta`.`role`";
- }
- break;
- case 'expired':
- $sWhereClause .= "AND `ta`.`added` < UNIX_TIMESTAMP() AND `ta`.`expired` <> 0 AND `ta`.`expired` < UNIX_TIMESTAMP()";
- break;
- }
- if(!empty($sOrderClause))
- $sOrderClause = "ORDER BY " . $sOrderClause;
-
- if(!empty($sLimitClause))
- $sLimitClause = "LIMIT " . $sLimitClause;
- $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` AS `ta` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
- $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- public function updateRoles($aSet, $aWhere)
- {
- $sWhereClause = 1;
- if(!empty($aWhere))
- $sWhereClause = $this->arrayToSQL($aWhere, ' AND ');
- return (int)$this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . $sWhereClause) > 0;
- }
-
- public function deleteRoles($aWhere)
- {
- if(empty($aWhere))
- return false;
- return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
- }
- public function setRole ($iGroupProfileId, $iProfileId, $iRole, $mixedPeriod = false, $sOrder = '')
- {
- $CNF = &$this->_oConfig->CNF;
- $iNow = time();
- $aBindings = array(
- 'group_profile_id' => $iGroupProfileId,
- 'fan_id' => $iProfileId,
- 'role' => $iRole,
- 'order' => $sOrder,
- 'added' => $iNow
- );
- $sSetClause = "`group_profile_id` = :group_profile_id, `fan_id` = :fan_id, `role` = :role, `added` = :added";
- $sSetExpired = "";
- if(!empty($mixedPeriod)) {
- if(is_numeric($mixedPeriod))
- $mixedPeriod = array('period' => (int)$mixedPeriod, 'period_unit' => BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY);
- $aBindings['period'] = (int)$mixedPeriod['period'];
- switch($mixedPeriod['period_unit']) {
- case BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY:
- case BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK:
- if($mixedPeriod['period_unit'] == BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK)
- $aBindings['period'] *= 7;
- $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period DAY)";
- break;
- case BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH:
- $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period MONTH)";
- break;
- case BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR:
- $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period YEAR)";
- break;
- }
- if(!empty($sSetExpired) && !empty($mixedPeriod['period_reserve'])) {
- $aBindings['reserve'] = (int)$mixedPeriod['period_reserve'];
- $sSetExpired = "DATE_ADD(" . $sSetExpired . ", INTERVAL :reserve DAY)";
- }
- if(!empty($sSetExpired))
- $sSetClause .= ", `expired` = UNIX_TIMESTAMP(" . $sSetExpired . ")";
- }
- return !$this->query("REPLACE INTO `" . $CNF['TABLE_ADMINS'] . "` SET " . $sSetClause, $aBindings) ? false : true;
- }
- public function unsetRole($iGroupProfileId, $iProfileId)
- {
- return $this->deleteRoles(array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId));
- }
- public function getPrices($aParams)
- {
- $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
- $sSelectClause = "`tp`.*";
- $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
- if(!isset($aParams['order']) || empty($aParams['order']))
- $sOrderClause = "ORDER BY `tp`.`Order` ASC";
- switch($aParams['type']) {
- case 'by_id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = array(
- 'id' => $aParams['value']
- );
- $sWhereClause .= "AND `tp`.`id`=:id";
- break;
- case 'by_name':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = array(
- 'id' => $aParams['value']
- );
- $sWhereClause .= "AND `tp`.`name`=:name";
- break;
- case 'by_profile_id':
- $aMethod['params'][1] = array(
- 'profile_id' => $aParams['profile_id']
- );
- $sWhereClause .= "AND `tp`.`profile_id`=:profile_id";
- break;
- case 'by_prpp':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = array(
- 'profile_id' => $aParams['profile_id'],
- 'role_id' => $aParams['role_id'],
- 'period' => $aParams['period'],
- 'period_unit' => $aParams['period_unit'],
- );
- $sWhereClause .= "AND `tp`.`profile_id`=:profile_id AND `tp`.`role_id`=:role_id AND `tp`.`period`=:period AND `tp`.`period_unit`=:period_unit";
- break;
- }
- $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
- $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- public function getPriceOrderMax($iRoleId)
- {
- return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `role_id`=:role_id", array(
- 'role_id' => $iRoleId
- ));
- }
- public function deletePrices($aWhere)
- {
- return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ")) > 0;
- }
- public function insertInvite($sKey, $sGroupProfileId, $iAuthorProfileId, $iInvitedProfileId)
- {
- $aBindings = array(
- 'key' => $sKey,
- 'group_profile_id' => $sGroupProfileId,
- 'author_profile_id' => $iAuthorProfileId,
- 'invited_profile_id' => $iInvitedProfileId,
- 'added' => time()
- );
- $CNF = $this->_oConfig->CNF;
- $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);
- return (int)$this->lastId();
- }
-
- public function getInviteByKey($sKey, $iGroupProfileId)
- {
- $aBindings = array(
- 'key' => $sKey,
- 'group_profile_id' => $iGroupProfileId
- );
- $CNF = $this->_oConfig->CNF;
- return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
- }
-
- public function getInviteByInvited($iInvitedProfileId, $iGroupProfileId)
- {
- $aBindings = array(
- 'invited_profile_id' => $iInvitedProfileId,
- 'group_profile_id' => $iGroupProfileId
- );
- $CNF = $this->_oConfig->CNF;
- return $this->getOne("SELECT COUNT(*) FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `invited_profile_id` = :invited_profile_id AND group_profile_id = :group_profile_id", $aBindings);
- }
-
- public function updateInviteByKey($sKey, $iGroupProfileId, $sColumn, $sValue)
- {
- $aBindings = array(
- 'key' => $sKey,
- 'value' => $sValue,
- 'group_profile_id' => $iGroupProfileId
- );
- $CNF = $this->_oConfig->CNF;
- return $this->query("UPDATE `" . $CNF["TABLE_INVITES"] . "` SET `" . $sColumn . "` = :value WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
- }
-
- public function deleteInviteByKey($sKey, $iGroupProfileId)
- {
- $aBindings = array(
- 'key' => $sKey,
- 'group_profile_id' => $iGroupProfileId
- );
- $CNF = $this->_oConfig->CNF;
- return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
- }
-
- public function deleteInvite($iId)
- {
- $aBindings = array(
- 'id' => $iId
- );
- $CNF = $this->_oConfig->CNF;
- return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `id` = :id", $aBindings);
- }
- }
- /** @} */
|