BxDolObject.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. define('BX_DOL_OBJECT_ERR_NOT_AVAILABLE', 1);
  10. define('BX_DOL_OBJECT_ERR_ACCESS_DENIED', 2);
  11. define('BX_DOL_OBJECT_ERR_DUPLICATE', 3);
  12. define('BX_DOL_OBJECT_ERR_WRONG_DATE', 4);
  13. define('BX_DOL_OBJECT_ERR_CANNOT_PERFORM', 5);
  14. /**
  15. * Base class for all "Object" classes.
  16. * Child classes usually represents high level programming constructions to generate ready 'objects' functionality, like Comments, Votings, Forms.
  17. */
  18. class BxDolObject extends BxDolFactory implements iBxDolReplaceable
  19. {
  20. protected $_oTemplate = null;
  21. protected $_oQuery = null;
  22. protected $_bIsApi = false;
  23. protected $_iId = 0; ///< item id the action to be performed with
  24. protected $_sSystem = ''; ///< current system name
  25. protected $_aSystem = array(); ///< current system array
  26. protected $_aMarkers = array ();
  27. protected $_sTmplContentElementBlock = '';
  28. protected $_sTmplContentElementInline = '';
  29. protected $_sTmplContentDoAction;
  30. protected $_sTmplContentDoActionLabel = '';
  31. protected $_sTmplContentCounter = '';
  32. protected $_sTmplContentCounterLabel = '';
  33. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  34. {
  35. parent::__construct();
  36. $this->_bIsApi = bx_is_api();
  37. $aSystems = $this->getSystems();
  38. if(!isset($aSystems[$sSystem]))
  39. return;
  40. $this->_sSystem = $sSystem;
  41. $this->_aSystem = $aSystems[$sSystem];
  42. if ($oTemplate)
  43. $this->_oTemplate = $oTemplate;
  44. else
  45. $this->_oTemplate = BxDolTemplate::getInstance();
  46. if(!$this->isEnabled())
  47. return;
  48. if($iInit)
  49. $this->init($iId);
  50. }
  51. public static function &getSystems()
  52. {
  53. $aResult = [];
  54. return $aResult;
  55. }
  56. static public function pruning()
  57. {
  58. $iResults = 0;
  59. $sClass = get_called_class();
  60. $sMethod = 'getSystems';
  61. if(!method_exists($sClass, $sMethod))
  62. return $iResults;
  63. $aSystems = $sClass::$sMethod();
  64. foreach($aSystems as $aSystem) {
  65. if(empty($aSystem['pruning']))
  66. continue;
  67. $oObject = $sClass::getObjectInstance($aSystem['name'], 0, false);
  68. if(!$oObject || !$oObject->isEnabled())
  69. continue;
  70. $iResults += $oObject->_oQuery->pruningByDate($aSystem['pruning']);
  71. }
  72. return $iResults;
  73. }
  74. public function init($iId)
  75. {
  76. if(!$this->isEnabled())
  77. return false;
  78. if(empty($this->_iId) && $iId)
  79. $this->setId($iId);
  80. $this->addMarkers(array(
  81. 'object_id' => $this->getId(),
  82. 'user_id' => $this->_getAuthorId()
  83. ));
  84. return true;
  85. }
  86. public function getSystemId()
  87. {
  88. return $this->_aSystem['id'];
  89. }
  90. public function getSystemName()
  91. {
  92. return $this->_sSystem;
  93. }
  94. public function getSystemInfo()
  95. {
  96. return $this->_aSystem;
  97. }
  98. public function getId()
  99. {
  100. return $this->_iId;
  101. }
  102. public function setId($iId)
  103. {
  104. if($iId == $this->getId())
  105. return;
  106. $this->_iId = $iId;
  107. }
  108. public function isEnabled ()
  109. {
  110. return $this->_aSystem && (int)$this->_aSystem['is_on'] == 1;
  111. }
  112. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  113. {
  114. return $this->_oQuery->isPerformed($iObjectId, $iAuthorId);
  115. }
  116. public function getVote($iObjectId = 0, $bForceGet = false)
  117. {
  118. return $this->_getVote($iObjectId, $bForceGet);
  119. }
  120. public function getTrack($iObjectId, $iAuthorId)
  121. {
  122. return $this->_getTrack($iObjectId, $iAuthorId);
  123. }
  124. /**
  125. * Interface functions for outer usage
  126. */
  127. public function getConditions($sMainTable, $sMainField)
  128. {
  129. if(!$this->isEnabled())
  130. return array();
  131. $sTable = isset($this->_aSystem['table_main']) ? $this->_aSystem['table_main'] : '';
  132. if(empty($sTable) || empty($sMainTable) || empty($sMainField))
  133. return array();
  134. return array(
  135. 'join' => array (
  136. 'objects_' . $this->_sSystem => array(
  137. 'type' => 'INNER',
  138. 'table' => $sTable,
  139. 'mainTable' => $sMainTable,
  140. 'mainField' => $sMainField,
  141. 'onField' => 'object_id',
  142. 'joinFields' => array('count'),
  143. ),
  144. ),
  145. );
  146. }
  147. public function getConditionsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  148. {
  149. if(!$this->isEnabled())
  150. return array();
  151. $sTableTrack = isset($this->_aSystem['table_track']) ? $this->_aSystem['table_track'] : '';
  152. if(empty($sTableTrack) || empty($sMainTable) || empty($sMainField))
  153. return array();
  154. return array(
  155. 'restriction' => array (
  156. 'objects_' . $this->_sSystem . '_author' => array(
  157. 'value' => $iAuthorId,
  158. 'field' => 'author_id',
  159. 'operator' => '=',
  160. 'table' => $sTableTrack,
  161. ),
  162. ),
  163. 'join' => array (
  164. 'objects_' . $this->_sSystem => array(
  165. 'type' => 'INNER',
  166. 'table' => $sTableTrack,
  167. 'mainTable' => $sMainTable,
  168. 'mainField' => $sMainField,
  169. 'onField' => 'object_id',
  170. 'joinFields' => array('author_id'),
  171. ),
  172. ),
  173. );
  174. }
  175. public function getSqlParts($sMainTable, $sMainField)
  176. {
  177. if(!$this->isEnabled())
  178. return array();
  179. return $this->_oQuery->getSqlParts($sMainTable, $sMainField);
  180. }
  181. public function getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId = 0)
  182. {
  183. if(!$this->isEnabled())
  184. return array();
  185. return $this->_oQuery->getSqlPartsTrack($sMainTable, $sMainField, $iAuthorId);
  186. }
  187. public function getSqlPartsTrackAuthor($sMainTable, $sMainField, $iObjectId = 0)
  188. {
  189. if(!$this->isEnabled())
  190. return array();
  191. return $this->_oQuery->getSqlPartsTrackAuthor($sMainTable, $sMainField, $iObjectId);
  192. }
  193. public function addMarkers($aMarkers)
  194. {
  195. if(empty($aMarkers) || !is_array($aMarkers))
  196. return false;
  197. $this->_aMarkers = array_merge($this->_aMarkers, $aMarkers);
  198. return true;
  199. }
  200. public function getElementAPI($aParams = [])
  201. {
  202. if(!$this->_bIsApi)
  203. return;
  204. //TODO: Implement for Objects like Views, Votes, etc.
  205. $sClass = get_class($this);
  206. return [
  207. 'display_type' => 'element',
  208. 'name' => md5($sClass),
  209. 'title' => get_class($this)
  210. ];
  211. }
  212. public function getCounterAPI($aParams = [])
  213. {
  214. if(!$this->_bIsApi)
  215. return;
  216. //TODO: Implement for Objects like Views, Votes, etc.
  217. $sClass = get_class($this);
  218. return [
  219. 'display_type' => 'element',
  220. 'name' => md5($sClass),
  221. 'title' => get_class($this)
  222. ];
  223. }
  224. /**
  225. * Database functions
  226. */
  227. public function getQueryObject ()
  228. {
  229. return $this->_oQuery;
  230. }
  231. /**
  232. * Permissions functions
  233. */
  234. public function checkAction ($sAction, $isPerformAction = false)
  235. {
  236. $iId = $this->_getAuthorId();
  237. $a = checkActionModule($iId, $sAction, 'system', $isPerformAction);
  238. return $a[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  239. }
  240. public function checkActionErrorMsg ($sAction)
  241. {
  242. $iId = $this->_getAuthorId();
  243. $a = checkActionModule($iId, $sAction, 'system');
  244. return $a[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $a[CHECK_ACTION_MESSAGE] : '';
  245. }
  246. /**
  247. * Actions' response functions
  248. */
  249. public function onObjectDelete($iObjectId = 0)
  250. {
  251. $this->_oQuery->deleteObjectEntries($iObjectId ? $iObjectId : $this->getId());
  252. }
  253. /**
  254. * Internal functions
  255. */
  256. protected function _getAuthorId ()
  257. {
  258. return isMember() ? bx_get_logged_profile_id() : 0;
  259. }
  260. protected function _getAuthorPassword ()
  261. {
  262. return getLoggedPassword();
  263. }
  264. protected function _getAuthorIp ()
  265. {
  266. return getVisitorIP();
  267. }
  268. protected function _getAuthorInfo($iAuthorId = 0)
  269. {
  270. $oProfile = $this->_getAuthorObject($iAuthorId);
  271. return array(
  272. $oProfile->getDisplayName(),
  273. $oProfile->getUrl(),
  274. $oProfile->getThumb(),
  275. $oProfile->getUnit(),
  276. $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  277. );
  278. }
  279. protected function _getAuthorObject($iAuthorId = 0)
  280. {
  281. if($iAuthorId == 0)
  282. return BxDolProfileUndefined::getInstance();
  283. $oProfile = BxDolProfile::getInstance($iAuthorId);
  284. if(!$oProfile)
  285. $oProfile = BxDolProfileUndefined::getInstance();
  286. return $oProfile;
  287. }
  288. /**
  289. * Update Trigger table using data which is automatically gotten from object's internal table.
  290. */
  291. protected function _trigger()
  292. {
  293. if(!$this->_aSystem['trigger_table'])
  294. return false;
  295. $iId = $this->getId();
  296. if(!$iId)
  297. return false;
  298. return $this->_oQuery->updateTriggerTable($iId);
  299. }
  300. /**
  301. * Update (increment/decrement) Trigger table using provided value.
  302. */
  303. protected function _triggerValue($iValue)
  304. {
  305. if(!$this->_aSystem['trigger_table'])
  306. return false;
  307. $iId = $this->getId();
  308. if(!$iId)
  309. return false;
  310. return $this->_oQuery->updateTriggerTableValue($iId, $iValue);
  311. }
  312. /**
  313. * Replace provided markers in a string
  314. * @param $mixed string or array to replace markers in
  315. * @return string where all occured markers are replaced
  316. */
  317. protected function _replaceMarkers ($mixed)
  318. {
  319. return bx_replace_markers($mixed, $this->_aMarkers);
  320. }
  321. protected function _prepareParamsData($aParams)
  322. {
  323. $aParams = array_merge([
  324. 'sSystem' => $this->getSystemName(),
  325. 'iObjId' => $this->getId(),
  326. 'iAuthorId' => $this->_getAuthorId(),
  327. 'sRootUrl' => BX_DOL_URL_ROOT,
  328. ], $aParams);
  329. foreach($aParams as $sKey => $mixedValue)
  330. if(is_bool($mixedValue))
  331. $aParams[$sKey] = (int)$mixedValue;
  332. return $aParams;
  333. }
  334. protected function _getRequestParamsData($aKeys = array())
  335. {
  336. $sParams = bx_get('params');
  337. if($sParams === false)
  338. return [];
  339. $aParams = [];
  340. parse_str(bx_process_input($sParams), $aParams);
  341. return $aParams;
  342. }
  343. protected function _prepareRequestParamsData($aParams, $aParamsAdd = array())
  344. {
  345. $aRequestParams = array_intersect_key($aParams, $this->_aElementDefaults);
  346. $aRequestParams = array_merge($aRequestParams, $aParamsAdd);
  347. foreach($aRequestParams as $sKey => $mixedValue)
  348. if(is_bool($mixedValue))
  349. $aRequestParams[$sKey] = (int)$mixedValue;
  350. return $aRequestParams;
  351. }
  352. protected function _getTmplContentElementBlock()
  353. {
  354. return $this->_sTmplContentElementBlock;
  355. }
  356. protected function _getTmplContentElementInline()
  357. {
  358. return $this->_sTmplContentElementInline;
  359. }
  360. protected function _getTmplContentDoAction()
  361. {
  362. return $this->_sTmplContentDoAction;
  363. }
  364. protected function _getTmplContentDoActionLabel()
  365. {
  366. return $this->_sTmplContentDoActionLabel;
  367. }
  368. protected function _getTmplContentCounter()
  369. {
  370. return $this->_sTmplContentCounter;
  371. }
  372. protected function _getTmplContentCounterLabel()
  373. {
  374. return $this->_sTmplContentCounterLabel;
  375. }
  376. }
  377. /** @} */