BxDolGridSubscribedMe.php 3.5 KB

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