BxDolObject.php 8.7 KB

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