BxDolGridConnections.php 5.5 KB

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