BxDolGridConnections.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Messages Messages
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  12. bx_import('BxTemplGrid');
  13. class BxDolGridConnections extends BxTemplGrid
  14. {
  15. protected $_bOwner = false;
  16. protected $_sContentModule = 'bx_persons';
  17. protected $_sObjectConnections = 'sys_profiles_friends';
  18. public function __construct ($aOptions, $oTemplate = false)
  19. {
  20. parent::__construct ($aOptions, $oTemplate);
  21. $this->_sDefaultSortingOrder = 'DESC';
  22. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  23. if (!$iProfileId)
  24. return;
  25. bx_import('BxDolProfile');
  26. $oProfile = BxDolProfile::getInstance($iProfileId);
  27. if (!$oProfile)
  28. return;
  29. bx_import('BxDolConnection');
  30. $oConnection = BxDolConnection::getObjectInstance($this->_sObjectConnections);
  31. if (!$oConnection)
  32. return;
  33. if ($oProfile->id() == bx_get_logged_profile_id())
  34. $this->_bOwner = true;
  35. $aSQLParts = $oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $oProfile->id(), $this->_bOwner ? false : true);
  36. $this->addMarkers(array(
  37. 'profile_id' => $oProfile->id(),
  38. 'join_connections' => $aSQLParts['join'],
  39. 'content_module' => $this->_sContentModule,
  40. ));
  41. }
  42. /**
  43. * 'accept' action handler
  44. */
  45. public function performActionAccept()
  46. {
  47. $iId = 0;
  48. $aIds = bx_get('ids');
  49. if ($aIds && is_array($aIds))
  50. $iId = (int)array_pop($aIds);
  51. if (!$iId) {
  52. $this->_echoResultJson(array('msg' => _t('_sys_txt_error_occured')), true);
  53. exit;
  54. }
  55. bx_import('BxDolConnection');
  56. $oConn = BxDolConnection::getObjectInstance($this->_sObjectConnections);
  57. $a = $oConn->actionAdd($iId);
  58. if (isset($a['err']) && $a['err'])
  59. $this->_echoResultJson(array('msg' => $a['msg']), true);
  60. else
  61. $this->_echoResultJson(array('grid' => $this->getCode(false), 'blink' => $iId), true);
  62. }
  63. /**
  64. * 'add friend' action handler
  65. */
  66. public function performActionAddFriend()
  67. {
  68. return $this->performActionAccept();
  69. }
  70. protected function _delete ($mixedId) {
  71. bx_import('BxDolConnection');
  72. $oConn = BxDolConnection::getObjectInstance($this->_sObjectConnections);
  73. if ($oConn->isConnected(bx_get_logged_profile_id(), (int)$mixedId, true))
  74. $a = $oConn->actionRemove($mixedId);
  75. else
  76. $a = $oConn->actionReject($mixedId);
  77. return isset($a['err']) && $a['err'] ? false : true;
  78. }
  79. protected function _getCellName ($mixedValue, $sKey, $aField, $aRow)
  80. {
  81. bx_import('BxDolProfile');
  82. $oProfile = BxDolProfile::getInstance($aRow['id']);
  83. if (!$oProfile)
  84. return _t('_sys_txt_error_occured');
  85. return parent::_getCellDefault ($oProfile->getUnit(), $sKey, $aField, $aRow);
  86. }
  87. protected function _getCellInfo ($mixedValue, $sKey, $aField, $aRow)
  88. {
  89. $s = '';
  90. bx_import('BxDolConnection');
  91. $oConn = BxDolConnection::getObjectInstance($this->_sObjectConnections);
  92. // for friend requests display mutual friends
  93. if ($this->_bOwner && !$aRow['mutual']) {
  94. $a = $oConn->getCommonContent($aRow['id'], bx_get_logged_profile_id(), true);
  95. $i = count($a);
  96. if (1 == $i) {
  97. $iProfileId = array_pop($a);
  98. bx_import('BxDolProfile');
  99. $oProfile = BxDolProfile::getInstance($iProfileId);
  100. $s = _t('_sys_txt_one_mutual_friend', $oProfile->getUrl(), $oProfile->getDisplayName());
  101. } elseif ($i) {
  102. $s = _t('_sys_txt_n_mutual_friends', $i);
  103. }
  104. }
  105. // display friends number if no other info is available
  106. if (!$s) {
  107. $a = $oConn->getConnectedContent($aRow['id'], true);
  108. $i = count($a);
  109. $s = _t('_sys_txt_n_friends', $i);
  110. }
  111. return parent::_getCellDefault ($s, $sKey, $aField, $aRow);
  112. }
  113. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  114. {
  115. if (!$this->_bOwner)
  116. return '';
  117. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  118. }
  119. protected function _getActionAccept ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  120. {
  121. if (!$this->_bOwner || $aRow['mutual'])
  122. return '';
  123. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  124. }
  125. protected function _getActionAddFriend ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  126. {
  127. if ($this->_bOwner || $aRow['id'] == bx_get_logged_profile_id())
  128. return '';
  129. bx_import('BxDolConnection');
  130. $oConn = BxDolConnection::getObjectInstance($this->_sObjectConnections);
  131. if ($oConn->isConnected($aRow['id'], bx_get_logged_profile_id()) || $oConn->isConnected(bx_get_logged_profile_id(), $aRow['id']))
  132. return '';
  133. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  134. }
  135. }
  136. /** @} */