BxDolConnectionQuery.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 TridentCore Trident Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * Database queries for Connection objects.
  12. * @see BxDolConnection
  13. */
  14. class BxDolConnectionQuery extends BxDolDb
  15. {
  16. protected $_aObject;
  17. protected $_sTable;
  18. protected $_sType;
  19. public function __construct($aObject)
  20. {
  21. parent::__construct();
  22. $this->_aObject = $aObject;
  23. $this->_sTable = $aObject['table'];
  24. $this->_sType = $aObject['type'];
  25. }
  26. static public function getConnectionObject ($sObject)
  27. {
  28. $oDb = BxDolDb::getInstance();
  29. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_connection` WHERE `object` = ?", $sObject);
  30. $aObject = $oDb->getRow($sQuery);
  31. if (!$aObject || !is_array($aObject))
  32. return false;
  33. return $aObject;
  34. }
  35. public function getCommonContentSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  36. {
  37. $sWhereJoin1 = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator1);
  38. $sWhereJoin2 = $this->prepare(" AND `c2`.`initiator` = ?", $iInitiator2);
  39. if (false !== $isMutual) {
  40. $sWhereJoin1 .= $this->prepare(" AND `c`.`mutual` = ?", $isMutual);
  41. $sWhereJoin2 .= $this->prepare(" AND `c2`.`mutual` = ?", $isMutual);
  42. }
  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)",
  47. );
  48. }
  49. public function getConnectedContentSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  50. {
  51. $sWhere = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator);
  52. if (false !== $isMutual)
  53. $sWhere .= $this->prepare(" AND `c`.`mutual` = ?", $isMutual);
  54. return array(
  55. 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`content` = `$sContentTable`.`$sContentField` $sWhere)",
  56. );
  57. }
  58. public function getConnectedInitiatorsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  59. {
  60. $sWhere = $this->prepare(" AND `c`.`content` = ?", $iInitiator);
  61. if (false !== $isMutual)
  62. $sWhere .= $this->prepare(" AND `c`.`mutual` = ?", $isMutual);
  63. return array(
  64. 'join' => "INNER JOIN `{$this->_sTable}` AS `c` ON (`c`.`initiator` = `$sContentTable`.`$sContentField` $sWhere)",
  65. );
  66. }
  67. public function getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder)
  68. {
  69. $sWhereJoin = (false !== $isMutual) ? $this->prepare(" AND `c2`.`mutual` = ?", $isMutual) : '';
  70. $sJoin = $this->prepare("INNER JOIN `" . $this->_sTable . "` AS `c2` ON (`c2`.`initiator` = ? AND `c`.`content` = `c2`.`content` $sWhereJoin)", $iInitiator2);
  71. $sWhere = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator1);
  72. $sQuery = $this->_getConnectionsQuery($sWhere, $sJoin, '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  73. return $this->getColumn($sQuery);
  74. }
  75. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  76. {
  77. $sWhere = $this->prepare(" AND `c`.`initiator` = ?", $iInitiator);
  78. $sQuery = $this->_getConnectionsQuery($sWhere, '', '`c`.`content`', $isMutual, $iStart, $iLimit, $iOrder);
  79. return $this->getColumn($sQuery);
  80. }
  81. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  82. {
  83. $sWhere = $this->prepare(" AND `c`.`content` = ?", $iContent);
  84. $sQuery = $this->_getConnectionsQuery($sWhere, '', '`c`.`initiator`', $isMutual, $iStart, $iLimit, $iOrder);
  85. return $this->getColumn($sQuery);
  86. }
  87. protected function _getConnectionsQuery ($sWhere, $sJoin = '', $sFields = '*', $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  88. {
  89. $sOrder = $this->_getOrderClause($iOrder);
  90. $sWhere .= (false !== $isMutual) ? $this->prepare(" AND `c`.`mutual` = ?", $isMutual) : '';
  91. return $this->prepare("SELECT $sFields FROM `" . $this->_sTable . "` AS `c` $sJoin WHERE 1 $sWhere $sOrder LIMIT ?, ?", $iStart, $iLimit);
  92. }
  93. protected function _getOrderClause ($iOrder = BX_CONNECTIONS_ORDER_NONE, $sTable = '')
  94. {
  95. if ($sTable)
  96. $sTable .= '.';
  97. $sOrder = '';
  98. switch ($iOrder) {
  99. case BX_CONNECTIONS_ORDER_ADDED_ASC:
  100. $sOrder = "ORDER BY {$sTable}`added` ASC";
  101. break;
  102. case BX_CONNECTIONS_ORDER_ADDED_DESC:
  103. $sOrder = "ORDER BY {$sTable}`added` DESC";
  104. break;
  105. }
  106. return $sOrder;
  107. }
  108. public function getConnection ($iInitiator, $iContent)
  109. {
  110. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  111. return $this->getRow($sQuery);
  112. }
  113. public function addConnection ($iInitiator, $iContent, &$iMutualParam = null)
  114. {
  115. if ($this->getConnection($iInitiator, $iContent)) // connection already exists
  116. return false;
  117. $iMutual = 0;
  118. $sMutualField = '';
  119. if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType) {
  120. $aConnectionMutual = $this->getConnection($iContent, $iInitiator);
  121. $iMutual = $aConnectionMutual ? 1 : 0;
  122. $sMutualField = $this->prepare(", `mutual` = ?", $iMutual);
  123. }
  124. $sQuery = $this->prepare("INSERT INTO `" . $this->_sTable . "` SET `initiator` = ?, `content` = ?, `added` = ?", $iInitiator, $iContent, time());
  125. $sQuery .= $sMutualField;
  126. if (!$this->query($sQuery))
  127. return false;
  128. if ($iMutual) // in case of mutual connection update 'mutual' field
  129. $this->updateConnectionMutual($iContent, $iInitiator, $iMutual);
  130. if (null !== $iMutualParam)
  131. $iMutualParam = $iMutual;
  132. return true;
  133. }
  134. public function updateConnectionMutual ($iInitiator, $iContent, $iMutual)
  135. {
  136. $sQuery = $this->prepare("UPDATE `" . $this->_sTable . "` SET `mutual` = ? WHERE `initiator` = ? AND `content` = ?", $iMutual, $iInitiator, $iContent);
  137. return $this->query($sQuery);
  138. }
  139. public function removeConnection ($iInitiator, $iContent)
  140. {
  141. if (!($aConnection = $this->getConnection($iInitiator, $iContent))) // connection doesn't exist
  142. return false;
  143. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTable . "` WHERE `initiator` = ? AND `content` = ?", $iInitiator, $iContent);
  144. if (!$this->query($sQuery))
  145. return false;
  146. if (BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $aConnection['mutual'])
  147. $this->removeConnection($iContent, $iInitiator);
  148. return true;
  149. }
  150. public function onDelete ($iId, $sField = 'initiator')
  151. {
  152. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `$sField` = ?", $iId);
  153. return $this->query($sQuery);
  154. }
  155. public function onModuleDelete ($sTable, $sFieldId, $sField = 'initiator')
  156. {
  157. return $this->onModuleDeleteCustom ($sTable, $sFieldId, $sField);
  158. }
  159. public function onModuleProfileDelete ($sModuleName, $sField = 'initiator')
  160. {
  161. return $this->onModuleDeleteCustom ('sys_profiles', 'id', $sField, $this->prepare(" AND `sys_profiles`.`type` = ? ", $sModuleName));
  162. }
  163. protected function onModuleDeleteCustom ($sTable, $sFieldId, $sField = 'initiator', $sWhere = '')
  164. {
  165. $sQuery = "DELETE `" . $this->_sTable . "` FROM `" . $this->_sTable . "` INNER JOIN `{$sTable}` WHERE `" . $this->_sTable . "`.`$sField` = `{$sTable}`.`{$sFieldId}` " . $sWhere;
  166. return $this->query($sQuery);
  167. }
  168. }
  169. /** @} */