BxDolRelation.php 9.3 KB

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