BxDolObjectQuery.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  10. * @see BxDolObject
  11. */
  12. class BxDolObjectQuery extends BxDolDb
  13. {
  14. protected $_oModule;
  15. protected $_sTable;
  16. protected $_sTableTrack;
  17. protected $_sTriggerTable;
  18. protected $_sTriggerFieldId;
  19. protected $_sTriggerFieldAuthor;
  20. protected $_sTriggerFieldCount;
  21. protected $_sMethodGetEntry;
  22. public function __construct(&$oModule)
  23. {
  24. parent::__construct();
  25. $this->_oModule = $oModule;
  26. $aSystem = $this->_oModule->getSystemInfo();
  27. $this->_sTable = isset($aSystem['table_main']) ? $aSystem['table_main'] : '';
  28. $this->_sTableTrack = isset($aSystem['table_track']) ? $aSystem['table_track'] : '';
  29. $this->_sTriggerTable = isset($aSystem['trigger_table']) ? $aSystem['trigger_table'] : '';
  30. $this->_sTriggerFieldId = isset($aSystem['trigger_field_id']) ? $aSystem['trigger_field_id'] : '';
  31. $this->_sTriggerFieldAuthor = isset($aSystem['trigger_field_author']) ? $aSystem['trigger_field_author'] : '';
  32. $this->_sTriggerFieldCount = isset($aSystem['trigger_field_count']) ? $aSystem['trigger_field_count'] : '';
  33. }
  34. /**
  35. * Get SQL parts for main table.
  36. */
  37. public function getSqlParts($sMainTable, $sMainField)
  38. {
  39. if(empty($this->_sTable) || empty($sMainTable) || empty($sMainField))
  40. return array();
  41. return array (
  42. 'fields' => ", `{$this->_sTable}`.`count` as `count` ",
  43. 'join' => " LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  44. );
  45. }
  46. /**
  47. * Get SQL parts for track table.
  48. */
  49. public function getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  50. {
  51. if(empty($this->_sTableTrack) || empty($sMainTable) || empty($sMainField))
  52. return array();
  53. return array (
  54. 'fields' => ", `{$this->_sTableTrack}`.`author_id` as `favorer_id` ",
  55. 'where' => $this->prepareAsString(" AND `{$this->_sTableTrack}`.`author_id` = ?", $iAuthorId),
  56. 'join' => " LEFT JOIN `{$this->_sTableTrack}` ON (`{$this->_sTableTrack}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  57. );
  58. }
  59. public function isPerformed($iObjectId, $iAuthorId)
  60. {
  61. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  62. return (int)$this->getOne($sQuery) != 0;
  63. }
  64. public function getPerformedBy($iObjectId)
  65. {
  66. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  67. return $this->getColumn($sQuery);
  68. }
  69. public function deleteObjectEntries($iObjectId)
  70. {
  71. if(!empty($this->_sTable)) {
  72. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
  73. if($this->query($sQuery))
  74. $this->query("OPTIMIZE TABLE {$this->_sTable}");
  75. }
  76. if(!empty($this->_sTableTrack)) {
  77. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  78. if($this->query($sQuery))
  79. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  80. }
  81. }
  82. public function getObjectAuthorId($iId)
  83. {
  84. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  85. return (int)$this->getOne($sQuery);
  86. }
  87. public function getObjectCount($iId)
  88. {
  89. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldCount}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  90. return (int)$this->getOne($sQuery);
  91. }
  92. public function updateTriggerTable($iObjectId)
  93. {
  94. if(empty($this->_sMethodGetEntry))
  95. return false;
  96. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  97. if(empty($aEntry) || !is_array($aEntry))
  98. return false;
  99. return $this->_updateTriggerTable($iObjectId, $aEntry);
  100. }
  101. protected function _updateTriggerTable($iObjectId, $aEntry)
  102. {
  103. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  104. return (int)$this->query($sQuery) > 0;
  105. }
  106. }
  107. /** @} */