BxDolGridConnections.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  10. class BxDolGridConnections extends BxTemplGrid
  11. {
  12. protected $_bInit = false;
  13. protected $_bOwner = false;
  14. protected $_sObjectConnections = 'sys_profiles_friends';
  15. protected $_oProfile;
  16. protected $_sProfileTrackAction = 'view_friend_requests';
  17. protected $_oConnection;
  18. public function __construct ($aOptions, $oTemplate = false)
  19. {
  20. parent::__construct ($aOptions, $oTemplate);
  21. $this->_sDefaultSortingOrder = 'DESC';
  22. $this->_aQueryAppendExclude[] = 'join_connections';
  23. if(($iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT)) !== false)
  24. $this->setProfile($iProfileId);
  25. }
  26. public function init($bForceInit = false)
  27. {
  28. if($this->_bInit && !$bForceInit)
  29. return $this->_bInit;
  30. if(!$this->_oProfile)
  31. return false;
  32. if($this->_oProfile->id() == bx_get_logged_profile_id())
  33. $this->_bOwner = true;
  34. $this->_oConnection = BxDolConnection::getObjectInstance($this->_sObjectConnections);
  35. if(!$this->_oConnection)
  36. return false;
  37. $aSQLParts = $this->_oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $this->_oProfile->id(), $this->_bOwner ? false : true);
  38. $this->addMarkers(array(
  39. 'profile_id' => $this->_oProfile->id(),
  40. 'join_connections' => $aSQLParts['join']
  41. ));
  42. return true;
  43. }
  44. public function setProfile($iProfileId)
  45. {
  46. $this->_oProfile = BxDolProfile::getInstance((int)$iProfileId);
  47. $this->_bInit = $this->init(true);
  48. }
  49. public function getCode ($isDisplayHeader = true)
  50. {
  51. if(!$this->_bInit)
  52. return '';
  53. BxDolProfileQuery::getInstance()->updateProfileTrack($this->_oProfile->id(), $this->_sProfileTrackAction);
  54. return parent::getCode($isDisplayHeader);
  55. }
  56. /**
  57. * 'accept' action handler
  58. */
  59. public function performActionAccept()
  60. {
  61. list ($iId, $iViewedId) = $this->_prepareIds();
  62. if(!$iId)
  63. return $this->_bIsApi ? [] : echoJson(['msg' => _t('_sys_txt_error_occured')]);
  64. $a = $this->_oConnection->actionAdd($iId, $iViewedId);
  65. if (isset($a['err']) && $a['err'])
  66. $aResult = ['msg' => $a['msg']];
  67. else
  68. $aResult = !$this->_bIsApi ? ['grid' => $this->getCode(false), 'blink' => $iId] : [];
  69. return $this->_bIsApi ? $aResult : echoJson($aResult);
  70. }
  71. /**
  72. * 'add friend' action handler
  73. */
  74. public function performActionAddFriend()
  75. {
  76. return $this->performActionAccept();
  77. }
  78. protected function _delete ($mixedId)
  79. {
  80. list ($iId, $iViewedId) = $this->_prepareIds();
  81. if ($this->_oConnection->isConnected($iViewedId, $iId, true))
  82. $a = $this->_oConnection->actionRemove($iViewedId, $iId);
  83. else
  84. $a = $this->_oConnection->actionReject($iId, $iViewedId);
  85. if (isset($a['err']) && $a['err'])
  86. return false;
  87. return true;
  88. }
  89. protected function _getCellName ($mixedValue, $sKey, $aField, $aRow)
  90. {
  91. if($this->_bIsApi)
  92. return ['type' => 'profile', 'data' => BxDolProfile::getData($aRow['id'])];
  93. $oProfile = BxDolProfile::getInstance($aRow['id']);
  94. if (!$oProfile)
  95. return _t('_sys_txt_error_occured');
  96. $sBadges = bx_srv($oProfile->getModule(), 'get_badges', array($oProfile->getContentId()));
  97. return parent::_getCellDefault($oProfile->getUnit(0, array('template' => array('vars' => array('addon' => $sBadges)))), $sKey, $aField, $aRow);
  98. }
  99. protected function _getCellInfo ($mixedValue, $sKey, $aField, $aRow)
  100. {
  101. $s = '';
  102. // for friend requests display mutual friends
  103. if($this->_bOwner && !$aRow['mutual']) {
  104. $iLogged = bx_get_logged_profile_id();
  105. $iConnections = $this->_oConnection->getCommonContentCount($aRow['id'], $iLogged, true);
  106. if($iConnections == 1) {
  107. $aConnections = $this->_oConnection->getCommonContent($aRow['id'], $iLogged, true);
  108. $oProfile = BxDolProfile::getInstance(array_pop($aConnections));
  109. $s = _t('_sys_txt_one_mutual_friend', $oProfile->getUrl(), $oProfile->getDisplayName());
  110. }
  111. else if($iConnections)
  112. $s = _t('_sys_txt_n_mutual_friends', $iConnections);
  113. }
  114. // display friends number if no other info is available
  115. if(!$s)
  116. $s = _t('_sys_txt_n_friends', $this->_oConnection->getConnectedContentCount($aRow['id'], true));
  117. return parent::_getCellDefault ($s, $sKey, $aField, $aRow);
  118. }
  119. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  120. {
  121. if (!$this->_bOwner)
  122. return $this->_bIsApi ? [] : '';
  123. return parent::_getActionDelete($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  124. }
  125. protected function _getActionAccept ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  126. {
  127. if (!$this->_bOwner || $aRow['mutual'])
  128. return $this->_bIsApi ? [] : '';
  129. if($this->_bIsApi)
  130. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
  131. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  132. }
  133. protected function _getActionAddFriend ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  134. {
  135. if (!isLogged() || $this->_bOwner || $aRow['id'] == bx_get_logged_profile_id())
  136. return $this->_bIsApi ? [] : '';
  137. if ($this->_oConnection->isConnected($aRow['id'], bx_get_logged_profile_id()) || $this->_oConnection->isConnected(bx_get_logged_profile_id(), $aRow['id']))
  138. return $this->_bIsApi ? [] : '';
  139. if($this->_bIsApi)
  140. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
  141. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  142. }
  143. protected function _prepareIds ()
  144. {
  145. $aIds = bx_get('ids');
  146. if($aIds && is_array($aIds))
  147. $mixedId = array_pop($aIds);
  148. return $this->__prepareIds($mixedId);
  149. }
  150. protected function __prepareIds ($mixedId)
  151. {
  152. $iId = 0;
  153. $iViewedId = false;
  154. if(strpos($mixedId, ':') !== false) {
  155. list ($iId, $iViewedId) = explode (':', $mixedId);
  156. $iId = (int)$iId;
  157. $iViewedId = (int)$iViewedId;
  158. }
  159. else
  160. $iId = (int)$mixedId;
  161. return [$iId, $iViewedId];
  162. }
  163. }
  164. /** @} */