BxDolConnectionRelation.php 14 KB

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