BxDolRelation.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 $_sParamDivider;
  12. protected $_sParamEnabled;
  13. protected $_sPreList;
  14. protected function __construct($aObject)
  15. {
  16. parent::__construct($aObject);
  17. $this->_oQuery = new BxDolRelationQuery($aObject);
  18. $this->_sParamDivider = '_';
  19. $this->_sParamEnabled = 'sys_relations';
  20. $this->_sPreList = 'sys_relations';
  21. }
  22. /**
  23. * Add new relation.
  24. * @param $mixedContent content to make relation with or an array with content and relation type
  25. * @return array
  26. */
  27. public function actionAdd($mixedContent = 0, $iInitiator = false)
  28. {
  29. if(empty($mixedContent))
  30. $mixedContent = bx_process_input($_POST['id'], BX_DATA_INT);
  31. $iContent = 0;
  32. $iRelation = 0;
  33. if(is_array($mixedContent)) {
  34. $iContent = (int)$mixedContent['content'];
  35. $iRelation = (int)$mixedContent['relation'];
  36. }
  37. else
  38. $iContent = (int)$mixedContent;
  39. $iInitiator = $iInitiator ? (int)$iInitiator : (int)bx_get_logged_profile_id();
  40. $aResult = parent::actionAdd($iContent, $iInitiator);
  41. if(empty($iRelation) || (isset($aResult['err']) && $aResult['err'] !== false))
  42. return $aResult;
  43. $this->_oQuery->updateConnection($iInitiator, $iContent, array(
  44. 'relation' => $iRelation
  45. ));
  46. return $aResult;
  47. }
  48. /**
  49. * Confirm relation request without creation of retroactive relation.
  50. * @param $iContent content to make relation with
  51. * @return array
  52. */
  53. public function actionConfirm($iContent = 0, $iInitiator = false)
  54. {
  55. if(!$iContent)
  56. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  57. return $this->_action($iContent, $iInitiator ? $iInitiator : bx_get_logged_profile_id(), 'confirmConnection', '_sys_conn_err_connection_does_not_exists');
  58. }
  59. /**
  60. * 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.
  61. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  62. * @return array
  63. */
  64. public function actionRemove($iContent = 0, $iInitiator = false)
  65. {
  66. if(!$iContent)
  67. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  68. return $this->_action($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  69. }
  70. /**
  71. * Confirm relation request without creation of retroactive relation.
  72. * @param $iInitiator initiator of the connection, in most cases some profile id
  73. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  74. * @return true - if connection was added, false - if connection already exists or error occured
  75. */
  76. public function confirmConnection($iInitiator, $iContent)
  77. {
  78. $iMutual = 1;
  79. if(!$this->_oQuery->updateConnectionMutual((int)$iInitiator, (int)$iContent, $iMutual))
  80. return false;
  81. bx_alert($this->_sObject, 'connection_confirmed', 0, bx_get_logged_profile_id(), array(
  82. 'initiator' => (int)$iInitiator,
  83. 'content' => (int)$iContent,
  84. 'mutual' => (int)$iMutual,
  85. 'object' => $this,
  86. ));
  87. return true;
  88. }
  89. /**
  90. * Compound function, which calls getCommonContentExt, getConnectedContentExt or getConnectedInitiatorsExt depending on $sContentType
  91. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  92. * @param $iId1 one content or initiator
  93. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  94. * @param $isMutual get mutual connections only
  95. * @return array of available connections
  96. */
  97. public function getConnectionsAsArrayExt($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  98. {
  99. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  100. return $this->getCommonContentExt($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  101. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  102. $sMethod = 'getConnectedInitiatorsExt';
  103. else
  104. $sMethod = 'getConnectedContentExt';
  105. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  106. }
  107. /**
  108. * Get common content (full info) between two initiators
  109. * @param $iInitiator1 one initiator
  110. * @param $iInitiator2 second initiator
  111. * @param $isMutual get mutual connections only
  112. * @return array of available connections
  113. */
  114. public function getCommonContentExt($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  115. {
  116. return $this->_oQuery->getCommonContentExt($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  117. }
  118. /**
  119. * Get connected initiators (full info)
  120. * @param $iContent content of the connection
  121. * @param $isMutual get mutual connections only
  122. * @return array of available connections
  123. */
  124. public function getConnectedInitiatorsExt($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  125. {
  126. return $this->_oQuery->getConnectedInitiatorsExt($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  127. }
  128. /**
  129. * Get connected content (full info)
  130. * @param $iInitiator initiator of the connection
  131. * @param $isMutual get mutual connections only
  132. * @return array of available connections
  133. */
  134. public function getConnectedContentExt($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  135. {
  136. return $this->_oQuery->getConnectedContentExt($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  137. }
  138. /**
  139. * Check whether connection between Initiator and Content can be established.
  140. */
  141. public function checkAllowedConnect($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false)
  142. {
  143. if(!$this->isRelationAvailable($iInitiator, $iContent))
  144. return _t('_sys_txt_access_denied');
  145. $mixedResult = $this->checkAllowedConnectCustom($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap);
  146. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  147. return $mixedResult;
  148. return parent::checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap);
  149. }
  150. /**
  151. * Custom check action method which can be overwritten.
  152. * Currently only friends can establish relations.
  153. */
  154. public function checkAllowedConnectCustom($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false)
  155. {
  156. if(!BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iInitiator, $iContent, true))
  157. return _t('_sys_txt_access_denied');
  158. return CHECK_ACTION_RESULT_ALLOWED;
  159. }
  160. public function isRelationAvailableFromProfile($sModule)
  161. {
  162. $sModule .= $this->_sParamDivider;
  163. $aTypes = $this->getRelationTypes();
  164. foreach($aTypes as $sType)
  165. if(substr($sType, 0, strlen($sModule)) == $sModule)
  166. return true;
  167. return false;
  168. }
  169. public function isRelationAvailableWithProfile($sModule)
  170. {
  171. $sModule = $this->_sParamDivider . $sModule;
  172. $aTypes = $this->getRelationTypes();
  173. foreach($aTypes as $sType)
  174. if(substr($sType, -strlen($sModule)) == $sModule)
  175. return true;
  176. return false;
  177. }
  178. public function isRelationAvailableBetweenProfiles($sModuleInitiator, $sModuleContent)
  179. {
  180. $aTypes = $this->getRelationTypes();
  181. if(in_array($sModuleInitiator . $this->_sParamDivider . $sModuleContent, $aTypes))
  182. return true;
  183. return false;
  184. }
  185. public function isRelationAvailable($iInitiator, $iContent)
  186. {
  187. $oInitiator = BxDolProfile::getInstance($iInitiator);
  188. $oContent = BxDolProfile::getInstance($iContent);
  189. if(!$oInitiator || !$oContent)
  190. return false;
  191. return $this->isRelationAvailableBetweenProfiles($oInitiator->getModule(), $oContent->getModule());
  192. }
  193. public function getRelations($iInitiator, $iContent, &$aSuggestions = array())
  194. {
  195. $aRelations = BxDolFormQuery::getDataItems($this->_sPreList, false, BX_DATA_VALUES_ALL);
  196. bx_alert($this->_sObject, 'get_relations', 0, bx_get_logged_profile_id(), array(
  197. 'initiator' => (int)$iInitiator,
  198. 'content' => (int)$iContent,
  199. 'pre_list' => $this->_sPreList,
  200. 'relations' => &$aRelations
  201. ));
  202. if($this->isConnected($iContent, $iInitiator)) {
  203. $iRelation = $this->getRelation($iContent, $iInitiator);
  204. if(!empty($iRelation) && !empty($aRelations[$iRelation]['Data']))
  205. $aSuggestions = unserialize($aRelations[$iRelation]['Data']);
  206. }
  207. return $aRelations;
  208. }
  209. public function getRelation($iInitiator, $iContent)
  210. {
  211. $aConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  212. if(empty($aConnection) || !is_array($aConnection))
  213. return 0;
  214. return (int)$aConnection['relation'];
  215. }
  216. public function getRelationTranslation($iValue, $sUseValues = BX_DATA_VALUES_DEFAULT)
  217. {
  218. $aRelations = BxDolFormQuery::getDataItems($this->_sPreList, false, $sUseValues);
  219. return !empty($aRelations[$iValue]) ? $aRelations[$iValue] : _t('_uknown');
  220. }
  221. public function getRelationTypes()
  222. {
  223. $sParam = getParam($this->_sParamEnabled);
  224. if(empty($sParam))
  225. return array();
  226. return explode(',', $sParam);
  227. }
  228. }