BxCreditsGridProfilesAdministration.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 BxCreditsGridProfilesAdministration extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. $this->_sModule = 'bx_credits';
  18. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  19. if(!$oTemplate)
  20. $oTemplate = $this->_oModule->_oTemplate;
  21. parent::__construct ($aOptions, $oTemplate);
  22. }
  23. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  24. {
  25. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&id=' . $iId;
  26. }
  27. public function performActionEdit()
  28. {
  29. $sAction = 'edit';
  30. $aIds = bx_get('ids');
  31. if(!$aIds || !is_array($aIds)) {
  32. $iId = (int)bx_get('id');
  33. if(!$iId)
  34. return false;
  35. $aIds = [$iId];
  36. }
  37. $iId = $aIds[0];
  38. $aProfile = $this->_oModule->_oDb->getProfile(['type' => 'id', 'id' => $iId]);
  39. if(empty($aProfile) || !is_array($aProfile))
  40. return $this->_bIsApi ? [] : echoJson([]);
  41. $oForm = $this->_getFormObject($sAction, $aProfile);
  42. $oForm->initChecker($aProfile);
  43. if($oForm->isSubmittedAndValid()) {
  44. if($oForm->update($iId) !== false)
  45. $aRes = $this->_bIsApi ? [] : ['grid' => $this->getCode(false), 'blink' => $iId];
  46. else
  47. $aRes = ($sMsg = _t('_bx_credits_txt_err_cannot_perform_action')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  48. return $this->_bIsApi ? $aRes : echoJson($aRes);
  49. }
  50. else {
  51. if($this->_bIsApi)
  52. return $this->getFormBlockAPI($oForm, $sAction, $iId);
  53. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('edit_profile_popup'), _t('_bx_credits_grid_popup_title_pfl_edit', BxDolProfile::getInstanceMagic($aProfile['id'])->getDisplayName()), $this->_oModule->_oTemplate->parseHtmlByName('profile_form.html', [
  54. 'form_id' => $oForm->aFormAttrs['id'],
  55. 'form' => $oForm->getCode(true),
  56. 'object' => $this->_sObject,
  57. 'action' => $sAction
  58. ]));
  59. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  60. }
  61. }
  62. protected function _getFormObject($sAction, $aProfile = array())
  63. {
  64. $CNF = &$this->_oModule->_oConfig->CNF;
  65. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PROFILE'], $CNF['OBJECT_FORM_PROFILE_DISPLAY_' . strtoupper($sAction)]);
  66. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction;
  67. if(!empty($aProfile['id']))
  68. $oForm->aFormAttrs['action'] .= '&id=' . $aProfile['id'];
  69. return $oForm;
  70. }
  71. protected function _getCellId($mixedValue, $sKey, $aField, $aRow)
  72. {
  73. return parent::_getCellDefault($this->_getProfile($mixedValue), $sKey, $aField, $aRow);
  74. }
  75. protected function _getCellBalanceCleared($mixedValue, $sKey, $aField, $aRow)
  76. {
  77. return parent::_getCellDefault($this->_oModule->getProfileBalanceCleared($aRow['id']), $sKey, $aField, $aRow);
  78. }
  79. protected function _getProfile($mixedValue)
  80. {
  81. $oProfile = BxDolProfile::getInstanceMagic($mixedValue);
  82. if(!$oProfile)
  83. return $mixedValue;
  84. return $oProfile->getUnit(0, ['template' => ['name' => 'unit', 'size' => 'icon']]);
  85. }
  86. }
  87. /** @} */