BxDolConnectionQuery.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * Database queries for Connection objects.
  12. * @see BxDolConnection
  13. */
  14. class BxDolConnectionQuery extends BxDolDb {
  15. protected $_aObject;
  16. protected $_sTable;
  17. protected $_sType;
  18. public function __construct($aObject) {
  19. parent::__construct();
  20. $this->_aObject = $aObject;
  21. $this->_sTable = $aObject['table'];
  22. $this->_sType = $aObject['type'];
  23. }
  24. static public function getConnectionObject ($sObject) {
  25. $oDb = BxDolDb::getInstance();
  26. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_connection` WHERE `object` = ?", $sObject);
  27. $aObject = $oDb->getRow($sQuery);
  28. if (!$aObject || !is_array($aObject))
  29. return false;
  30. return $aObject;
  31. }
  32. public function getCommonContentSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false) {
  33. $sWhereJoin1 = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator1);
  34. $sWhereJoin2 = $this->prepare(" AND `c2`.`initiator` = ?", $iInitiator2);
  35. if (false !== $isMutual) {
  36. $sWhereJoin1 .= $this->prepare(" AND `c`.`mutual` = ?", $isMutual);
  37. $sWhereJoin2 .= $this->prepare(" AND `c2`.`mutual` = ?", $isMutual);
  38. }
  39. return array(
  40. 'join' => "
  41. INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sWhereJoin1)
  42. INNER JOIN `{$this->_sTable}` AS `c2` ON (`c2`.`content` = `c`.`content` $sWhereJoin2)",
  43. );
  44. }
  45. public function getConnectedContentSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false) {
  46. $sWhere = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator);
  47. if (false !== $isMutual)
  48. $sWhere .= $this->prepare(" AND `c`.`mutual` = ?", $isMutual);
  49. return array(
  50. 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sWhere)",
  51. );
  52. }
  53. public function getConnectedInitiatorsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false) {
  54. $sWhere = $this->prepare(" AND `c`.`content` = ?", $iInitiator);
  55. if (false !== $isMutual)
  56. $sWhere .= $this->prepare(" AND `c`.`mutual` = ?", $isMutual);
  57. return array(
  58. 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`initiator` = `$sContentTable`.`$sContentField` $sWhere)",
  59. );
  60. }
  61. public function getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder) {
  62. $sWhereJoin = (false !== $isMutual) ? $this->prepare(" AND `c2`.`mutual` = ?", $isMutual) : '';
  63. $sJoin = $this->prepare("INNER JOIN `" . $this->_sTable . "` AS `c2` ON (`c2`.`initiator` = ? AND `c`.`content` = `c2`.`content` $sWhereJoin)", $iInitiator2);
  64. $sWhere = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator1);
  65. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  66. return $this->getColumn($sQuery);
  67. }
  68. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  69. $sWhere = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator);
  70. $sQuery = $this->_getConnectionsQuery($sWhere, '', '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  71. return $this->getColumn($sQuery);
  72. }
  73. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  74. $sWhere = $this->prepare(" AND `c`.`content` = ?", $iContent);
  75. $sQuery = $this->_getConnectionsQuery($sWhere, '', '`c`.`initiator`', $isMutual, $iStart, $iLimit, $iOrder);
  76. return $this->getColumn($sQuery);
  77. }
  78. protected function _getConnectionsQuery ($sWhere, $sJoin = '', $sFields = '*', $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  79. $sOrder = $this->_getOrderClause($iOrder);
  80. $sWhere .= (false !== $isMutual) ? $this->prepare(" AND `c`.`mutual` = ?", $isMutual) : '';
  81. return $this->prepare("SELECT $sFields FROM `" . $this->_sTable . "` AS `c` $sJoin WHERE 1 $sWhere $sOrder LIMIT ?, ?", $iStart, $iLimit);
  82. }
  83. protected function _getOrderClause ($iOrder = BX_CONNECTIONS_ORDER_NONE, $sTable = '') {
  84. if ($sTable)
  85. $sTable .= '.';
  86. $sOrder = '';
  87. switch ($iOrder) {
  88. case BX_CONNECTIONS_ORDER_ADDED_ASC:
  89. $sOrder = "ORDER BY {$sTable}`added` ASC";
  90. break;
  91. case BX_CONNECTIONS_ORDER_ADDED_DESC:
  92. $sOrder = "ORDER BY {$sTable}`added` DESC";
  93. break;
  94. }
  95. return $sOrder;
  96. }
  97. public function getConnection ($iInitiator, $iContent) {
  98. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  99. return $this->getRow($sQuery);
  100. }
  101. public function addConnection ($iInitiator, $iContent, &$iMutualParam = null) {
  102. if ($this->getConnection($iInitiator, $iContent)) // connection already exists
  103. return false;
  104. $iMutual = 0;
  105. $sMutualField = '';
  106. if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType) {
  107. $aConnectionMutual = $this->getConnection($iContent, $iInitiator);
  108. $iMutual = $aConnectionMutual ? 1 : 0;
  109. $sMutualField = $this->prepare(", `mutual` = ?", $iMutual);
  110. }
  111. $sQuery = $this->prepare("INSERT INTO `" . $this->_sTable . "` SET `initiator` = ?, `content` = ?, `added` = ?", $iInitiator, $iContent, time());
  112. $sQuery .= $sMutualField;
  113. if (!$this->query($sQuery))
  114. return false;
  115. if ($iMutual) // in case of mutual connection update 'mutual' field
  116. $this->updateConnectionMutual($iContent, $iInitiator, $iMutual);
  117. if (null !== $iMutualParam)
  118. $iMutualParam = $iMutual;
  119. return true;
  120. }
  121. public function updateConnectionMutual ($iInitiator, $iContent, $iMutual) {
  122. $sQuery = $this->prepare("UPDATE `" . $this->_sTable . "` SET `mutual` = ? WHERE `initiator` = ? AND `content` = ?", $iMutual, $iInitiator, $iContent);
  123. return $this->query($sQuery);
  124. }
  125. public function removeConnection ($iInitiator, $iContent) {
  126. if (!($aConnection = $this->getConnection($iInitiator, $iContent))) // connection doesn't exist
  127. return false;
  128. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  129. if (!$this->query($sQuery))
  130. return false;
  131. if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $aConnection['mutual'])
  132. $this->removeConnection($iContent, $iInitiator);
  133. return true;
  134. }
  135. }
  136. /** @} */