BxDolRelation.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php defined('BX_DOL') 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. class BxDolRelation extends BxTemplConnection
  10. {
  11. protected static $_sParamEnabled = 'sys_relations_enable';
  12. protected static $_sParamTypes = 'sys_relations';
  13. protected $_sParamDivider;
  14. protected $_sPreList;
  15. protected function __construct($aObject)
  16. {
  17. parent::__construct($aObject);
  18. $this->_oQuery = new BxDolRelationQuery($aObject);
  19. $this->_sParamDivider = '_';
  20. $this->_sPreList = 'sys_relations';
  21. }
  22. /**
  23. * Whether the Relations feature is enabled globaly or not.
  24. * @return boolean
  25. */
  26. public static function isEnabled()
  27. {
  28. return getParam(self::$_sParamEnabled) == 'on';
  29. }
  30. /**
  31. * Add new relation.
  32. * @param $mixedContent content to make relation with or an array with content and relation type
  33. * @return array
  34. */
  35. public function actionAdd($mixedContent = 0, $iInitiator = false)
  36. {
  37. if(empty($mixedContent))
  38. $mixedContent = bx_process_input($_POST['id'], BX_DATA_INT);
  39. $iContent = 0;
  40. $iRelation = 0;
  41. if(is_array($mixedContent)) {
  42. $iContent = (int)$mixedContent['content'];
  43. $iRelation = (int)$mixedContent['relation'];
  44. }
  45. else
  46. $iContent = (int)$mixedContent;
  47. $iInitiator = $iInitiator ? (int)$iInitiator : (int)bx_get_logged_profile_id();
  48. $aResult = parent::actionAdd($iContent, $iInitiator);
  49. if(empty($iRelation) || (isset($aResult['err']) && $aResult['err'] !== false))
  50. return $aResult;
  51. $this->_oQuery->updateConnection($iInitiator, $iContent, array(
  52. 'relation' => $iRelation
  53. ));
  54. return $aResult;
  55. }
  56. /**
  57. * Confirm relation request without creation of retroactive relation.
  58. * @param $iContent content to make relation with
  59. * @return array
  60. */
  61. public function actionConfirm($iContent = 0, $iInitiator = false)
  62. {
  63. if(!$iContent)
  64. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  65. return $this->_action($iContent, $iInitiator ? $iInitiator : bx_get_logged_profile_id(), 'confirmConnection', '_sys_conn_err_connection_does_not_exists');
  66. }
  67. /**
  68. * Remove relation without removing a retroactive relation. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  69. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  70. * @return array
  71. */
  72. public function actionRemove($iContent = 0, $iInitiator = false)
  73. {
  74. if(!$iContent)
  75. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  76. return $this->_action($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists', false, true);
  77. }
  78. /**
  79. * Add new connection.
  80. * @param $iInitiator initiator of the connection, in most cases some profile id
  81. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  82. * @return true - if connection was added, false - if connection already exists or error occured
  83. */
  84. public function addConnection($iInitiator, $iContent, $aParams = [])
  85. {
  86. $bResult = parent::addConnection($iInitiator, $iContent, $aParams);
  87. if($bResult && !empty($aParams['relation']))
  88. $this->_oQuery->updateConnection($iInitiator, $iContent, [
  89. 'relation' => (int)$aParams['relation']
  90. ]);
  91. return $bResult;
  92. }
  93. /**
  94. * Confirm relation request without creation of retroactive relation.
  95. * @param $iInitiator initiator of the connection, in most cases some profile id
  96. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  97. * @return true - if connection was added, false - if connection already exists or error occured
  98. */
  99. public function confirmConnection($iInitiator, $iContent)
  100. {
  101. $iMutual = 1;
  102. if(!$this->_oQuery->updateConnectionMutual((int)$iInitiator, (int)$iContent, $iMutual))
  103. return false;
  104. /**
  105. * @hooks
  106. * @hookdef hook-bx_dol_relation-connection_confirmed '{object_name}', 'connection_confirmed' - hook after relation was confirmed
  107. * - $unit_name - relation object name
  108. * - $action - equals `connection_confirmed`
  109. * - $object_id - not used
  110. * - $sender_id - logged in profile id
  111. * - $extra_params - array of additional params with the following array keys:
  112. * - `initiator` - [int] profile id who created the connection
  113. * - `content` - [int] profile id with whom the connection was created
  114. * - `mutual` - [int] if the relation is mutual or not
  115. * - `object` - [object] an instance of relation, @see BxDolRelation
  116. * @hook @ref hook-bx_dol_relation-connection_confirmed
  117. */
  118. bx_alert($this->_sObject, 'connection_confirmed', 0, bx_get_logged_profile_id(), [
  119. 'initiator' => (int)$iInitiator,
  120. 'content' => (int)$iContent,
  121. 'mutual' => (int)$iMutual,
  122. 'object' => $this,
  123. ]);
  124. return true;
  125. }
  126. /**
  127. * Compound function, which calls getCommonContentExt, getConnectedContentExt or getConnectedInitiatorsExt depending on $sContentType
  128. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  129. * @param $iId1 one content or initiator
  130. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  131. * @param $isMutual get mutual connections only
  132. * @return array of available connections
  133. */
  134. public function getConnectionsAsArrayExt($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  135. {
  136. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  137. return $this->getCommonContentExt($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  138. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  139. $sMethod = 'getConnectedInitiatorsExt';
  140. else
  141. $sMethod = 'getConnectedContentExt';
  142. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  143. }
  144. /**
  145. * Get common content (full info) between two initiators
  146. * @param $iInitiator1 one initiator
  147. * @param $iInitiator2 second initiator
  148. * @param $isMutual get mutual connections only
  149. * @return array of available connections
  150. */
  151. public function getCommonContentExt($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  152. {
  153. return $this->_oQuery->getCommonContentExt($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  154. }
  155. /**
  156. * Get connected initiators (full info)
  157. * @param $iContent content of the connection
  158. * @param $isMutual get mutual connections only
  159. * @return array of available connections
  160. */
  161. public function getConnectedInitiatorsExt($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  162. {
  163. return $this->_oQuery->getConnectedInitiatorsExt($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  164. }
  165. /**
  166. * Get connected content (full info)
  167. * @param $iInitiator initiator of the connection
  168. * @param $isMutual get mutual connections only
  169. * @return array of available connections
  170. */
  171. public function getConnectedContentExt($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  172. {
  173. return $this->_oQuery->getConnectedContentExt($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  174. }
  175. /**
  176. * Check whether connection between Initiator and Content can be established.
  177. */
  178. public function checkAllowedConnect($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  179. {
  180. if(!$this->isRelationAvailable($iInitiator, $iContent))
  181. return _t('_sys_txt_access_denied');
  182. $mixedResult = $this->checkAllowedConnectCustom($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  183. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  184. return $mixedResult;
  185. return parent::checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  186. }
  187. /**
  188. * Custom check action method which can be overwritten.
  189. * Currently only friends can establish relations.
  190. */
  191. public function checkAllowedConnectCustom($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  192. {
  193. if(!BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iInitiator, $iContent, true))
  194. return _t('_sys_txt_access_denied');
  195. return CHECK_ACTION_RESULT_ALLOWED;
  196. }
  197. public function checkAllowedConfirmConnection($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  198. {
  199. return $this->checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, false);
  200. }
  201. public function isRelationAvailableFromProfile($sModule)
  202. {
  203. $sModule .= $this->_sParamDivider;
  204. $aTypes = $this->getRelationTypes();
  205. foreach($aTypes as $sType)
  206. if(substr($sType, 0, strlen($sModule)) == $sModule)
  207. return true;
  208. return false;
  209. }
  210. public function isRelationAvailableWithProfile($sModule)
  211. {
  212. $sModule = $this->_sParamDivider . $sModule;
  213. $aTypes = $this->getRelationTypes();
  214. foreach($aTypes as $sType)
  215. if(substr($sType, -strlen($sModule)) == $sModule)
  216. return true;
  217. return false;
  218. }
  219. public function isRelationAvailableBetweenProfiles($sModuleInitiator, $sModuleContent)
  220. {
  221. $aTypes = $this->getRelationTypes();
  222. if(in_array($sModuleInitiator . $this->_sParamDivider . $sModuleContent, $aTypes))
  223. return true;
  224. return false;
  225. }
  226. public function isRelationAvailable($iInitiator, $iContent)
  227. {
  228. $oInitiator = BxDolProfile::getInstance($iInitiator);
  229. $oContent = BxDolProfile::getInstance($iContent);
  230. if(!$oInitiator || !$oContent)
  231. return false;
  232. return $this->isRelationAvailableBetweenProfiles($oInitiator->getModule(), $oContent->getModule());
  233. }
  234. public function getRelations($iInitiator, $iContent, &$aSuggestions = array())
  235. {
  236. $aRelations = BxDolFormQuery::getDataItems($this->_sPreList, false, BX_DATA_VALUES_ALL);
  237. /**
  238. * @hooks
  239. * @hookdef hook-bx_dol_relation-get_relations '{object_name}', 'get_relations' - hook to override relation info
  240. * - $unit_name - relation object name
  241. * - $action - equals `get_relations`
  242. * - $object_id - not used
  243. * - $sender_id - logged in profile id
  244. * - $extra_params - array of additional params with the following array keys:
  245. * - `initiator` - [int] profile id who created the connection
  246. * - `content` - [int] profile id with whom the connection was created
  247. * - `pre_list` - [string] predefined list name with all available relation types
  248. * - `relations` - [array] by ref, array with relations between 'initiator' and 'content' profiles, can be overridden in hook processing
  249. * @hook @ref hook-bx_dol_relation-get_relations
  250. */
  251. bx_alert($this->_sObject, 'get_relations', 0, bx_get_logged_profile_id(), [
  252. 'initiator' => (int)$iInitiator,
  253. 'content' => (int)$iContent,
  254. 'pre_list' => $this->_sPreList,
  255. 'relations' => &$aRelations
  256. ]);
  257. if($this->isConnected($iContent, $iInitiator)) {
  258. $iRelation = $this->getRelation($iContent, $iInitiator);
  259. if(!empty($iRelation) && !empty($aRelations[$iRelation]['Data']))
  260. $aSuggestions = unserialize($aRelations[$iRelation]['Data']);
  261. }
  262. return $aRelations;
  263. }
  264. public function getRelation($iInitiator, $iContent)
  265. {
  266. $aConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  267. if(empty($aConnection) || !is_array($aConnection))
  268. return 0;
  269. return (int)$aConnection['relation'];
  270. }
  271. public function getRelationTranslation($iValue, $sUseValues = BX_DATA_VALUES_DEFAULT)
  272. {
  273. $aRelations = BxDolFormQuery::getDataItems($this->_sPreList, false, $sUseValues);
  274. return !empty($aRelations[$iValue]) ? $aRelations[$iValue] : _t('_uknown');
  275. }
  276. public function getRelationTypes()
  277. {
  278. $sParam = getParam(self::$_sParamTypes);
  279. if(empty($sParam))
  280. return array();
  281. return explode(',', $sParam);
  282. }
  283. }