BxDolConnectionQuery.php 17 KB

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