BxDolGridConnectionsin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 BxDolGridConnectedMe extends BxTemplGrid
  11. {
  12. protected $_bInit = false;
  13. protected $_bOwner = false;
  14. protected $_iProfileId = 0;
  15. protected $_oConnection = null;
  16. protected $_sConnectionObject = '';
  17. protected $_sConnectionMethod = 'getConnectedInitiatorsAsSQLParts';
  18. public function __construct ($aOptions, $oTemplate = false)
  19. {
  20. parent::__construct ($aOptions, $oTemplate);
  21. $this->_sDefaultSortingOrder = 'DESC';
  22. $this->_aQueryAppendExclude[] = 'join_connections';
  23. $this->_bInit = false;
  24. $this->_bOwner = false;
  25. $this->_iProfileId = 0;
  26. $this->_oConnection = null;
  27. $this->_sConnectionObject = '';
  28. $this->_sConnectionMethod = 'getConnectedInitiatorsAsSQLParts';
  29. }
  30. public function init()
  31. {
  32. if(!$this->_iProfileId)
  33. return false;
  34. $oProfile = BxDolProfile::getInstance($this->_iProfileId);
  35. if(!$oProfile)
  36. return false;
  37. if($oProfile->id() == bx_get_logged_profile_id())
  38. $this->_bOwner = true;
  39. $this->_oConnection = BxDolConnection::getObjectInstance($this->_sConnectionObject);
  40. if(!$this->_oConnection)
  41. return false;
  42. $aSQLParts = $this->_oConnection->{$this->_sConnectionMethod}('p', 'id', $oProfile->id());
  43. $this->addMarkers(array(
  44. 'profile_id' => $oProfile->id(),
  45. 'join_connections' => $aSQLParts['join']
  46. ));
  47. return true;
  48. }
  49. public function setProfileId($iProfileId)
  50. {
  51. $this->_iProfileId = (int)$iProfileId;
  52. $this->_bInit = $this->init();
  53. }
  54. public function getCode ($isDisplayHeader = true)
  55. {
  56. if(!$this->_bInit)
  57. return '';
  58. return parent::getCode($isDisplayHeader);
  59. }
  60. protected function _getCellName ($mixedValue, $sKey, $aField, $aRow)
  61. {
  62. $oProfile = BxDolProfile::getInstance($aRow['id']);
  63. if (!$oProfile)
  64. return _t('_sys_txt_error_occured');
  65. return parent::_getCellDefault ($oProfile->getUnit(), $sKey, $aField, $aRow);
  66. }
  67. /**
  68. * 'accept' action handler
  69. */
  70. public function performActionAdd()
  71. {
  72. list($iId, $iViewedId) = $this->_prepareIds();
  73. if(!$iId)
  74. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  75. $a = $this->_oConnection->actionAdd($iId, $iViewedId);
  76. if(!empty($a['err']))
  77. return echoJson(array('msg' => $a['msg']));
  78. return echoJson(array('grid' => $this->getCode(false), 'blink' => $iId));
  79. }
  80. protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  81. {
  82. $iViewerId = bx_get_logged_profile_id();
  83. if(!isLogged() || $iViewerId == $aRow['id'] || $this->_oConnection->isConnected($iViewerId, $aRow['id']))
  84. return '';
  85. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  86. }
  87. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  88. {
  89. if (!isLogged() || !$this->_bOwner)
  90. return '';
  91. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  92. }
  93. protected function _delete ($mixedId)
  94. {
  95. list($iId, $iViewedId) = $this->_prepareIds();
  96. if(!$this->_oConnection->isConnected($iViewedId, $iId))
  97. return true;
  98. return $this->_oConnection->removeConnection($iViewedId, $iId);
  99. }
  100. protected function _prepareIds ()
  101. {
  102. $iViewedId = (int)bx_get_logged_profile_id();
  103. $iId = 0;
  104. $aIds = bx_get('ids');
  105. if($aIds && is_array($aIds))
  106. $mixedId = array_pop($aIds);
  107. if(strpos($mixedId, ':') !== false) {
  108. list($iId, $iViewedId) = explode (':', $mixedId);
  109. $iId = (int)$iId;
  110. $iViewedId = (int)$iViewedId;
  111. }
  112. else
  113. $iId = (int)$mixedId;
  114. return array($iId, $iViewedId);
  115. }
  116. }
  117. /** @} */