BxDolObjectQuery.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public function getSqlParts($sMainTable, $sMainField)
  35. {
  36. if(empty($sMainTable) || empty($sMainField))
  37. return array();
  38. return array (
  39. 'fields' => ", `{$this->_sTable}`.`count` as `count` ",
  40. 'join' => " LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  41. );
  42. }
  43. public function getSqlPartsTrack($sMainTable, $sMainField)
  44. {
  45. if(empty($sMainTable) || empty($sMainField))
  46. return array();
  47. return array (
  48. 'fields' => ", `{$this->_sTableTrack}`.`author_id` as `favorer_id` ",
  49. 'join' => " LEFT JOIN `{$this->_sTableTrack}` ON (`{$this->_sTableTrack}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  50. );
  51. }
  52. public function isPerformed($iObjectId, $iAuthorId)
  53. {
  54. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  55. return (int)$this->getOne($sQuery) != 0;
  56. }
  57. public function getPerformedBy($iObjectId)
  58. {
  59. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  60. return $this->getColumn($sQuery);
  61. }
  62. public function deleteObjectEntries($iObjectId)
  63. {
  64. if(!empty($this->_sTable)) {
  65. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
  66. if($this->query($sQuery))
  67. $this->query("OPTIMIZE TABLE {$this->_sTable}");
  68. }
  69. if(!empty($this->_sTableTrack)) {
  70. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  71. if($this->query($sQuery))
  72. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  73. }
  74. }
  75. public function getObjectAuthorId($iId)
  76. {
  77. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  78. return $this->getOne($sQuery);
  79. }
  80. public function updateTriggerTable($iObjectId)
  81. {
  82. if(empty($this->_sMethodGetEntry))
  83. return false;
  84. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  85. if(empty($aEntry) || !is_array($aEntry))
  86. return false;
  87. return $this->_updateTriggerTable($iObjectId, $aEntry);
  88. }
  89. protected function _updateTriggerTable($iObjectId, $aEntry)
  90. {
  91. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  92. return (int)$this->query($sQuery) > 0;
  93. }
  94. }
  95. /** @} */