BxDolGridConnectionIn.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $aParams = array('template' => array('name' => 'unit', 'size' => 'thumb'));
  73. if(BxDolModule::getInstance($oProfile->getModule()) instanceof BxBaseModGroupsModule)
  74. $aParams['template']['name'] = 'unit_wo_cover';
  75. return parent::_getCellDefault ($oProfile->getUnit(0, $aParams), $sKey, $aField, $aRow);
  76. }
  77. public function performActionAdd()
  78. {
  79. list($iId, $iViewedId) = $this->_prepareIds();
  80. if(!$iId)
  81. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  82. $a = $this->_oConnection->actionAdd($iId, $iViewedId);
  83. if(!empty($a['err']))
  84. return echoJson(array('msg' => $a['msg']));
  85. return echoJson(array('grid' => $this->getCode(false), 'blink' => $iId));
  86. }
  87. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  88. {
  89. $iViewerId = bx_get_logged_profile_id();
  90. if(!isLogged() || $iViewerId == $aRow['id'] || $this->_oConnection->isConnected($iViewerId, $aRow['id']))
  91. return '';
  92. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  93. }
  94. protected function _prepareIds ()
  95. {
  96. $iViewedId = (int)bx_get_logged_profile_id();
  97. $iId = 0;
  98. $aIds = bx_get('ids');
  99. if($aIds && is_array($aIds))
  100. $mixedId = array_pop($aIds);
  101. if(strpos($mixedId, ':') !== false) {
  102. list($iId, $iViewedId) = explode (':', $mixedId);
  103. $iId = (int)$iId;
  104. $iViewedId = (int)$iViewedId;
  105. }
  106. else
  107. $iId = (int)$mixedId;
  108. return array($iId, $iViewedId);
  109. }
  110. }
  111. /** @} */