BxDolObjectQuery.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * @see BxDolObject
  11. */
  12. class BxDolObjectQuery extends BxDolDb
  13. {
  14. protected $_oModule;
  15. protected $_sTable;
  16. protected $_sTableTrack;
  17. protected $_sTableTrackFieldAuthor;
  18. protected $_sTriggerTable;
  19. protected $_sTriggerFieldId;
  20. protected $_sTriggerFieldAuthor;
  21. protected $_sTriggerFieldCount;
  22. protected $_sMethodGetEntry;
  23. public function __construct(&$oModule)
  24. {
  25. parent::__construct();
  26. $this->_oModule = $oModule;
  27. $aSystem = $this->_oModule->getSystemInfo();
  28. $this->_sTable = isset($aSystem['table_main']) ? $aSystem['table_main'] : '';
  29. $this->_sTableTrack = isset($aSystem['table_track']) ? $aSystem['table_track'] : '';
  30. $this->_sTableTrackFieldAuthor = 'author_id';
  31. $this->_sTriggerTable = isset($aSystem['trigger_table']) ? $aSystem['trigger_table'] : '';
  32. $this->_sTriggerFieldId = isset($aSystem['trigger_field_id']) ? $aSystem['trigger_field_id'] : '';
  33. $this->_sTriggerFieldAuthor = isset($aSystem['trigger_field_author']) ? $aSystem['trigger_field_author'] : '';
  34. $this->_sTriggerFieldCount = isset($aSystem['trigger_field_count']) ? $aSystem['trigger_field_count'] : '';
  35. }
  36. /**
  37. * Get SQL parts for main table.
  38. */
  39. public function getSqlParts($sMainTable, $sMainField)
  40. {
  41. if(empty($this->_sTable) || empty($sMainTable) || empty($sMainField))
  42. return array();
  43. return array (
  44. 'fields' => ", `{$this->_sTable}`.`count` as `count` ",
  45. 'join' => " LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  46. );
  47. }
  48. /**
  49. * Get SQL parts for track table.
  50. */
  51. public function getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  52. {
  53. if(empty($this->_sTableTrack) || empty($sMainTable) || empty($sMainField))
  54. return array();
  55. return array (
  56. 'fields' => ", `{$this->_sTableTrack}`.`author_id` as `favorer_id` ",
  57. 'where' => $this->prepareAsString(" AND `{$this->_sTableTrack}`.`author_id` = ?", $iAuthorId),
  58. 'join' => " LEFT JOIN `{$this->_sTableTrack}` ON (`{$this->_sTableTrack}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  59. );
  60. }
  61. public function isPerformed($iObjectId, $iAuthorId)
  62. {
  63. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  64. return (int)$this->getOne($sQuery) != 0;
  65. }
  66. public function getPerformedBy($iObjectId)
  67. {
  68. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  69. return $this->getColumn($sQuery);
  70. }
  71. public function getTrack($iObjectId, $iAuthorId)
  72. {
  73. return $this->getRow("SELECT * FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id AND `author_id` = :author_id LIMIT 1", array(
  74. 'object_id' => $iObjectId,
  75. 'author_id' => $iAuthorId
  76. ));
  77. }
  78. public function deleteObjectEntries($iObjectId)
  79. {
  80. if(!empty($this->_sTable)) {
  81. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  82. if($this->query($sQuery))
  83. $this->query("OPTIMIZE TABLE `{$this->_sTable}`");
  84. }
  85. if(!empty($this->_sTableTrack)) {
  86. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  87. if($this->query($sQuery))
  88. $this->query ("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  89. }
  90. }
  91. public function deleteAuthorEntries($iAuthorId)
  92. {
  93. if(empty($this->_sTableTrack))
  94. return;
  95. $bTable = !empty($this->_sTable);
  96. $bTableTrigger = !empty($this->_sTriggerTable) && !empty($this->_sTriggerFieldCount);
  97. if($bTable || $bTableTrigger) {
  98. $aTracks = $this->getAll("SELECT * FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=:author_id", array('author_id' => $iAuthorId));
  99. foreach($aTracks as $aTrack) {
  100. if($bTable)
  101. $this->_deleteAuthorEntriesTableMain($aTrack);
  102. /**
  103. * Note. It's essential that Trigger Table is updated at the end,
  104. * because it may require updated data from main ($this->_sTable) table.
  105. */
  106. if($bTableTrigger)
  107. $this->_deleteAuthorEntriesTableTrigger($aTrack);
  108. }
  109. }
  110. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=?", $iAuthorId);
  111. if($this->query($sQuery))
  112. $this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  113. }
  114. public function getObjectAuthorId($iId)
  115. {
  116. if(empty($this->_sTriggerFieldAuthor))
  117. return 0;
  118. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  119. return (int)$this->getOne($sQuery);
  120. }
  121. public function getObjectCount($iId)
  122. {
  123. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldCount}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  124. return (int)$this->getOne($sQuery);
  125. }
  126. public function updateMainTableValue($iObjectId, $iValue)
  127. {
  128. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + ? WHERE `object_id` = ?", (int)$iValue, $iObjectId);
  129. return (int)$this->query($sQuery) > 0;
  130. }
  131. public function updateTriggerTable($iObjectId)
  132. {
  133. if(empty($this->_sMethodGetEntry))
  134. return false;
  135. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  136. if(empty($aEntry) || !is_array($aEntry))
  137. return false;
  138. return $this->_updateTriggerTable($iObjectId, $aEntry);
  139. }
  140. /**
  141. * Is used instead of 'updateTriggerTable' when trigger table contains
  142. * only one field related to this object (View, Vote, Comment, etc).
  143. * For example, it's used in Favourite and Feature objects.
  144. */
  145. public function updateTriggerTableValue($iObjectId, $iValue)
  146. {
  147. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + ? WHERE `{$this->_sTriggerFieldId}` = ?", (int)$iValue, $iObjectId);
  148. return (int)$this->query($sQuery) > 0;
  149. }
  150. protected function _updateTriggerTable($iObjectId, $aEntry)
  151. {
  152. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  153. return (int)$this->query($sQuery) > 0;
  154. }
  155. protected function _deleteAuthorEntriesTableMain($aTrack)
  156. {
  157. return $this->updateMainTableValue($aTrack['object_id'], -1);
  158. }
  159. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  160. {
  161. return $this->updateTriggerTableValue($aTrack['object_id'], -1);
  162. }
  163. }
  164. /** @} */