BxDolObjectQuery.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 $_sTableTrackFieldObject;
  18. protected $_sTableTrackFieldAuthor;
  19. protected $_sTableTrackFieldDate;
  20. protected $_sTriggerTable;
  21. protected $_sTriggerFieldId;
  22. protected $_sTriggerFieldAuthor;
  23. protected $_sTriggerFieldCount;
  24. protected $_sMethodGetEntry;
  25. public function __construct(&$oModule)
  26. {
  27. parent::__construct();
  28. $this->_oModule = $oModule;
  29. $aSystem = $this->_oModule->getSystemInfo();
  30. $this->_sTable = isset($aSystem['table_main']) ? $aSystem['table_main'] : '';
  31. $this->_sTableTrack = isset($aSystem['table_track']) ? $aSystem['table_track'] : '';
  32. $this->_sTableTrackFieldObject = 'object_id';
  33. $this->_sTableTrackFieldAuthor = 'author_id';
  34. $this->_sTableTrackFieldDate = 'date';
  35. $this->_sTriggerTable = isset($aSystem['trigger_table']) ? $aSystem['trigger_table'] : '';
  36. $this->_sTriggerFieldId = isset($aSystem['trigger_field_id']) ? $aSystem['trigger_field_id'] : '';
  37. $this->_sTriggerFieldAuthor = isset($aSystem['trigger_field_author']) ? $aSystem['trigger_field_author'] : '';
  38. $this->_sTriggerFieldCount = isset($aSystem['trigger_field_count']) ? $aSystem['trigger_field_count'] : '';
  39. }
  40. /**
  41. * Get SQL parts for main table.
  42. */
  43. public function getSqlParts($sMainTable, $sMainField)
  44. {
  45. if(empty($this->_sTable) || empty($sMainTable) || empty($sMainField))
  46. return array();
  47. return array (
  48. 'fields' => ", `{$this->_sTable}`.`count` as `count` ",
  49. 'join' => " LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  50. );
  51. }
  52. /**
  53. * Get SQL parts for track table.
  54. */
  55. public function getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  56. {
  57. if(empty($this->_sTableTrack) || empty($sMainTable) || empty($sMainField))
  58. return array();
  59. return array (
  60. 'fields' => ", `{$this->_sTableTrack}`.`{$this->_sTableTrackFieldAuthor}` as `{$this->_sTableTrackFieldAuthor}` ",
  61. 'where' => $this->prepareAsString(" AND `{$this->_sTableTrack}`.`{$this->_sTableTrackFieldAuthor}` = ?", $iAuthorId),
  62. 'join' => " LEFT JOIN `{$this->_sTableTrack}` ON (`{$this->_sTableTrack}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  63. );
  64. }
  65. public function getSqlPartsTrackAuthor($sMainTable, $sMainField, $iObjectId = 0)
  66. {
  67. if(empty($this->_sTableTrack) || empty($sMainTable) || empty($sMainField))
  68. return array();
  69. return array (
  70. 'fields' => ", `{$this->_sTableTrack}`.`object_id` as `object_id` ",
  71. 'where' => $this->prepareAsString(" AND `{$this->_sTableTrack}`.`object_id` = ?", $iObjectId),
  72. 'join' => " LEFT JOIN `{$this->_sTableTrack}` ON (`{$this->_sTableTrack}`.`{$this->_sTableTrackFieldAuthor}` = `{$sMainTable}`.`{$sMainField}`) ",
  73. );
  74. }
  75. public function isPerformed($iObjectId, $iAuthorId)
  76. {
  77. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  78. return (int)$this->getOne($sQuery) != 0;
  79. }
  80. public function getPerformedBy($iObjectId, $iStart = 0, $iPerPage = 0)
  81. {
  82. $sLimitClause = "";
  83. if(!empty($iPerPage))
  84. $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $iStart, $iPerPage);
  85. $sQuery = $this->prepare("SELECT `tt`.`author_id` FROM `{$this->_sTableTrack}` AS `tt` INNER JOIN `sys_profiles` AS `tp` ON `tt`.`author_id`=`tp`.`id` AND `tp`.`status`='active' WHERE `tt`.`object_id` = ?" . $sLimitClause, $iObjectId);
  86. return $this->getColumn($sQuery);
  87. }
  88. public function getData($iObjectId)
  89. {
  90. $sQuery = $this->prepare("SELECT * FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  91. return $this->getAll($sQuery);
  92. }
  93. public function getTrack($iObjectId, $iAuthorId)
  94. {
  95. return $this->getRow("SELECT * FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id AND `author_id` = :author_id LIMIT 1", array(
  96. 'object_id' => $iObjectId,
  97. 'author_id' => $iAuthorId
  98. ));
  99. }
  100. public function getTrackBy($aParams = array())
  101. {
  102. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  103. $sWhereClause = $sOrderByClause = $sLimitClause = "";
  104. $aBindings = array();
  105. switch($aParams['type']) {
  106. case 'id':
  107. $aMethod['name'] = 'getRow';
  108. $aBindings['id'] = $aParams['id'];
  109. $sWhereClause .= " AND `id`=:id";
  110. break;
  111. case 'all':
  112. break;
  113. }
  114. if(!empty($aParams['order_by']))
  115. $sOrderByClause = " ORDER BY " . $aParams['order_by'] . (!empty($aParams['order_way']) ? " " . $aParams['order_way'] : "");
  116. if(isset($aParams['start']) && !empty($aParams['per_page']))
  117. $sLimitClause = " LIMIT " . $aParams['start'] . ", " . $aParams['per_page'];
  118. $aMethod['params'][0] = "SELECT * FROM `{$this->_sTableTrack}` WHERE 1 " . $sWhereClause . $sOrderByClause . $sLimitClause;
  119. $aMethod['params'][] = $aBindings;
  120. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  121. }
  122. public function deleteObjectEntries($iObjectId)
  123. {
  124. if(!empty($this->_sTable)) {
  125. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  126. if($this->query($sQuery))
  127. $this->query("OPTIMIZE TABLE `{$this->_sTable}`");
  128. }
  129. if(!empty($this->_sTableTrack))
  130. $this->pruningByObject($iObjectId);
  131. }
  132. public function deleteAuthorEntries($iAuthorId)
  133. {
  134. if(empty($this->_sTableTrack))
  135. return;
  136. $bTable = !empty($this->_sTable);
  137. $bTableTrigger = !empty($this->_sTriggerTable) && !empty($this->_sTriggerFieldCount);
  138. if($bTable || $bTableTrigger) {
  139. $aTracks = $this->getAll("SELECT * FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=:author_id", array('author_id' => $iAuthorId));
  140. foreach($aTracks as $aTrack) {
  141. if($bTable)
  142. $this->_deleteAuthorEntriesTableMain($aTrack);
  143. /**
  144. * Note. It's essential that Trigger Table is updated at the end,
  145. * because it may require updated data from main ($this->_sTable) table.
  146. */
  147. if($bTableTrigger)
  148. $this->_deleteAuthorEntriesTableTrigger($aTrack);
  149. }
  150. }
  151. $this->pruningByAuthor($iAuthorId);
  152. }
  153. public function getObjectInfo($iId)
  154. {
  155. if(empty($this->_sTriggerFieldId))
  156. return [];
  157. return $this->getRow("SELECT * FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = :id LIMIT 1", [
  158. 'id' => $iId
  159. ]);
  160. }
  161. public function getObjectAuthorId($iId)
  162. {
  163. if(empty($this->_sTriggerFieldAuthor))
  164. return 0;
  165. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  166. return (int)$this->getOne($sQuery);
  167. }
  168. public function getObjectCount($iId)
  169. {
  170. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldCount}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  171. return (int)$this->getOne($sQuery);
  172. }
  173. public function updateMainTableValue($iObjectId, $iValue)
  174. {
  175. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + ? WHERE `object_id` = ?", (int)$iValue, $iObjectId);
  176. return (int)$this->query($sQuery) > 0;
  177. }
  178. public function updateTriggerTable($iObjectId)
  179. {
  180. if(empty($this->_sMethodGetEntry))
  181. return false;
  182. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  183. if(empty($aEntry) || !is_array($aEntry))
  184. return false;
  185. return $this->_updateTriggerTable($iObjectId, $aEntry);
  186. }
  187. public function pruningByObject($iObjectId)
  188. {
  189. if($this->query("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id", ['object_id' => $iObjectId]))
  190. $this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  191. }
  192. public function pruningByAuthor($iAuthorId)
  193. {
  194. if($this->query("DELETE FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}` = :author_id", ['author_id' => $iAuthorId]))
  195. $this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  196. }
  197. public function pruningByDate($iDate)
  198. {
  199. $iResult = (int)$this->query("DELETE FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldDate}` < (UNIX_TIMESTAMP() - :date)", ['date' => $iDate]);
  200. if($iResult)
  201. $this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
  202. return $iResult;
  203. }
  204. /**
  205. * Is used instead of 'updateTriggerTable' when trigger table contains
  206. * only one field related to this object (View, Vote, Comment, etc).
  207. * For example, it's used in Favourite (simple mode w\o lists) and Feature objects.
  208. */
  209. public function updateTriggerTableValue($iObjectId, $iValue)
  210. {
  211. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + ? WHERE `{$this->_sTriggerFieldId}` = ?", (int)$iValue, $iObjectId);
  212. return (int)$this->query($sQuery) > 0;
  213. }
  214. protected function _updateTriggerTable($iObjectId, $aEntry)
  215. {
  216. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  217. return (int)$this->query($sQuery) > 0;
  218. }
  219. protected function _deleteAuthorEntriesTableMain($aTrack)
  220. {
  221. return $this->updateMainTableValue($aTrack['object_id'], -1);
  222. }
  223. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  224. {
  225. return $this->updateTriggerTableValue($aTrack['object_id'], -1);
  226. }
  227. }
  228. /** @} */