BxCreditsGrid.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Credits Credits
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxCreditsGrid extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_iUserId;
  16. public function __construct ($aOptions, $oTemplate = false)
  17. {
  18. $this->_sModule = 'bx_credits';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. if(!$oTemplate)
  21. $oTemplate = $this->_oModule->_oTemplate;
  22. parent::__construct ($aOptions, $oTemplate);
  23. $iUserId = bx_get_logged_profile_id();
  24. if($iUserId !== false) {
  25. $this->_iUserId = (int)$iUserId;
  26. $this->_aQueryAppend['user_id'] = $this->_iUserId;
  27. }
  28. }
  29. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  30. {
  31. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&id=' . $iId;
  32. }
  33. protected function _getProfile($mixedValue)
  34. {
  35. if(is_numeric($mixedValue) && (int)$mixedValue == 0)
  36. $mixedValue = $this->_oModule->_oConfig->getAuthor();
  37. $oProfile = BxDolProfile::getInstanceMagic($mixedValue);
  38. if(!$oProfile)
  39. return $mixedValue;
  40. return $oProfile->getUnit(0, ['template' => ['name' => 'unit', 'size' => 'icon']]);
  41. }
  42. protected function _performActionWithProfileAmount($sAction)
  43. {
  44. $CNF = &$this->_oModule->_oConfig->CNF;
  45. $oForm = $this->_getFormObject($sAction);
  46. $oForm->initChecker();
  47. if($oForm->isSubmittedAndValid()) {
  48. $iProfile = (int)$oForm->getCleanValue('profile');
  49. $fAmount = (float)$oForm->getCleanValue('amount');
  50. $sMessage = $oForm->getCleanValue('message');
  51. $aResult = $this->_oModule->{'process' . bx_gen_method_name($sAction)}($this->_iUserId, $iProfile, $fAmount, $sMessage);
  52. $aResult = $this->_onPerformAction($aResult);
  53. return $this->_bIsApi ? $aResult : echoJson($aResult);
  54. }
  55. else {
  56. if($this->_bIsApi)
  57. return $this->getFormBlockAPI($oForm, $sAction);
  58. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds($sAction . '_popup'), _t($CNF['T'][$sAction . '_popup']), $this->_oModule->_oTemplate->parseHtmlByName('credit_form.html', [
  59. 'form_id' => $oForm->aFormAttrs['id'],
  60. 'form' => $oForm->getCode(true),
  61. 'object' => $this->_sObject,
  62. 'action' => $sAction
  63. ]));
  64. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  65. }
  66. }
  67. protected function _onPerformAction($aResult)
  68. {
  69. $aRes = [];
  70. if((int)$aResult['code'] == 0) {
  71. if(!empty($aResult['id']))
  72. $aRes = $this->_bIsApi ? [] : ['grid' => $this->getCode(false), 'blink' => $aResult['id']];
  73. else
  74. $aRes = ($sMsg = _t(!empty($aResult['msg']) ? $aResult['msg'] : '_bx_credits_msg_action_performed')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  75. }
  76. else
  77. $aRes = ($sMsg = _t(!empty($aResult['msg']) ? $aResult['msg'] : '_bx_credits_err_cannot_perform_action')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  78. return $aRes;
  79. }
  80. protected function _getFormObject($sAction)
  81. {
  82. $CNF = &$this->_oModule->_oConfig->CNF;
  83. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CREDIT'], $CNF['OBJECT_FORM_CREDIT_DISPLAY_' . strtoupper($sAction)]);
  84. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction;
  85. $oForm->setUserId($this->_iUserId);
  86. return $oForm;
  87. }
  88. protected function _getId()
  89. {
  90. $aIds = bx_get('ids');
  91. if(!empty($aIds) && is_array($aIds))
  92. return array_shift($aIds);
  93. if(($iId = bx_get('id')) !== false)
  94. return (int)$iId;
  95. return false;
  96. }
  97. }
  98. /** @} */