BxDolObject.php 12 KB

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