BxDolObjectQuery.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 deleteObjectEntries($iObjectId)
  72. {
  73. if(!empty($this->_sTable)) {
  74. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  75. if($this->query($sQuery))
  76. $this->query("OPTIMIZE TABLE `{$this->_sTable}`");
  77. }
  78. if(!empty($this->_sTableTrack)) {
  79. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  80. if($this->query($sQuery))
  81. $this->query ("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  82. }
  83. }
  84. public function deleteAuthorEntries($iAuthorId)
  85. {
  86. if(empty($this->_sTableTrack))
  87. return;
  88. $bTable = !empty($this->_sTable);
  89. $bTableTrigger = !empty($this->_sTriggerTable) && !empty($this->_sTriggerFieldCount);
  90. if($bTable || $bTableTrigger) {
  91. $aTracks = $this->getAll("SELECT * FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=:author_id", array('author_id' => $iAuthorId));
  92. foreach($aTracks as $aTrack) {
  93. if($bTable)
  94. $this->_deleteAuthorEntriesTableMain($aTrack);
  95. /**
  96. * Note. It's essential that Trigger Table is updated at the end,
  97. * because it may require updated data from main ($this->_sTable) table.
  98. */
  99. if($bTableTrigger)
  100. $this->_deleteAuthorEntriesTableTrigger($aTrack);
  101. }
  102. }
  103. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=?", $iAuthorId);
  104. if($this->query($sQuery))
  105. $this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  106. }
  107. public function getObjectAuthorId($iId)
  108. {
  109. if(empty($this->_sTriggerFieldAuthor))
  110. return 0;
  111. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  112. return (int)$this->getOne($sQuery);
  113. }
  114. public function getObjectCount($iId)
  115. {
  116. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldCount}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  117. return (int)$this->getOne($sQuery);
  118. }
  119. public function updateMainTableValue($iObjectId, $iValue)
  120. {
  121. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + ? WHERE `object_id` = ?", (int)$iValue, $iObjectId);
  122. return (int)$this->query($sQuery) > 0;
  123. }
  124. public function updateTriggerTable($iObjectId)
  125. {
  126. if(empty($this->_sMethodGetEntry))
  127. return false;
  128. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  129. if(empty($aEntry) || !is_array($aEntry))
  130. return false;
  131. return $this->_updateTriggerTable($iObjectId, $aEntry);
  132. }
  133. public function updateTriggerTableValue($iObjectId, $iValue)
  134. {
  135. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + ? WHERE `{$this->_sTriggerFieldId}` = ?", (int)$iValue, $iObjectId);
  136. return (int)$this->query($sQuery) > 0;
  137. }
  138. protected function _updateTriggerTable($iObjectId, $aEntry)
  139. {
  140. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  141. return (int)$this->query($sQuery) > 0;
  142. }
  143. protected function _deleteAuthorEntriesTableMain($aTrack)
  144. {
  145. return $this->updateMainTableValue($aTrack['object_id'], -1);
  146. }
  147. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  148. {
  149. return $this->updateTriggerTableValue($aTrack['object_id'], -1);
  150. }
  151. }
  152. /** @} */