BxDolObject.php 11 KB

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