BxDolObject.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 getConditions($sMainTable, $sMainField)
  75. {
  76. if(!$this->isEnabled())
  77. return array();
  78. $sTable = isset($this->_aSystem['table_main']) ? $this->_aSystem['table_main'] : '';
  79. if(empty($sTable) || empty($sMainTable) || empty($sMainField))
  80. return array();
  81. return array(
  82. 'join' => array (
  83. 'objects_' . $this->_sSystem => array(
  84. 'type' => 'INNER',
  85. 'table' => $sTable,
  86. 'mainTable' => $sMainTable,
  87. 'mainField' => $sMainField,
  88. 'onField' => 'object_id',
  89. 'joinFields' => array('count'),
  90. ),
  91. ),
  92. );
  93. }
  94. public function getConditionsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  95. {
  96. if(!$this->isEnabled())
  97. return array();
  98. $sTableTrack = isset($this->_aSystem['table_track']) ? $this->_aSystem['table_track'] : '';
  99. if(empty($sTableTrack) || empty($sMainTable) || empty($sMainField))
  100. return array();
  101. return array(
  102. 'restriction' => array (
  103. 'objects_' . $this->_sSystem . '_author' => array(
  104. 'value' => $iAuthorId,
  105. 'field' => 'author_id',
  106. 'operator' => '=',
  107. 'table' => $sTableTrack,
  108. ),
  109. ),
  110. 'join' => array (
  111. 'objects_' . $this->_sSystem => array(
  112. 'type' => 'INNER',
  113. 'table' => $sTableTrack,
  114. 'mainTable' => $sMainTable,
  115. 'mainField' => $sMainField,
  116. 'onField' => 'object_id',
  117. 'joinFields' => array('author_id'),
  118. ),
  119. ),
  120. );
  121. }
  122. public function getSqlParts($sMainTable, $sMainField)
  123. {
  124. if(!$this->isEnabled())
  125. return array();
  126. return $this->_oQuery->getSqlParts($sMainTable, $sMainField);
  127. }
  128. public function getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  129. {
  130. if(!$this->isEnabled())
  131. return array();
  132. return $this->_oQuery->getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId);
  133. }
  134. public function addMarkers($aMarkers)
  135. {
  136. if(empty($aMarkers) || !is_array($aMarkers))
  137. return false;
  138. $this->_aMarkers = array_merge($this->_aMarkers, $aMarkers);
  139. return true;
  140. }
  141. /**
  142. * Permissions functions
  143. */
  144. public function checkAction ($sAction, $isPerformAction = false)
  145. {
  146. $iId = $this->_getAuthorId();
  147. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  148. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  149. }
  150. public function checkActionErrorMsg ($sAction)
  151. {
  152. $iId = $this->_getAuthorId();
  153. $a = checkActionModule($iId, $sAction, 'system');
  154. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  155. }
  156. /**
  157. * Actions' response functions
  158. */
  159. public function onObjectDelete($iObjectId = 0)
  160. {
  161. $this->_oQuery->deleteObjectEntries($iObjectId ? $iObjectId : $this->getId());
  162. }
  163. /**
  164. * Internal functions
  165. */
  166. protected function _getAuthorId ()
  167. {
  168. return isMember() ? bx_get_logged_profile_id() : 0;
  169. }
  170. protected function _getAuthorPassword ()
  171. {
  172. return isMember() ? $_COOKIE['memberPassword'] : "";
  173. }
  174. protected function _getAuthorIp ()
  175. {
  176. return getVisitorIP();
  177. }
  178. protected function _getAuthorInfo($iAuthorId = 0)
  179. {
  180. $oProfile = $this->_getAuthorObject($iAuthorId);
  181. return array(
  182. $oProfile->getDisplayName(),
  183. $oProfile->getUrl(),
  184. $oProfile->getThumb(),
  185. $oProfile->getUnit()
  186. );
  187. }
  188. protected function _getAuthorObject($iAuthorId = 0)
  189. {
  190. if($iAuthorId == 0)
  191. return BxDolProfileUndefined::getInstance();
  192. $oProfile = BxDolProfile::getInstance($iAuthorId);
  193. if(!$oProfile)
  194. $oProfile = BxDolProfileUndefined::getInstance();
  195. return $oProfile;
  196. }
  197. /**
  198. * Update Trigger table using data which is automatically gotten from object's internal table.
  199. */
  200. protected function _trigger()
  201. {
  202. if(!$this->_aSystem['trigger_table'])
  203. return false;
  204. $iId = $this->getId();
  205. if(!$iId)
  206. return false;
  207. return $this->_oQuery->updateTriggerTable($iId);
  208. }
  209. /**
  210. * Update (increment/decrement) Trigger table using provided value.
  211. */
  212. protected function _triggerValue($iValue)
  213. {
  214. if(!$this->_aSystem['trigger_table'])
  215. return false;
  216. $iId = $this->getId();
  217. if(!$iId)
  218. return false;
  219. return $this->_oQuery->updateTriggerTableValue($iId, $iValue);
  220. }
  221. /**
  222. * Replace provided markers in a string
  223. * @param $mixed string or array to replace markers in
  224. * @return string where all occured markers are replaced
  225. */
  226. protected function _replaceMarkers ($mixed)
  227. {
  228. return bx_replace_markers($mixed, $this->_aMarkers);
  229. }
  230. }
  231. /** @} */