BxDolGridConnectionIn.php 3.9 KB

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