BxGitHubGridApps.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 BxGitHubGridApps 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 getFormBlockTitleAPI($sAction, $iId = 0)
  21. {
  22. $sResult = '';
  23. switch($sAction) {
  24. case 'add':
  25. $sResult = _t('_bx_github_grid_popup_title_apps_add');
  26. break;
  27. case 'edit':
  28. $sResult = _t('_bx_github_grid_popup_title_apps_edit');
  29. break;
  30. }
  31. return $sResult;
  32. }
  33. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  34. {
  35. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&id=' . $iId;
  36. }
  37. public function setProfileId($iProfileId)
  38. {
  39. $this->_iProfileId = (int)$iProfileId;
  40. $this->_aQueryAppend['profile_id'] = $this->_iProfileId;
  41. }
  42. public function performActionAdd()
  43. {
  44. $CNF = &$this->_oModule->_oConfig->CNF;
  45. $sAction = 'add';
  46. $oForm = $this->_getFormObject($sAction, $CNF['OBJECT_FORM_APPS_DISPLAY_ADD']);
  47. $oForm->initChecker();
  48. if($oForm->isSubmittedAndValid()) {
  49. if(($iId = (int)$oForm->insert(['profile_id' => $this->_iProfileId, 'added' => time()])) != 0)
  50. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  51. else
  52. $aRes = ['msg' => _t('_bx_github_err_cannot_perform_action')];
  53. return echoJson($aRes);
  54. }
  55. if($this->_bIsApi)
  56. return $this->getFormBlockAPI($oForm, $sAction);
  57. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('apps_popup') . $sAction, _t('_bx_github_grid_popup_title_apps_' . $sAction), $this->_oModule->_oTemplate->parseHtmlByName('popup_apps.html', [
  58. 'form_id' => $oForm->aFormAttrs['id'],
  59. 'form' => $oForm->getCode(true),
  60. 'object' => $this->_sObject,
  61. 'action' => $sAction
  62. ]));
  63. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  64. }
  65. public function performActionEdit()
  66. {
  67. $CNF = &$this->_oModule->_oConfig->CNF;
  68. $sAction = 'edit';
  69. $iId = $this->_getId();
  70. $aItem = $this->_oModule->_oDb->getApps(['sample' => 'id', 'id' => $iId]);
  71. if(!$aItem)
  72. return echoJson([]);
  73. $oForm = $this->_getFormObject($sAction, $CNF['OBJECT_FORM_APPS_DISPLAY_EDIT'], ['id' => $iId]);
  74. $oForm->initChecker($aItem);
  75. if($oForm->isSubmittedAndValid()) {
  76. if($oForm->update($aItem['id']) !== false)
  77. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  78. else
  79. $aRes = ['msg' => _t('_bx_github_err_cannot_perform_action')];
  80. return echoJson($aRes);
  81. }
  82. if($this->_bIsApi)
  83. return $this->getFormBlockAPI($oForm, $sAction, $iId);
  84. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('apps_popup') . $sAction, _t('_bx_github_grid_popup_title_apps_' . $sAction), $this->_oModule->_oTemplate->parseHtmlByName('popup_apps.html', [
  85. 'form_id' => $oForm->aFormAttrs['id'],
  86. 'form' => $oForm->getCode(true),
  87. 'object' => $this->_sObject,
  88. 'action' => $sAction
  89. ]));
  90. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  91. }
  92. protected function _getCellCallbackUrl($mixedValue, $sKey, $aField, $aRow)
  93. {
  94. $mixedValue = BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'authorize/' . $aRow['id'];
  95. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  96. }
  97. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  98. {
  99. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(' AND `profile_id`=?', $this->_iProfileId);
  100. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  101. }
  102. protected function _getFormObject($sAction, $sDisplay, $aParams = [])
  103. {
  104. $CNF = &$this->_oModule->_oConfig->CNF;
  105. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_APPS'], $sDisplay);
  106. $oForm->setId($sDisplay);
  107. $oForm->setName($sDisplay);
  108. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', array_merge([
  109. 'o' => $this->_sObject,
  110. 'a' => $sAction,
  111. 'profile_id' => $this->_iProfileId
  112. ], $aParams)));
  113. return $oForm;
  114. }
  115. protected function _getId()
  116. {
  117. if(($aIds = bx_get('ids')) && is_array($aIds))
  118. return reset($aIds);
  119. if(($iId = bx_get('id')) !== false)
  120. return (int)$iId;
  121. return false;
  122. }
  123. }
  124. /** @} */