BxDolObjectQuery.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 isPerformed($iObjectId, $iAuthorId)
  44. {
  45. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  46. return (int)$this->getOne($sQuery) != 0;
  47. }
  48. public function getPerformedBy($iObjectId)
  49. {
  50. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  51. return $this->getColumn($sQuery);
  52. }
  53. public function deleteObjectEntries($iObjectId)
  54. {
  55. if(!empty($this->_sTable)) {
  56. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
  57. if($this->query($sQuery))
  58. $this->query("OPTIMIZE TABLE {$this->_sTable}");
  59. }
  60. if(!empty($this->_sTableTrack)) {
  61. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  62. if($this->query($sQuery))
  63. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  64. }
  65. }
  66. public function getObjectAuthorId($iId)
  67. {
  68. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  69. return $this->getOne($sQuery);
  70. }
  71. public function updateTriggerTable($iObjectId)
  72. {
  73. if(empty($this->_sMethodGetEntry))
  74. return false;
  75. $aEntry = $this->{$this->_sMethodGetEntry}($iObjectId);
  76. if(empty($aEntry) || !is_array($aEntry))
  77. return false;
  78. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
  79. return (int)$this->query($sQuery) > 0;
  80. }
  81. }
  82. /** @} */