BxDolGridConnections.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $a = $this->_oConnection->getCommonContent($aRow['id'], bx_get_logged_profile_id(), true);
  105. $i = count($a);
  106. if (1 == $i) {
  107. $iProfileId = array_pop($a);
  108. $oProfile = BxDolProfile::getInstance($iProfileId);
  109. $s = _t('_sys_txt_one_mutual_friend', $oProfile->getUrl(), $oProfile->getDisplayName());
  110. } elseif ($i) {
  111. $s = _t('_sys_txt_n_mutual_friends', $i);
  112. }
  113. }
  114. // display friends number if no other info is available
  115. if (!$s) {
  116. $a = $this->_oConnection->getConnectedContent($aRow['id'], true);
  117. $i = count($a);
  118. $s = _t('_sys_txt_n_friends', $i);
  119. }
  120. return parent::_getCellDefault ($s, $sKey, $aField, $aRow);
  121. }
  122. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  123. {
  124. if (!$this->_bOwner)
  125. return $this->_bIsApi ? [] : '';
  126. return parent::_getActionDelete($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  127. }
  128. protected function _getActionAccept ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  129. {
  130. if (!$this->_bOwner || $aRow['mutual'])
  131. return $this->_bIsApi ? [] : '';
  132. if($this->_bIsApi)
  133. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
  134. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  135. }
  136. protected function _getActionAddFriend ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  137. {
  138. if (!isLogged() || $this->_bOwner || $aRow['id'] == bx_get_logged_profile_id())
  139. return $this->_bIsApi ? [] : '';
  140. if ($this->_oConnection->isConnected($aRow['id'], bx_get_logged_profile_id()) || $this->_oConnection->isConnected(bx_get_logged_profile_id(), $aRow['id']))
  141. return $this->_bIsApi ? [] : '';
  142. if($this->_bIsApi)
  143. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
  144. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  145. }
  146. protected function _prepareIds ()
  147. {
  148. $aIds = bx_get('ids');
  149. if($aIds && is_array($aIds))
  150. $mixedId = array_pop($aIds);
  151. return $this->__prepareIds($mixedId);
  152. }
  153. protected function __prepareIds ($mixedId)
  154. {
  155. $iId = 0;
  156. $iViewedId = false;
  157. if(strpos($mixedId, ':') !== false) {
  158. list ($iId, $iViewedId) = explode (':', $mixedId);
  159. $iId = (int)$iId;
  160. $iViewedId = (int)$iViewedId;
  161. }
  162. else
  163. $iId = (int)$mixedId;
  164. return [$iId, $iViewedId];
  165. }
  166. }
  167. /** @} */