BxDolGridConnections.php 5.7 KB

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