BxDolRelation.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 BxDolConnection
  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');
  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. bx_alert($this->_sObject, 'connection_confirmed', 0, bx_get_logged_profile_id(), array(
  105. 'initiator' => (int)$iInitiator,
  106. 'content' => (int)$iContent,
  107. 'mutual' => (int)$iMutual,
  108. 'object' => $this,
  109. ));
  110. return true;
  111. }
  112. /**
  113. * Compound function, which calls getCommonContentExt, getConnectedContentExt or getConnectedInitiatorsExt depending on $sContentType
  114. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  115. * @param $iId1 one content or initiator
  116. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  117. * @param $isMutual get mutual connections only
  118. * @return array of available connections
  119. */
  120. public function getConnectionsAsArrayExt($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  121. {
  122. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  123. return $this->getCommonContentExt($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  124. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  125. $sMethod = 'getConnectedInitiatorsExt';
  126. else
  127. $sMethod = 'getConnectedContentExt';
  128. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  129. }
  130. /**
  131. * Get common content (full info) between two initiators
  132. * @param $iInitiator1 one initiator
  133. * @param $iInitiator2 second initiator
  134. * @param $isMutual get mutual connections only
  135. * @return array of available connections
  136. */
  137. public function getCommonContentExt($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  138. {
  139. return $this->_oQuery->getCommonContentExt($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  140. }
  141. /**
  142. * Get connected initiators (full info)
  143. * @param $iContent content of the connection
  144. * @param $isMutual get mutual connections only
  145. * @return array of available connections
  146. */
  147. public function getConnectedInitiatorsExt($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  148. {
  149. return $this->_oQuery->getConnectedInitiatorsExt($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  150. }
  151. /**
  152. * Get connected content (full info)
  153. * @param $iInitiator initiator of the connection
  154. * @param $isMutual get mutual connections only
  155. * @return array of available connections
  156. */
  157. public function getConnectedContentExt($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  158. {
  159. return $this->_oQuery->getConnectedContentExt($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  160. }
  161. /**
  162. * Check whether connection between Initiator and Content can be established.
  163. */
  164. public function checkAllowedConnect($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  165. {
  166. if(!$this->isRelationAvailable($iInitiator, $iContent))
  167. return _t('_sys_txt_access_denied');
  168. $mixedResult = $this->checkAllowedConnectCustom($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  169. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  170. return $mixedResult;
  171. return parent::checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  172. }
  173. /**
  174. * Custom check action method which can be overwritten.
  175. * Currently only friends can establish relations.
  176. */
  177. public function checkAllowedConnectCustom($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  178. {
  179. if(!BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iInitiator, $iContent, true))
  180. return _t('_sys_txt_access_denied');
  181. return CHECK_ACTION_RESULT_ALLOWED;
  182. }
  183. public function checkAllowedConfirmConnection($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  184. {
  185. return $this->checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, false);
  186. }
  187. public function isRelationAvailableFromProfile($sModule)
  188. {
  189. $sModule .= $this->_sParamDivider;
  190. $aTypes = $this->getRelationTypes();
  191. foreach($aTypes as $sType)
  192. if(substr($sType, 0, strlen($sModule)) == $sModule)
  193. return true;
  194. return false;
  195. }
  196. public function isRelationAvailableWithProfile($sModule)
  197. {
  198. $sModule = $this->_sParamDivider . $sModule;
  199. $aTypes = $this->getRelationTypes();
  200. foreach($aTypes as $sType)
  201. if(substr($sType, -strlen($sModule)) == $sModule)
  202. return true;
  203. return false;
  204. }
  205. public function isRelationAvailableBetweenProfiles($sModuleInitiator, $sModuleContent)
  206. {
  207. $aTypes = $this->getRelationTypes();
  208. if(in_array($sModuleInitiator . $this->_sParamDivider . $sModuleContent, $aTypes))
  209. return true;
  210. return false;
  211. }
  212. public function isRelationAvailable($iInitiator, $iContent)
  213. {
  214. $oInitiator = BxDolProfile::getInstance($iInitiator);
  215. $oContent = BxDolProfile::getInstance($iContent);
  216. if(!$oInitiator || !$oContent)
  217. return false;
  218. return $this->isRelationAvailableBetweenProfiles($oInitiator->getModule(), $oContent->getModule());
  219. }
  220. public function getRelations($iInitiator, $iContent, &$aSuggestions = array())
  221. {
  222. $aRelations = BxDolFormQuery::getDataItems($this->_sPreList, false, BX_DATA_VALUES_ALL);
  223. bx_alert($this->_sObject, 'get_relations', 0, bx_get_logged_profile_id(), array(
  224. 'initiator' => (int)$iInitiator,
  225. 'content' => (int)$iContent,
  226. 'pre_list' => $this->_sPreList,
  227. 'relations' => &$aRelations
  228. ));
  229. if($this->isConnected($iContent, $iInitiator)) {
  230. $iRelation = $this->getRelation($iContent, $iInitiator);
  231. if(!empty($iRelation) && !empty($aRelations[$iRelation]['Data']))
  232. $aSuggestions = unserialize($aRelations[$iRelation]['Data']);
  233. }
  234. return $aRelations;
  235. }
  236. public function getRelation($iInitiator, $iContent)
  237. {
  238. $aConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  239. if(empty($aConnection) || !is_array($aConnection))
  240. return 0;
  241. return (int)$aConnection['relation'];
  242. }
  243. public function getRelationTranslation($iValue, $sUseValues = BX_DATA_VALUES_DEFAULT)
  244. {
  245. $aRelations = BxDolFormQuery::getDataItems($this->_sPreList, false, $sUseValues);
  246. return !empty($aRelations[$iValue]) ? $aRelations[$iValue] : _t('_uknown');
  247. }
  248. public function getRelationTypes()
  249. {
  250. $sParam = getParam(self::$_sParamTypes);
  251. if(empty($sParam))
  252. return array();
  253. return explode(',', $sParam);
  254. }
  255. }