1
0

BxDolObject.php 7.7 KB

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