1
0

BxGitHubGridAuthorizations.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * @defgroup Tasks Tasks
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. class BxGitHubGridAuthorizations extends BxBaseModGeneralGrid
  11. {
  12. protected $_iProfileId;
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. $this->_sModule = 'bx_github';
  16. parent::__construct ($aOptions, $oTemplate);
  17. if(($sFfName = 'profile_id') && ($iValue = bx_get($sFfName)) !== false)
  18. $this->setProfileId($iValue);
  19. }
  20. public function setProfileId($iProfileId)
  21. {
  22. $this->_iProfileId = (int)$iProfileId;
  23. $this->_aQueryAppend['profile_id'] = $this->_iProfileId;
  24. }
  25. public function performActionRefresh()
  26. {
  27. $iId = $this->_getId();
  28. $aItem = $this->_oModule->_oDb->getAuthorizations(['sample' => 'id', 'id' => $iId]);
  29. if(!$aItem)
  30. return echoJson([]);
  31. if($this->_oModule->refreshAccessToken($this->_iProfileId, $aItem['app_id'], $aItem['refresh_token']) !== false)
  32. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  33. else
  34. $aRes = ['msg' => _t('_bx_github_err_cannot_perform_action')];
  35. return echoJson($aRes);
  36. }
  37. protected function _getCellAppId($mixedValue, $sKey, $aField, $aRow)
  38. {
  39. if($mixedValue)
  40. $mixedValue = $aRow['app_title'] ?: _t('_undefined');
  41. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  42. }
  43. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  44. {
  45. return $this->_getCellDefaultDateTime($mixedValue, $sKey, $aField, $aRow);
  46. }
  47. protected function _getCellChanged($mixedValue, $sKey, $aField, $aRow)
  48. {
  49. return $this->_getCellDefaultDateTime($mixedValue, $sKey, $aField, $aRow);
  50. }
  51. protected function _getCellAtExpiresIn($mixedValue, $sKey, $aField, $aRow)
  52. {
  53. if(!$mixedValue)
  54. return parent::_getCellDefault(_t('_never'), $sKey, $aField, $aRow);
  55. return $this->_getCellDefaultDateTime($aRow['changed'] + $mixedValue, $sKey, $aField, $aRow);
  56. }
  57. protected function _getCellDefaultDateTime($mixedValue, $sKey, $aField, $aRow)
  58. {
  59. if($this->_bIsApi)
  60. return ['type' => 'time', 'data' => $mixedValue];
  61. return parent::_getCellDefault(bx_time_js($mixedValue, BX_FORMAT_DATE_TIME, true), $sKey, $aField, $aRow);
  62. }
  63. protected function _getActionRefresh($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  64. {
  65. if(!$aRow['at_expires_in'] || !$aRow['refresh_token'])
  66. return $this->_bIsApi ? [] : '';
  67. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  68. }
  69. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  70. {
  71. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(' AND `tan`.`profile_id`=?', $this->_iProfileId);
  72. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  73. }
  74. protected function _getId()
  75. {
  76. if(($aIds = bx_get('ids')) && is_array($aIds))
  77. return reset($aIds);
  78. if(($iId = bx_get('id')) !== false)
  79. return (int)$iId;
  80. return false;
  81. }
  82. }
  83. /** @} */