1
0

BxDolGridSubscribedMe.php 3.6 KB

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