BxAclGridAdministration.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 PaidLevels Paid Levels
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxAclGridLevels.php');
  12. class BxAclGridAdministration extends BxAclGridLevels
  13. {
  14. protected $_sParamsDivider = '#-#';
  15. protected $_aLevels = [];
  16. protected $_iLevelId = 0;
  17. public function __construct ($aOptions, $oTemplate = false)
  18. {
  19. parent::__construct ($aOptions, $oTemplate);
  20. $this->_aLevels = $this->_oModule->_oDb->getLevels(['type' => 'for_selector']);
  21. if(($iLevelId = bx_get('level')) !== false) {
  22. $this->_iLevelId = (int)$iLevelId;
  23. $this->_aQueryAppend['level'] = $this->_iLevelId;
  24. }
  25. }
  26. public function performActionAdd()
  27. {
  28. $CNF = &$this->_oModule->_oConfig->CNF;
  29. $sAction = 'add';
  30. $sFilter = bx_get('filter');
  31. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  32. list($this->_iLevelId, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  33. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PRICE'], $CNF['OBJECT_FORM_PRICE_DISPLAY_ADD']);
  34. $aActionParams = ['o' => $this->_sObject, 'a' => $sAction];
  35. if($this->_iLevelId) {
  36. $aActionParams['level'] = $this->_iLevelId;
  37. $oForm->setLevelId($this->_iLevelId);
  38. }
  39. $oForm->setAction(bx_append_url_params(BX_DOL_URL_ROOT . 'grid.php', $aActionParams));
  40. $oForm->initChecker();
  41. if($oForm->isSubmittedAndValid()) {
  42. $iLevelId = $oForm->getCleanValue('level_id');
  43. $iPeriod = $oForm->getCleanValue('period');
  44. $sPeriodUnit = $oForm->getCleanValue('period_unit');
  45. if(!empty($iPeriod) && empty($sPeriodUnit))
  46. return echoJson(['msg' => _t('_bx_acl_form_price_input_err_period_unit')]);
  47. $aPriceSimilar = $this->_oModule->_oDb->getPrices([
  48. 'type' => 'by_level_id_duration',
  49. 'level_id' => $iLevelId,
  50. 'period' => $iPeriod,
  51. 'period_unit' => $sPeriodUnit
  52. ]);
  53. if(($iId = (int)$oForm->insert(['added' => time(), 'order' => $this->_oModule->_oDb->getPriceOrderMax($iLevelId) + 1])) != 0) {
  54. //TODO: May be we don't need to have this 'Purchasable' flag at all or at least we shouldn't update it from here.
  55. $this->_oModule->_oDb->updateLevels(['Purchasable' => 'yes'], ['ID' => $iLevelId]);
  56. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  57. if(!empty($aPriceSimilar) && is_array($aPriceSimilar))
  58. $aRes['msg'] = _t('_bx_acl_err_price_duplicate');
  59. }
  60. else
  61. $aRes = ['msg' => _t('_bx_acl_err_cannot_perform')];
  62. echoJson($aRes);
  63. return;
  64. }
  65. bx_import('BxTemplStudioFunctions');
  66. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_price'), _t('_bx_acl_popup_title_price_add'), $this->_oModule->_oTemplate->parseHtmlByName('popup_price.html', [
  67. 'form_id' => $oForm->aFormAttrs['id'],
  68. 'form' => $oForm->getCode(true),
  69. 'object' => $this->_sObject,
  70. 'action' => $sAction
  71. ]));
  72. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  73. }
  74. public function performActionEdit()
  75. {
  76. $CNF = &$this->_oModule->_oConfig->CNF;
  77. $sAction = 'edit';
  78. $aIds = $this->_getIds();
  79. if($aIds === false)
  80. return echoJson([]);
  81. $iId = $aIds[0];
  82. $aItem = $this->_oModule->_oDb->getPrices(['type' => 'by_id', 'value' => $iId]);
  83. if(!is_array($aItem) || empty($aItem)) {
  84. echoJson([]);
  85. exit;
  86. }
  87. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PRICE'], $CNF['OBJECT_FORM_PRICE_DISPLAY_EDIT']);
  88. $oForm->setAction(BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&level=' . $this->_iLevelId);
  89. $oForm->initChecker($aItem);
  90. if($oForm->isSubmittedAndValid()) {
  91. if($oForm->update($aItem['id']) !== false)
  92. $aRes = ['grid' => $this->getCode(false), 'blink' => $aItem['id']];
  93. else
  94. $aRes = ['msg' => _t('_bx_acl_err_cannot_perform')];
  95. echoJson($aRes);
  96. return;
  97. }
  98. bx_import('BxTemplStudioFunctions');
  99. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_price'), _t('_bx_acl_popup_title_price_edit'), $this->_oModule->_oTemplate->parseHtmlByName('popup_price.html', [
  100. 'form_id' => $oForm->aFormAttrs['id'],
  101. 'form' => $oForm->getCode(true),
  102. 'object' => $this->_sObject,
  103. 'action' => $sAction
  104. ]));
  105. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  106. }
  107. public function performActionDelete()
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. $aIds = bx_get('ids');
  111. if(!$aIds || !is_array($aIds)) {
  112. echoJson([]);
  113. return;
  114. }
  115. $iAffected = 0;
  116. $aIdsAffected = [];
  117. foreach($aIds as $iId)
  118. if($this->_oModule->_oDb->deletePrices(['id' => $iId])) {
  119. $aIdsAffected[] = $iId;
  120. $iAffected++;
  121. }
  122. echoJson($iAffected ? ['grid' => $this->getCode(false), 'blink' => $aIdsAffected] : ['msg' => _t('_bx_acl_err_cannot_perform')]);
  123. }
  124. protected function _addJsCss()
  125. {
  126. parent::_addJsCss();
  127. $this->_oModule->_oTemplate->addStudioJs(array('jquery.form.min.js', 'administration.js'));
  128. $this->_oModule->_oTemplate->addStudioCss(array('administration.css'));
  129. $oForm = new BxTemplFormView(array());
  130. $oForm->addCssJs();
  131. }
  132. protected function _getCellLevelId($mixedValue, $sKey, $aField, $aRow)
  133. {
  134. $mixedValue = _t($this->_aLevels[$mixedValue] ?? '_undefined');
  135. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  136. }
  137. protected function _getFilterControls()
  138. {
  139. parent::_getFilterControls();
  140. $sContent = '';
  141. $oForm = new BxTemplFormView([]);
  142. $aInputLevels = [
  143. 'type' => 'select',
  144. 'name' => 'level',
  145. 'attrs' => [
  146. 'id' => 'bx-grid-level-' . $this->_sObject,
  147. 'onChange' => 'javascript:' . $this->_oModule->_oConfig->getJsObject('administration') . '.onChangeLevel()'
  148. ],
  149. 'value' => $this->_iLevelId,
  150. 'values' => array(0 => _t('_bx_acl_txt_all_level'))
  151. ];
  152. foreach($this->_aLevels as $iId => $sTitle)
  153. $aInputLevels['values'][$iId] = _t($sTitle);
  154. $sContent .= $oForm->genRow($aInputLevels);
  155. $aInputSearch = [
  156. 'type' => 'text',
  157. 'name' => 'keyword',
  158. 'attrs' => [
  159. 'id' => 'bx-grid-search-' . $this->_sObject,
  160. ]
  161. ];
  162. $sContent .= $oForm->genRow($aInputSearch);
  163. return $sContent;
  164. }
  165. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  166. {
  167. if(empty($this->_iLevelId) && strpos($sFilter, $this->_sParamsDivider) !== false)
  168. list($this->_iLevelId, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  169. if(!empty($this->_iLevelId))
  170. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `level_id`=? ", $this->_iLevelId);
  171. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  172. }
  173. protected function _isVisibleGrid ($a)
  174. {
  175. return isAdmin();
  176. }
  177. }
  178. /** @} */