BxDolObjectQuery.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 getTrackBy($aParams = array())
  79. {
  80. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  81. $sWhereClause = $sOrderByClause = $sLimitClause = "";
  82. $aBindings = array();
  83. switch($aParams['type']) {
  84. case 'id':
  85. $aMethod['name'] = 'getRow';
  86. $aBindings['id'] = $aParams['id'];
  87. $sWhereClause .= " AND `id`=:id";
  88. break;
  89. case 'all':
  90. break;
  91. }
  92. if(!empty($aParams['order_by']))
  93. $sOrderByClause = " ORDER BY " . $aParams['order_by'] . (!empty($aParams['order_way']) ? " " . $aParams['order_way'] : "");
  94. if(isset($aParams['start']) && !empty($aParams['per_page']))
  95. $sLimitClause = " LIMIT " . $aParams['start'] . ", " . $aParams['per_page'];
  96. $aMethod['params'][0] = "SELECT * FROM `{$this->_sTableTrack}` WHERE 1 " . $sWhereClause . $sOrderByClause . $sLimitClause;
  97. $aMethod['params'][] = $aBindings;
  98. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  99. }
  100. public function deleteObjectEntries($iObjectId)
  101. {
  102. if(!empty($this->_sTable)) {
  103. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  104. if($this->query($sQuery))
  105. $this->query("OPTIMIZE TABLE `{$this->_sTable}`");
  106. }
  107. if(!empty($this->_sTableTrack)) {
  108. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  109. if($this->query($sQuery))
  110. $this->query ("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  111. }
  112. }
  113. public function deleteAuthorEntries($iAuthorId)
  114. {
  115. if(empty($this->_sTableTrack))
  116. return;
  117. $bTable = !empty($this->_sTable);
  118. $bTableTrigger = !empty($this->_sTriggerTable) && !empty($this->_sTriggerFieldCount);
  119. if($bTable || $bTableTrigger) {
  120. $aTracks = $this->getAll("SELECT * FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=:author_id", array('author_id' => $iAuthorId));
  121. foreach($aTracks as $aTrack) {
  122. if($bTable)
  123. $this->_deleteAuthorEntriesTableMain($aTrack);
  124. /**
  125. * Note. It's essential that Trigger Table is updated at the end,
  126. * because it may require updated data from main ($this->_sTable) table.
  127. */
  128. if($bTableTrigger)
  129. $this->_deleteAuthorEntriesTableTrigger($aTrack);
  130. }
  131. }
  132. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=?", $iAuthorId);
  133. if($this->query($sQuery))
  134. $this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  135. }
  136. public function getObjectAuthorId($iId)
  137. {
  138. if(empty($this->_sTriggerFieldAuthor))
  139. return 0;
  140. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  141. return (int)$this->getOne($sQuery);
  142. }
  143. public function getObjectCount($iId)
  144. {
  145. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldCount}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  146. return (int)$this->getOne($sQuery);
  147. }
  148. public function updateMainTableValue($iObjectId, $iValue)
  149. {
  150. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + ? WHERE `object_id` = ?", (int)$iValue, $iObjectId);
  151. return (int)$this->query($sQuery) > 0;
  152. }
  153. public function updateTriggerTable($iObjectId)
  154. {
  155. if(empty($this->_sMethodGetEntry))
  156. return false;
  157. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  158. if(empty($aEntry) || !is_array($aEntry))
  159. return false;
  160. return $this->_updateTriggerTable($iObjectId, $aEntry);
  161. }
  162. /**
  163. * Is used instead of 'updateTriggerTable' when trigger table contains
  164. * only one field related to this object (View, Vote, Comment, etc).
  165. * For example, it's used in Favourite and Feature objects.
  166. */
  167. public function updateTriggerTableValue($iObjectId, $iValue)
  168. {
  169. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + ? WHERE `{$this->_sTriggerFieldId}` = ?", (int)$iValue, $iObjectId);
  170. return (int)$this->query($sQuery) > 0;
  171. }
  172. protected function _updateTriggerTable($iObjectId, $aEntry)
  173. {
  174. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  175. return (int)$this->query($sQuery) > 0;
  176. }
  177. protected function _deleteAuthorEntriesTableMain($aTrack)
  178. {
  179. return $this->updateMainTableValue($aTrack['object_id'], -1);
  180. }
  181. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  182. {
  183. return $this->updateTriggerTableValue($aTrack['object_id'], -1);
  184. }
  185. }
  186. /** @} */