BxReputationGridLevels.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Reputation Reputation
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxReputationGridLevels extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. $this->_sModule = 'bx_reputation';
  18. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  19. parent::__construct ($aOptions, $oTemplate);
  20. }
  21. public function getCode($isDisplayHeader = true)
  22. {
  23. $mixedResult = parent::getCode($isDisplayHeader);
  24. if(!$mixedResult)
  25. return $mixedResult;
  26. return $this->_oModule->_oTemplate->getJsCode('levels', ['sObjNameGrid' => $this->_sObject]) . $mixedResult;
  27. }
  28. public function performActionAdd()
  29. {
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. $sAction = 'add';
  32. $sForm = $CNF['OBJECT_FORM_LEVEL_DISPLAY_ADD'];
  33. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LEVEL'], $CNF['OBJECT_FORM_LEVEL_DISPLAY_ADD']);
  34. $oForm->setId($sForm);
  35. $oForm->setName($sForm);
  36. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', [
  37. 'o' => $this->_sObject,
  38. 'a' => $sAction
  39. ]));
  40. $oForm->initChecker();
  41. if($oForm->isSubmittedAndValid()) {
  42. $aValsToAdd = [
  43. 'order' => $this->_oModule->_oDb->getLevelOrderMax() + 1,
  44. 'date' => time()
  45. ];
  46. $iLevel = $oForm->insert($aValsToAdd);
  47. if(!$iLevel)
  48. return $this->_getActionResult(['msg' => _t($CNF['T']['err_cannot_perform'])]);
  49. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iLevel]);
  50. }
  51. if($this->_bIsApi)
  52. return $this->getFormBlockAPI($oForm, $sAction);
  53. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('level_popup'), _t($CNF['T']['popup_title_level_add']), $this->_oModule->_oTemplate->parseHtmlByName('popup_level.html', [
  54. 'form_id' => $oForm->getId(),
  55. 'form' => $oForm->getCode(true),
  56. 'object' => $this->_sObject,
  57. 'action' => $sAction
  58. ]));
  59. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  60. }
  61. public function performActionEdit()
  62. {
  63. $CNF = &$this->_oModule->_oConfig->CNF;
  64. $sAction = 'edit';
  65. $aIds = $this->_getIds();
  66. if($aIds === false)
  67. return $this->_getActionResult([]);
  68. $iLevel = array_shift($aIds);
  69. $aLevel = $this->_oModule->_oDb->getLevels(['sample' => 'id', 'id' => $iLevel]);
  70. if(!is_array($aLevel) || empty($aLevel))
  71. return $this->_getActionResult([]);
  72. $sForm = $CNF['OBJECT_FORM_LEVEL_DISPLAY_EDIT'];
  73. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LEVEL'], $CNF['OBJECT_FORM_LEVEL_DISPLAY_EDIT']);
  74. $oForm->setId($sForm);
  75. $oForm->setName($sForm);
  76. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', [
  77. 'o' => $this->_sObject,
  78. 'a' => $sAction,
  79. 'id' => $iLevel
  80. ]));
  81. $oForm->initChecker($aLevel);
  82. if($oForm->isSubmittedAndValid()) {
  83. if(!$oForm->update($iLevel))
  84. return $this->_getActionResult(['msg' => _t($CNF['T']['err_cannot_perform'])]);
  85. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iLevel]);
  86. }
  87. if($this->_bIsApi)
  88. return $this->getFormBlockAPI($oForm, $sAction, $iLevel);
  89. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('level_popup'), _t($CNF['T']['popup_title_level_edit']), $this->_oModule->_oTemplate->parseHtmlByName('popup_level.html', [
  90. 'form_id' => $oForm->getId(),
  91. 'form' => $oForm->getCode(true),
  92. 'object' => $this->_sObject,
  93. 'action' => $sAction
  94. ]));
  95. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  96. }
  97. protected function _getCellIcon($mixedValue, $sKey, $aField, $aRow)
  98. {
  99. $mixedValue = $this->_oTemplate->getIcon($mixedValue, ['class' => 'bx-reputation-level-icon']);
  100. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  101. }
  102. protected function _getCellPointsOut($mixedValue, $sKey, $aField, $aRow)
  103. {
  104. if((int)$mixedValue == 0)
  105. $mixedValue = _t('_lifetime');
  106. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  107. }
  108. protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
  109. {
  110. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  111. }
  112. protected function _getIds()
  113. {
  114. $aIds = bx_get('ids');
  115. if(!$aIds || !is_array($aIds)) {
  116. $iId = (int)bx_get('id');
  117. if(!$iId)
  118. return false;
  119. $aIds = [$iId];
  120. }
  121. return $aIds;
  122. }
  123. }
  124. /** @} */