BxDolObject.php 11 KB

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