BxDolObject.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 BxDolFactory implements iBxDolReplaceable
  14. {
  15. protected $_oTemplate = null;
  16. protected $_oQuery = null;
  17. protected $_iId = 0; ///< item id the action to be performed with
  18. protected $_sSystem = ''; ///< current system name
  19. protected $_aSystem = array(); ///< current system array
  20. protected $_aMarkers = array ();
  21. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  22. {
  23. parent::__construct();
  24. $aSystems = $this->getSystems();
  25. if(!isset($aSystems[$sSystem]))
  26. return;
  27. $this->_sSystem = $sSystem;
  28. $this->_aSystem = $aSystems[$sSystem];
  29. if ($oTemplate)
  30. $this->_oTemplate = $oTemplate;
  31. else
  32. $this->_oTemplate = BxDolTemplate::getInstance();
  33. if(!$this->isEnabled())
  34. return;
  35. if($iInit)
  36. $this->init($iId);
  37. }
  38. public function init($iId)
  39. {
  40. if(!$this->isEnabled())
  41. return false;
  42. if(empty($this->_iId) && $iId)
  43. $this->setId($iId);
  44. $this->addMarkers(array(
  45. 'object_id' => $this->getId(),
  46. 'user_id' => $this->_getAuthorId()
  47. ));
  48. return true;
  49. }
  50. public function getSystemId()
  51. {
  52. return $this->_aSystem['id'];
  53. }
  54. public function getSystemName()
  55. {
  56. return $this->_sSystem;
  57. }
  58. public function getSystemInfo()
  59. {
  60. return $this->_aSystem;
  61. }
  62. public function getId()
  63. {
  64. return $this->_iId;
  65. }
  66. public function setId($iId)
  67. {
  68. if($iId == $this->getId())
  69. return;
  70. $this->_iId = $iId;
  71. }
  72. public function isEnabled ()
  73. {
  74. return $this->_aSystem && (int)$this->_aSystem['is_on'] == 1;
  75. }
  76. /**
  77. * Interface functions for outer usage
  78. */
  79. public function getConditions($sMainTable, $sMainField)
  80. {
  81. if(!$this->isEnabled())
  82. return array();
  83. $sTable = isset($this->_aSystem['table_main']) ? $this->_aSystem['table_main'] : '';
  84. if(empty($sTable) || empty($sMainTable) || empty($sMainField))
  85. return array();
  86. return array(
  87. 'join' => array (
  88. 'objects_' . $this->_sSystem => array(
  89. 'type' => 'INNER',
  90. 'table' => $sTable,
  91. 'mainTable' => $sMainTable,
  92. 'mainField' => $sMainField,
  93. 'onField' => 'object_id',
  94. 'joinFields' => array('count'),
  95. ),
  96. ),
  97. );
  98. }
  99. public function getConditionsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  100. {
  101. if(!$this->isEnabled())
  102. return array();
  103. $sTableTrack = isset($this->_aSystem['table_track']) ? $this->_aSystem['table_track'] : '';
  104. if(empty($sTableTrack) || empty($sMainTable) || empty($sMainField))
  105. return array();
  106. return array(
  107. 'restriction' => array (
  108. 'objects_' . $this->_sSystem . '_author' => array(
  109. 'value' => $iAuthorId,
  110. 'field' => 'author_id',
  111. 'operator' => '=',
  112. 'table' => $sTableTrack,
  113. ),
  114. ),
  115. 'join' => array (
  116. 'objects_' . $this->_sSystem => array(
  117. 'type' => 'INNER',
  118. 'table' => $sTableTrack,
  119. 'mainTable' => $sMainTable,
  120. 'mainField' => $sMainField,
  121. 'onField' => 'object_id',
  122. 'joinFields' => array('author_id'),
  123. ),
  124. ),
  125. );
  126. }
  127. public function getSqlParts($sMainTable, $sMainField)
  128. {
  129. if(!$this->isEnabled())
  130. return array();
  131. return $this->_oQuery->getSqlParts($sMainTable, $sMainField);
  132. }
  133. public function getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  134. {
  135. if(!$this->isEnabled())
  136. return array();
  137. return $this->_oQuery->getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId);
  138. }
  139. public function addMarkers($aMarkers)
  140. {
  141. if(empty($aMarkers) || !is_array($aMarkers))
  142. return false;
  143. $this->_aMarkers = array_merge($this->_aMarkers, $aMarkers);
  144. return true;
  145. }
  146. /**
  147. * Permissions functions
  148. */
  149. public function checkAction ($sAction, $isPerformAction = false)
  150. {
  151. $iId = $this->_getAuthorId();
  152. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  153. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  154. }
  155. public function checkActionErrorMsg ($sAction)
  156. {
  157. $iId = $this->_getAuthorId();
  158. $a = checkActionModule($iId, $sAction, 'system');
  159. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  160. }
  161. /**
  162. * Actions' response functions
  163. */
  164. public function onObjectDelete($iObjectId = 0)
  165. {
  166. $this->_oQuery->deleteObjectEntries($iObjectId ? $iObjectId : $this->getId());
  167. }
  168. /**
  169. * Internal functions
  170. */
  171. protected function _getAuthorId ()
  172. {
  173. return isMember() ? bx_get_logged_profile_id() : 0;
  174. }
  175. protected function _getAuthorPassword ()
  176. {
  177. return isMember() ? $_COOKIE['memberPassword'] : "";
  178. }
  179. protected function _getAuthorIp ()
  180. {
  181. return getVisitorIP();
  182. }
  183. protected function _getAuthorInfo($iAuthorId = 0)
  184. {
  185. $oProfile = $this->_getAuthorObject($iAuthorId);
  186. return array(
  187. $oProfile->getDisplayName(),
  188. $oProfile->getUrl(),
  189. $oProfile->getThumb(),
  190. $oProfile->getUnit()
  191. );
  192. }
  193. protected function _getAuthorObject($iAuthorId = 0)
  194. {
  195. if($iAuthorId == 0)
  196. return BxDolProfileUndefined::getInstance();
  197. $oProfile = BxDolProfile::getInstance($iAuthorId);
  198. if(!$oProfile)
  199. $oProfile = BxDolProfileUndefined::getInstance();
  200. return $oProfile;
  201. }
  202. /**
  203. * Update Trigger table using data which is automatically gotten from object's internal table.
  204. */
  205. protected function _trigger()
  206. {
  207. if(!$this->_aSystem['trigger_table'])
  208. return false;
  209. $iId = $this->getId();
  210. if(!$iId)
  211. return false;
  212. return $this->_oQuery->updateTriggerTable($iId);
  213. }
  214. /**
  215. * Update (increment/decrement) Trigger table using provided value.
  216. */
  217. protected function _triggerValue($iValue)
  218. {
  219. if(!$this->_aSystem['trigger_table'])
  220. return false;
  221. $iId = $this->getId();
  222. if(!$iId)
  223. return false;
  224. return $this->_oQuery->updateTriggerTableValue($iId, $iValue);
  225. }
  226. /**
  227. * Replace provided markers in a string
  228. * @param $mixed string or array to replace markers in
  229. * @return string where all occured markers are replaced
  230. */
  231. protected function _replaceMarkers ($mixed)
  232. {
  233. return bx_replace_markers($mixed, $this->_aMarkers);
  234. }
  235. }
  236. /** @} */