BxDolConnectionQuery.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Database queries for Connection objects.
  11. * @see BxDolConnection
  12. */
  13. class BxDolConnectionQuery extends BxDolDb
  14. {
  15. protected $_aObject;
  16. protected $_sTable;
  17. protected $_sType;
  18. public function __construct($aObject)
  19. {
  20. parent::__construct();
  21. $this->_aObject = $aObject;
  22. $this->_sTable = $aObject['table'];
  23. $this->_sType = $aObject['type'];
  24. }
  25. static public function getConnectionObject ($sObject)
  26. {
  27. $oDb = BxDolDb::getInstance();
  28. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_connection` WHERE `object` = ?", $sObject);
  29. $aObject = $oDb->getRow($sQuery);
  30. if (!$aObject || !is_array($aObject))
  31. return false;
  32. return $aObject;
  33. }
  34. public function getCommonContentSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  35. {
  36. $sWhereJoin1 = $this->prepareAsString(" AND `c`.`initiator` = ?", $iInitiator1);
  37. $sWhereJoin2 = $this->prepareAsString(" AND `c2`.`initiator` = ?", $iInitiator2);
  38. if (false !== $isMutual) {
  39. $sWhereJoin1 .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
  40. $sWhereJoin2 .= $this->prepareAsString(" AND `c2`.`mutual` = ?", $isMutual);
  41. }
  42. $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')" : '';
  43. return array(
  44. 'join' => "
  45. INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sWhereJoin1)
  46. INNER JOIN `{$this->_sTable}` AS `c2` ON (`c2`.`content` = `c`.`content` $sWhereJoin2)" . $sJoin,
  47. );
  48. }
  49. public function getConnectedContentSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  50. {
  51. $aResult = $this->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  52. $aFields = array();
  53. foreach($aResult['fields'] as $sFieldAlias => $aField)
  54. $aFields[$sFieldAlias] = "`" . $aField['table_alias'] . "`.`" . $aField['name'] . "`";
  55. $sJoin = $this->_aObject['profile_content'] ? "INNER JOIN `sys_profiles` ON (`sys_profiles`.`id` = `{$aResult['join']['table_alias']}`.`content` AND `p`.`status` = 'active')" : '';
  56. return array(
  57. 'fields' => $aFields,
  58. 'join' => $aResult['join']['type'] . " JOIN `" . $aResult['join']['table'] . "` AS `" . $aResult['join']['table_alias'] . "` ON (" . $aResult['join']['condition'] . ") " . $sJoin,
  59. );
  60. }
  61. public function getConnectedContentSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  62. {
  63. $sWhere = $this->prepareAsString(" AND `c`.`initiator` = ?", $iInitiator);
  64. if(false !== $isMutual)
  65. $sWhere .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
  66. return array(
  67. 'fields' => array(
  68. 'added' => array('table_alias' => 'c', 'name' => 'added')
  69. ),
  70. 'join' => array(
  71. 'type' => 'INNER',
  72. 'table' => $this->_sTable,
  73. 'table_alias' => 'c',
  74. 'condition' => "`c`.`content` = `" . $sContentTable . "`.`" . $sContentField . "`" . $sWhere
  75. )
  76. );
  77. }
  78. public function getConnectedContentSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  79. {
  80. $sOn = "";
  81. if (false !== $isMutual)
  82. $sOn .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
  83. return array(
  84. 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sOn)",
  85. 'where' => " AND `c`.`initiator` = `$sInitiatorTable`.`$sInitiatorField`"
  86. );
  87. }
  88. public function getConnectedInitiatorsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  89. {
  90. $aResult = $this->getConnectedInitiatorsSQLPartsExt($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  91. $sJoin = $this->_aObject['profile_initiator'] ? "INNER JOIN `sys_profiles` ON (`sys_profiles`.`id` = `{$aResult['join']['table_alias']}`.`initiator` AND `sys_profiles`.`status` = 'active')" : '';
  92. return array(
  93. 'join' => $aResult['join']['type'] . " JOIN `" . $aResult['join']['table'] . "` AS `" . $aResult['join']['table_alias'] . "` ON (" . $aResult['join']['condition'] . ") " . $sJoin,
  94. );
  95. }
  96. public function getConnectedInitiatorsSQLPartsExt ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  97. {
  98. $sWhere = $this->prepareAsString(" AND `c`.`content` = ?", $iContent);
  99. if(false !== $isMutual)
  100. $sWhere .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
  101. return array(
  102. 'join' => array(
  103. 'type' => 'INNER',
  104. 'table' => $this->_sTable,
  105. 'table_alias' => 'c',
  106. 'condition' => "`c`.`initiator` = `" . $sInitiatorTable . "`.`" . $sInitiatorField . "`" . $sWhere
  107. )
  108. );
  109. }
  110. public function getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  111. {
  112. $sOn = "";
  113. if(false !== $isMutual)
  114. $sOn .= $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual);
  115. return array(
  116. 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`initiator` = `$sInitiatorTable`.`$sInitiatorField` $sOn)",
  117. 'where' => " AND `c`.`content` = `$sContentTable`.`$sContentField`"
  118. );
  119. }
  120. public function getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder)
  121. {
  122. $sWhereJoin = (false !== $isMutual) ? " AND `c2`.`mutual` = :mutual" : "";
  123. $sJoin = "INNER JOIN `" . $this->_sTable . "` AS `c2` ON (`c2`.`initiator` = :initiator2 AND `c`.`content` = `c2`.`content` $sWhereJoin)";
  124. $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')" : '';
  125. $sWhere = " AND `c`.`initiator` = :initiator1";
  126. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  127. return $this->getColumn($sQuery, array(
  128. 'mutual' => $isMutual,
  129. 'initiator1' => $iInitiator1,
  130. 'initiator2' => $iInitiator2,
  131. ));
  132. }
  133. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  134. {
  135. $sWhere = " AND `c`.`initiator` = :initiator";
  136. $sJoin = $this->_aObject['profile_content'] ? "INNER JOIN `sys_profiles` `p` ON `p`.`id` = `c`.`content` AND `p`.`status` = 'active'" : '';
  137. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  138. return $this->getColumn($sQuery, array(
  139. 'initiator' => $iInitiator
  140. ));
  141. }
  142. public function getConnectedContentByType ($iInitiator, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  143. {
  144. $mixedType = is_array($mixedType) ? $mixedType : array($mixedType);
  145. $sWhere = " AND `c`.`initiator` = :initiator";
  146. $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) . ')' : '';
  147. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  148. return $this->getColumn($sQuery, array(
  149. 'initiator' => $iInitiator,
  150. ));
  151. }
  152. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  153. {
  154. $sWhere = " AND `c`.`content` = :content";
  155. $sJoin = $this->_aObject['profile_initiator'] ? 'INNER JOIN `sys_profiles` `p` ON `p`.`id` = `c`.`initiator` AND `p`.`status` = \'active\'' : '';
  156. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`initiator`', $isMutual, $iStart, $iLimit, $iOrder);
  157. return $this->getColumn($sQuery, array(
  158. 'content' => $iContent
  159. ));
  160. }
  161. public function getConnectedInitiatorsByType ($iContent, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  162. {
  163. $mixedType = is_array($mixedType) ? $mixedType : array($mixedType);
  164. $sWhere = " AND `c`.`content` = :content";
  165. $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) . ')' : '';
  166. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`initiator`', $isMutual, $iStart, $iLimit, $iOrder);
  167. return $this->getColumn($sQuery, array(
  168. 'content' => $iContent,
  169. ));
  170. }
  171. protected function _getConnectionsQuery ($sWhere, $sJoin = '', $sFields = '*', $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  172. {
  173. $sOrder = $this->_getOrderClause($iOrder);
  174. $sWhere .= (false !== $isMutual) ? $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual) : '';
  175. $sLimit = "";
  176. if($iLimit != BX_CONNECTIONS_LIST_NO_LIMIT)
  177. $sLimit = $this->prepareAsString("LIMIT ?, ?", $iStart, $iLimit);
  178. return "SELECT $sFields FROM `" . $this->_sTable . "` AS `c` $sJoin WHERE 1 $sWhere $sOrder $sLimit";
  179. }
  180. public function getConnectedContentCount ($iInitiator, $isMutual = false)
  181. {
  182. $sWhere = $this->prepareAsString(" AND `c`.`initiator` = ?", $iInitiator);
  183. $sQuery = $this->_getConnectionsQueryCount($sWhere, '', $isMutual);
  184. return $this->getOne($sQuery);
  185. }
  186. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  187. {
  188. $sWhere = $this->prepareAsString(" AND `c`.`content` = ?", $iContent);
  189. $sQuery = $this->_getConnectionsQueryCount($sWhere, '', $isMutual);
  190. return $this->getOne($sQuery);
  191. }
  192. protected function _getConnectionsQueryCount ($sWhere, $sJoin = '', $isMutual = false)
  193. {
  194. $sWhere .= (false !== $isMutual) ? $this->prepareAsString(" AND `c`.`mutual` = ?", $isMutual) : '';
  195. return "SELECT COUNT(`id`) FROM `" . $this->_sTable . "` AS `c` $sJoin WHERE 1 $sWhere";
  196. }
  197. protected function _getOrderClause ($iOrder = BX_CONNECTIONS_ORDER_NONE, $sTable = '')
  198. {
  199. if ($sTable)
  200. $sTable .= '.';
  201. $sOrder = '';
  202. switch ($iOrder) {
  203. case BX_CONNECTIONS_ORDER_ADDED_ASC:
  204. $sOrder = "ORDER BY {$sTable}`added` ASC";
  205. break;
  206. case BX_CONNECTIONS_ORDER_ADDED_DESC:
  207. $sOrder = "ORDER BY {$sTable}`added` DESC";
  208. break;
  209. }
  210. return $sOrder;
  211. }
  212. public function getConnection ($iInitiator, $iContent)
  213. {
  214. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  215. return $this->fromMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent, 'getRow', $sQuery);
  216. }
  217. public function addConnection ($iInitiator, $iContent, &$iMutualParam = null)
  218. {
  219. if ($this->getConnection($iInitiator, $iContent)) // connection already exists
  220. return false;
  221. $aBindings = array();
  222. $iMutual = 0;
  223. $sMutualField = '';
  224. if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType) {
  225. $aConnectionMutual = $this->getConnection($iContent, $iInitiator);
  226. $iMutual = $aConnectionMutual ? 1 : 0;
  227. $sMutualField = ", `mutual` = :mutual";
  228. $aBindings['mutual'] = $iMutual;
  229. }
  230. $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_sTable . "` SET `initiator` = :initiator, `content` = :content, `added` = :added" . $sMutualField);
  231. if (!$this->query($sQuery, array_merge($aBindings, array('initiator' => $iInitiator, 'content' => $iContent, 'added' => time()))))
  232. return false;
  233. if ($iMutual) // in case of mutual connection update 'mutual' field
  234. $this->updateConnectionMutual($iContent, $iInitiator, $iMutual);
  235. if (null !== $iMutualParam)
  236. $iMutualParam = $iMutual;
  237. $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
  238. return true;
  239. }
  240. public function updateConnection ($iInitiator, $iContent, $aSet)
  241. {
  242. if(empty($aSet) || !is_array($aSet))
  243. return false;
  244. $sQuery = $this->prepare("UPDATE `" . $this->_sTable . "` SET " . $this->arrayToSQL($aSet) . " WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  245. if ($bResult = $this->query($sQuery))
  246. $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
  247. return $bResult;
  248. }
  249. public function updateConnectionMutual ($iInitiator, $iContent, $iMutual)
  250. {
  251. $sQuery = $this->prepare("UPDATE `" . $this->_sTable . "` SET `mutual` = ? WHERE `initiator` = ? AND `content` = ?", $iMutual, $iInitiator, $iContent);
  252. if ($bResult = $this->query($sQuery))
  253. $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
  254. return $bResult;
  255. }
  256. public function removeConnection ($iInitiator, $iContent)
  257. {
  258. if (!($aConnection = $this->getConnection($iInitiator, $iContent))) // connection doesn't exist
  259. return true;
  260. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  261. if (!$this->res($sQuery))
  262. return false;
  263. $this->cleanMemory('BxDolConnectionQuery::getConnection' . $this->_sTable . $iInitiator . '_' . $iContent);
  264. if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $aConnection['mutual'])
  265. $this->removeConnection($iContent, $iInitiator);
  266. return true;
  267. }
  268. public function onDelete ($iId, $sField = 'initiator')
  269. {
  270. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `$sField` = ?", $iId);
  271. return $this->query($sQuery);
  272. }
  273. public function onModuleDelete ($sTable, $sFieldId, $sField = 'initiator')
  274. {
  275. return $this->onModuleDeleteCustom ($sTable, $sFieldId, $sField);
  276. }
  277. public function onModuleProfileDelete ($sModuleName, $sField = 'initiator')
  278. {
  279. return $this->onModuleDeleteCustom ('sys_profiles', 'id', $sField, $this->prepareAsString(" AND `sys_profiles`.`type` = ? ", $sModuleName));
  280. }
  281. protected function onModuleDeleteCustom ($sTable, $sFieldId, $sField = 'initiator', $sWhere = '')
  282. {
  283. $sQuery = $this->prepare("DELETE `" . $this->_sTable . "` FROM `" . $this->_sTable . "` INNER JOIN `{$sTable}` WHERE `" . $this->_sTable . "`.`$sField` = `{$sTable}`.`{$sFieldId}` " . $sWhere);
  284. return $this->query($sQuery);
  285. }
  286. }
  287. /** @} */