| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- /**
- * Database queries for Connection objects.
- * @see BxDolConnection
- */
- class BxDolConnectionQuery extends BxDolDb
- {
- protected $_aObject;
- protected $_sTable;
- protected $_sType;
- public function __construct($aObject)
- {
- parent::__construct();
- $this->_aObject = $aObject;
- $this->_sTable = $aObject['table'];
- $this->_sType = $aObject['type'];
- }
- static public function getConnectionObject ($sObject)
- {
- $oDb = BxDolDb::getInstance();
- $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_connection` WHERE `object` = ?", $sObject);
- $aObject = $oDb->getRow($sQuery);
- if (!$aObject || !is_array($aObject))
- return false;
- return $aObject;
- }
- public function getCommonContentSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
- {
- $sWhereJoin1 = $this->prepareAsString(" AND `c`.`initiator` = ?", $iInitiator1);
- $sWhereJoin2 = $this->prepareAsString(" AND `c2`.`initiator` = ?", $iInitiator2);
- if (false !== $isMutual) {
- $sWhereJoin1 .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
- $sWhereJoin2 .= $this->prepareAsString(" AND `c2`.`mutual` = ?", $isMutual);
- }
- $sJoin = $this->_aObject['profile_content'] ? "INNER JOIN `sys_profiles` AS `p1` ON (`p1`.`id` = `c`.`content` AND `p1`.`status` = 'active') INNER JOIN `sys_profiles` AS `p2` ON (`p2`.`id` = `c2`.`content` AND `p2`.`status` = 'active')" : '';
- return array(
- 'join' => "
- INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sWhereJoin1)
- INNER JOIN `{$this->_sTable}` AS `c2` ON (`c2`.`content` = `c`.`content` $sWhereJoin2)" . $sJoin,
- );
- }
- public function getConnectedContentSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
- {
- $aResult = $this->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
- $aFields = array();
- foreach($aResult['fields'] as $sFieldAlias => $aField)
- $aFields[$sFieldAlias] = "`" . $aField['table_alias'] . "`.`" . $aField['name'] . "`";
- $sJoin = $this->_aObject['profile_content'] ? "INNER JOIN `sys_profiles` ON (`sys_profiles`.`id` = `{$aResult['join']['table_alias']}`.`content` AND `p`.`status` = 'active')" : '';
- return array(
- 'fields' => $aFields,
- 'join' => $aResult['join']['type'] . " JOIN `" . $aResult['join']['table'] . "` AS `" . $aResult['join']['table_alias'] . "` ON (" . $aResult['join']['condition'] . ") " . $sJoin,
- );
- }
- public function getConnectedContentSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
- {
- $sWhere = $this->prepareAsString(" AND `c`.`initiator` = ?", $iInitiator);
- if(false !== $isMutual)
- $sWhere .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
- return array(
- 'fields' => array(
- 'added' => array('table_alias' => 'c', 'name' => 'added')
- ),
- 'join' => array(
- 'type' => 'INNER',
- 'table' => $this->_sTable,
- 'table_alias' => 'c',
- 'condition' => "`c`.`content` = `" . $sContentTable . "`.`" . $sContentField . "`" . $sWhere
- )
- );
- }
- public function getConnectedContentSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
- {
- $sOn = "";
- if (false !== $isMutual)
- $sOn .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
- return array(
- 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sOn)",
- 'where' => " AND `c`.`initiator` = `$sInitiatorTable`.`$sInitiatorField`"
- );
- }
- public function getConnectedInitiatorsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
- {
- $aResult = $this->getConnectedInitiatorsSQLPartsExt($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
- $sJoin = $this->_aObject['profile_initiator'] ? "INNER JOIN `sys_profiles` ON (`sys_profiles`.`id` = `{$aResult['join']['table_alias']}`.`initiator` AND `sys_profiles`.`status` = 'active')" : '';
- return array(
- 'join' => $aResult['join']['type'] . " JOIN `" . $aResult['join']['table'] . "` AS `" . $aResult['join']['table_alias'] . "` ON (" . $aResult['join']['condition'] . ") " . $sJoin,
- );
- }
- public function getConnectedInitiatorsSQLPartsExt ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
- {
- $sWhere = $this->prepareAsString(" AND `c`.`content` = ?", $iContent);
- if(false !== $isMutual)
- $sWhere .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
- return array(
- 'join' => array(
- 'type' => 'INNER',
- 'table' => $this->_sTable,
- 'table_alias' => 'c',
- 'condition' => "`c`.`initiator` = `" . $sInitiatorTable . "`.`" . $sInitiatorField . "`" . $sWhere
- )
- );
- }
- public function getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
- {
- $sOn = "";
- if(false !== $isMutual)
- $sOn .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
- return array(
- 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`initiator` = `$sInitiatorTable`.`$sInitiatorField` $sOn)",
- 'where' => " AND `c`.`content` = `$sContentTable`.`$sContentField`"
- );
- }
- public function getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder)
- {
- $sWhereJoin = (false !== $isMutual) ? " AND `c2`.`mutual` = :mutual" : "";
- $sJoin = "INNER JOIN `" . $this->_sTable . "` AS `c2` ON (`c2`.`initiator` = :initiator2 AND `c`.`content` = `c2`.`content` $sWhereJoin)";
- $sJoin .= $this->_aObject['profile_content'] ? "INNER JOIN `sys_profiles` AS `p1` ON (`p1`.`id` = `c`.`content` AND `p1`.`status` = 'active') INNER JOIN `sys_profiles` AS `p2` ON (`p2`.`id` = `c2`.`content` AND `p2`.`status` = 'active')" : '';
- $sWhere = " AND `c`.`initiator` = :initiator1";
- $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
- return $this->getColumn($sQuery, array(
- 'mutual' => $isMutual,
- 'initiator1' => $iInitiator1,
- 'initiator2' => $iInitiator2,
- ));
- }
- public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
- {
- $sWhere = " AND `c`.`initiator` = :initiator";
- $sJoin = $this->_aObject['profile_content'] ? "INNER JOIN `sys_profiles` `p` ON `p`.`id` = `c`.`content` AND `p`.`status` = 'active'" : '';
- $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
- return $this->getColumn($sQuery, array(
- 'initiator' => $iInitiator
- ));
- }
-
- public function getConnectedContentByType ($iInitiator, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
- {
- $mixedType = is_array($mixedType) ? $mixedType : array($mixedType);
- $sWhere = " AND `c`.`initiator` = :initiator";
- $sJoin = $this->_aObject['profile_content'] ? 'INNER JOIN `sys_profiles` `p` ON `p`.`id` = `c`.`content` AND `p`.`status` = \'active\' AND `p`.`type` IN (' . $this->implode_escape($mixedType) . ')' : '';
- $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
- return $this->getColumn($sQuery, array(
- 'initiator' => $iInitiator,
- ));
- }
- public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
- {
- $sWhere = " AND `c`.`content` = :content";
- $sJoin = $this->_aObject['profile_initiator'] ? 'INNER JOIN `sys_profiles` `p` ON `p`.`id` = `c`.`initiator` AND `p`.`status` = \'active\'' : '';
- $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`initiator`', $isMutual, $iStart, $iLimit, $iOrder);
- return $this->getColumn($sQuery, array(
- 'content' => $iContent
- ));
- }
- public function getConnectedInitiatorsByType ($iContent, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
- {
- $mixedType = is_array($mixedType) ? $mixedType : array($mixedType);
- $sWhere = " AND `c`.`content` = :content";
- $sJoin = $this->_aObject['profile_initiator'] ? 'INNER JOIN `sys_profiles` `p` ON `p`.`id` = `c`.`initiator` AND `p`.`status` = \'active\' AND `p`.`type` IN (' . $this->implode_escape($mixedType) . ')' : '';
- $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`initiator`', $isMutual, $iStart, $iLimit, $iOrder);
- return $this->getColumn($sQuery, array(
- 'content' => $iContent,
- ));
- }
- protected function _getConnectionsQuery ($sWhere, $sJoin = '', $sFields = '*', $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
- {
- $sOrder = $this->_getOrderClause($iOrder);
- $sWhere .= (false !== $isMutual) ? $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual) : '';
- $sLimit = "";
- if($iLimit != BX_CONNECTIONS_LIST_NO_LIMIT)
- $sLimit = $this->prepareAsString("LIMIT ?, ?", $iStart, $iLimit);
- return "SELECT $sFields FROM `" . $this->_sTable . "` AS `c` $sJoin WHERE 1 $sWhere $sOrder $sLimit";
- }
- public function getConnectedContentCount ($iInitiator, $isMutual = false)
- {
- $sWhere = $this->prepareAsString(" AND `c`.`initiator` = ?", $iInitiator);
- $sQuery = $this->_getConnectionsQueryCount($sWhere, '', $isMutual);
- return $this->getOne($sQuery);
- }
- public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
- {
- $sWhere = $this->prepareAsString(" AND `c`.`content` = ?", $iContent);
- $sQuery = $this->_getConnectionsQueryCount($sWhere, '', $isMutual);
- return $this->getOne($sQuery);
- }
- protected function _getConnectionsQueryCount ($sWhere, $sJoin = '', $isMutual = false)
- {
- $sWhere .= (false !== $isMutual) ? $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual) : '';
- return "SELECT COUNT(`id`) FROM `" . $this->_sTable . "` AS `c` $sJoin WHERE 1 $sWhere";
- }
- protected function _getOrderClause ($iOrder = BX_CONNECTIONS_ORDER_NONE, $sTable = '')
- {
- if ($sTable)
- $sTable .= '.';
- $sOrder = '';
- switch ($iOrder) {
- case BX_CONNECTIONS_ORDER_ADDED_ASC:
- $sOrder = "ORDER BY {$sTable}`added` ASC";
- break;
- case BX_CONNECTIONS_ORDER_ADDED_DESC:
- $sOrder = "ORDER BY {$sTable}`added` DESC";
- break;
- }
- return $sOrder;
- }
- public function getConnection ($iInitiator, $iContent)
- {
- $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
- return $this->fromMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent, 'getRow', $sQuery);
- }
- public function addConnection ($iInitiator, $iContent, &$iMutualParam = null)
- {
- if ($this->getConnection($iInitiator, $iContent)) // connection already exists
- return false;
- $aBindings = array();
- $iMutual = 0;
- $sMutualField = '';
- if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType) {
- $aConnectionMutual = $this->getConnection($iContent, $iInitiator);
- $iMutual = $aConnectionMutual ? 1 : 0;
- $sMutualField = ", `mutual` = :mutual";
- $aBindings['mutual'] = $iMutual;
- }
- $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_sTable . "` SET `initiator` = :initiator, `content` = :content, `added` = :added" . $sMutualField);
- if (!$this->query($sQuery, array_merge($aBindings, array('initiator' => $iInitiator, 'content' => $iContent, 'added' => time()))))
- return false;
- if ($iMutual) // in case of mutual connection update 'mutual' field
- $this->updateConnectionMutual($iContent, $iInitiator, $iMutual);
- if (null !== $iMutualParam)
- $iMutualParam = $iMutual;
- $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
-
- return true;
- }
- public function updateConnection ($iInitiator, $iContent, $aSet)
- {
- if(empty($aSet) || !is_array($aSet))
- return false;
- $sQuery = $this->prepare("UPDATE `" . $this->_sTable . "` SET " . $this->arrayToSQL($aSet) . " WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
- if ($bResult = $this->query($sQuery))
- $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
- return $bResult;
- }
- public function updateConnectionMutual ($iInitiator, $iContent, $iMutual)
- {
- $sQuery = $this->prepare("UPDATE `" . $this->_sTable . "` SET `mutual` = ? WHERE `initiator` = ? AND `content` = ?", $iMutual, $iInitiator, $iContent);
- if ($bResult = $this->query($sQuery))
- $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
- return $bResult;
- }
- public function removeConnection ($iInitiator, $iContent)
- {
- if (!($aConnection = $this->getConnection($iInitiator, $iContent))) // connection doesn't exist
- return true;
- $sQuery = $this->prepare("DELETE FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
- if (!$this->res($sQuery))
- return false;
- $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
-
- if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $aConnection['mutual'])
- $this->removeConnection($iContent, $iInitiator);
- return true;
- }
- public function onDelete ($iId, $sField = 'initiator')
- {
- $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `$sField` = ?", $iId);
- return $this->query($sQuery);
- }
- public function onModuleDelete ($sTable, $sFieldId, $sField = 'initiator')
- {
- return $this->onModuleDeleteCustom ($sTable, $sFieldId, $sField);
- }
- public function onModuleProfileDelete ($sModuleName, $sField = 'initiator')
- {
- return $this->onModuleDeleteCustom ('sys_profiles', 'id', $sField, $this->prepareAsString(" AND `sys_profiles`.`type` = ? ", $sModuleName));
- }
- protected function onModuleDeleteCustom ($sTable, $sFieldId, $sField = 'initiator', $sWhere = '')
- {
- $sQuery = $this->prepare("DELETE `" . $this->_sTable . "` FROM `" . $this->_sTable . "` INNER JOIN `{$sTable}` WHERE `" . $this->_sTable . "`.`$sField` = `{$sTable}`.`{$sFieldId}` " . $sWhere);
- return $this->query($sQuery);
- }
- }
- /** @} */
|