1
0

BxAclGridAdministration.php 8.0 KB

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