BxDolObject.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') 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. * Base class for all "Object" classes.
  11. * Child classes usually represents high level programming constructions to generate ready 'objects' functionality, like Comments, Votings, Forms.
  12. */
  13. class BxDolObject extends BxDol
  14. {
  15. protected $_oQuery = null;
  16. protected $_iId = 0; ///< item id the action to be performed with
  17. protected $_sSystem = ''; ///< current system name
  18. protected $_aSystem = array(); ///< current system array
  19. protected $_aMarkers = array ();
  20. public function __construct($sSystem, $iId, $iInit = 1)
  21. {
  22. parent::__construct();
  23. $aSystems = $this->getSystems();
  24. if(!isset($aSystems[$sSystem]))
  25. return;
  26. $this->_sSystem = $sSystem;
  27. $this->_aSystem = $aSystems[$sSystem];
  28. if(!$this->isEnabled())
  29. return;
  30. if($iInit)
  31. $this->init($iId);
  32. }
  33. public function init($iId)
  34. {
  35. if(!$this->isEnabled())
  36. return false;
  37. if(empty($this->_iId) && $iId)
  38. $this->setId($iId);
  39. $this->addMarkers(array(
  40. 'object_id' => $this->getId(),
  41. 'user_id' => $this->_getAuthorId()
  42. ));
  43. return true;
  44. }
  45. public function getSystemId()
  46. {
  47. return $this->_aSystem['id'];
  48. }
  49. public function getSystemName()
  50. {
  51. return $this->_sSystem;
  52. }
  53. public function getSystemInfo()
  54. {
  55. return $this->_aSystem;
  56. }
  57. public function getId()
  58. {
  59. return $this->_iId;
  60. }
  61. public function setId($iId)
  62. {
  63. if($iId == $this->getId())
  64. return;
  65. $this->_iId = $iId;
  66. }
  67. public function isEnabled ()
  68. {
  69. return $this->_aSystem && (int)$this->_aSystem['is_on'] == 1;
  70. }
  71. /**
  72. * Interface functions for outer usage
  73. */
  74. public function getSqlParts($sMainTable, $sMainField)
  75. {
  76. if(!$this->isEnabled())
  77. return array();
  78. return $this->_oQuery->getSqlParts($sMainTable, $sMainField);
  79. }
  80. public function addMarkers($aMarkers)
  81. {
  82. if(empty($aMarkers) || !is_array($aMarkers))
  83. return false;
  84. $this->_aMarkers = array_merge($this->_aMarkers, $aMarkers);
  85. return true;
  86. }
  87. /**
  88. * Permissions functions
  89. */
  90. public function checkAction ($sAction, $isPerformAction = false)
  91. {
  92. $iId = $this->_getAuthorId();
  93. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  94. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  95. }
  96. public function checkActionErrorMsg ($sAction)
  97. {
  98. $iId = $this->_getAuthorId();
  99. $a = checkActionModule($iId, $sAction, 'system');
  100. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  101. }
  102. /**
  103. * Actions' response functions
  104. */
  105. public function onObjectDelete($iObjectId = 0)
  106. {
  107. $this->_oQuery->deleteObjectEntries($iObjectId ? $iObjectId : $this->getId());
  108. }
  109. /**
  110. * Internal functions
  111. */
  112. protected function _getAuthorId ()
  113. {
  114. return isMember() ? bx_get_logged_profile_id() : 0;
  115. }
  116. protected function _getAuthorPassword ()
  117. {
  118. return isMember() ? $_COOKIE['memberPassword'] : "";
  119. }
  120. protected function _getAuthorIp ()
  121. {
  122. return getVisitorIP();
  123. }
  124. protected function _getAuthorInfo($iAuthorId = 0)
  125. {
  126. $oProfile = $this->_getAuthorObject($iAuthorId);
  127. return array(
  128. $oProfile->getDisplayName(),
  129. $oProfile->getUrl(),
  130. $oProfile->getThumb(),
  131. $oProfile->getUnit()
  132. );
  133. }
  134. protected function _getAuthorObject($iAuthorId = 0)
  135. {
  136. if($iAuthorId == 0)
  137. return BxDolProfileUndefined::getInstance();
  138. $oProfile = BxDolProfile::getInstance($iAuthorId);
  139. if(!$oProfile)
  140. $oProfile = BxDolProfileUndefined::getInstance();
  141. return $oProfile;
  142. }
  143. protected function _trigger()
  144. {
  145. if(!$this->_aSystem['trigger_table'])
  146. return false;
  147. $iId = $this->getId();
  148. if(!$iId)
  149. return false;
  150. return $this->_oQuery->updateTriggerTable($iId);
  151. }
  152. /**
  153. * Replace provided markers in a string
  154. * @param $mixed string or array to replace markers in
  155. * @return string where all occured markers are replaced
  156. */
  157. protected function _replaceMarkers ($mixed)
  158. {
  159. return bx_replace_markers($mixed, $this->_aMarkers);
  160. }
  161. }
  162. /** @} */