BxDolObject.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 getSqlPartsTrackAuthor($sMainTable, $sMainField, $iObjectId = 0)
  150. {
  151. if(!$this->isEnabled())
  152. return array();
  153. return $this->_oQuery->getSqlPartsTrackAuthor($sMainTable, $sMainField, $iObjectId);
  154. }
  155. public function addMarkers($aMarkers)
  156. {
  157. if(empty($aMarkers) || !is_array($aMarkers))
  158. return false;
  159. $this->_aMarkers = array_merge($this->_aMarkers, $aMarkers);
  160. return true;
  161. }
  162. /**
  163. * Database functions
  164. */
  165. public function getQueryObject ()
  166. {
  167. return $this->_oQuery;
  168. }
  169. /**
  170. * Permissions functions
  171. */
  172. public function checkAction ($sAction, $isPerformAction = false)
  173. {
  174. $iId = $this->_getAuthorId();
  175. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  176. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  177. }
  178. public function checkActionErrorMsg ($sAction)
  179. {
  180. $iId = $this->_getAuthorId();
  181. $a = checkActionModule($iId, $sAction, 'system');
  182. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  183. }
  184. /**
  185. * Actions' response functions
  186. */
  187. public function onObjectDelete($iObjectId = 0)
  188. {
  189. $this->_oQuery->deleteObjectEntries($iObjectId ? $iObjectId : $this->getId());
  190. }
  191. /**
  192. * Internal functions
  193. */
  194. protected function _getAuthorId ()
  195. {
  196. return isMember() ? bx_get_logged_profile_id() : 0;
  197. }
  198. protected function _getAuthorPassword ()
  199. {
  200. return isMember() ? $_COOKIE['memberPassword'] : "";
  201. }
  202. protected function _getAuthorIp ()
  203. {
  204. return getVisitorIP();
  205. }
  206. protected function _getAuthorInfo($iAuthorId = 0)
  207. {
  208. $oProfile = $this->_getAuthorObject($iAuthorId);
  209. return array(
  210. $oProfile->getDisplayName(),
  211. $oProfile->getUrl(),
  212. $oProfile->getThumb(),
  213. $oProfile->getUnit(),
  214. $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  215. );
  216. }
  217. protected function _getAuthorObject($iAuthorId = 0)
  218. {
  219. if($iAuthorId == 0)
  220. return BxDolProfileUndefined::getInstance();
  221. $oProfile = BxDolProfile::getInstance($iAuthorId);
  222. if(!$oProfile)
  223. $oProfile = BxDolProfileUndefined::getInstance();
  224. return $oProfile;
  225. }
  226. /**
  227. * Update Trigger table using data which is automatically gotten from object's internal table.
  228. */
  229. protected function _trigger()
  230. {
  231. if(!$this->_aSystem['trigger_table'])
  232. return false;
  233. $iId = $this->getId();
  234. if(!$iId)
  235. return false;
  236. return $this->_oQuery->updateTriggerTable($iId);
  237. }
  238. /**
  239. * Update (increment/decrement) Trigger table using provided value.
  240. */
  241. protected function _triggerValue($iValue)
  242. {
  243. if(!$this->_aSystem['trigger_table'])
  244. return false;
  245. $iId = $this->getId();
  246. if(!$iId)
  247. return false;
  248. return $this->_oQuery->updateTriggerTableValue($iId, $iValue);
  249. }
  250. /**
  251. * Replace provided markers in a string
  252. * @param $mixed string or array to replace markers in
  253. * @return string where all occured markers are replaced
  254. */
  255. protected function _replaceMarkers ($mixed)
  256. {
  257. return bx_replace_markers($mixed, $this->_aMarkers);
  258. }
  259. protected function _getRequestParamsData($aKeys = array())
  260. {
  261. $sParams = bx_get('params');
  262. if($sParams === false)
  263. return array();
  264. $aParams = array();
  265. parse_str(bx_process_input($sParams), $aParams);
  266. return $aParams;
  267. }
  268. protected function _prepareRequestParamsData($aParams, $aParamsAdd = array())
  269. {
  270. $aRequestParams = array_intersect_key($aParams, $this->_aElementDefaults);
  271. $aRequestParams = array_merge($aRequestParams, $aParamsAdd);
  272. foreach($aRequestParams as $sKey => $mixedValue)
  273. if(is_bool($mixedValue))
  274. $aRequestParams[$sKey] = (int)$mixedValue;
  275. return $aRequestParams;
  276. }
  277. protected function _getTmplContentElementBlock()
  278. {
  279. return $this->_sTmplContentElementBlock;
  280. }
  281. protected function _getTmplContentElementInline()
  282. {
  283. return $this->_sTmplContentElementInline;
  284. }
  285. protected function _getTmplContentDoAction()
  286. {
  287. return $this->_sTmplContentDoAction;
  288. }
  289. protected function _getTmplContentDoActionLabel()
  290. {
  291. return $this->_sTmplContentDoActionLabel;
  292. }
  293. protected function _getTmplContentCounter()
  294. {
  295. return $this->_sTmplContentCounter;
  296. }
  297. protected function _getTmplContentCounterLabel()
  298. {
  299. return $this->_sTmplContentCounterLabel;
  300. }
  301. }
  302. /** @} */