BxAclGridAdministration.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup ACL ACL
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxAclGridAdministration extends BxTemplGrid
  12. {
  13. protected $MODULE;
  14. protected $_oModule;
  15. protected $_sParamsDivider = '#-#';
  16. protected $_iLevelId = 0;
  17. public function __construct ($aOptions, $oTemplate = false)
  18. {
  19. $this->MODULE = 'bx_acl';
  20. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  21. parent::__construct ($aOptions, $oTemplate);
  22. $iLevel = (int)bx_get('level');
  23. if($iLevel > 0) {
  24. $this->_iLevelId = $iLevel;
  25. $this->_aQueryAppend['level'] = $this->_iLevelId;
  26. }
  27. }
  28. public function performActionAdd()
  29. {
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. $sAction = 'add';
  32. $sFilter = bx_get('filter');
  33. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  34. list($this->_iLevelId, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  35. if((int)$this->_iLevelId == 0)
  36. $this->_iLevelId = (int)bx_get('level_id');
  37. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PRICE'], $CNF['OBJECT_FORM_PRICE_DISPLAY_ADD']);
  38. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&level=' . $this->_iLevelId;
  39. $oForm->aInputs['level_id']['value'] = $this->_iLevelId;
  40. $oForm->initChecker();
  41. if($oForm->isSubmittedAndValid()) {
  42. $iLevel = $oForm->getCleanValue('level_id');
  43. $iPeriod = $oForm->getCleanValue('period');
  44. $sPeriodUnit = $oForm->getCleanValue('period_unit');
  45. $aPrice = $this->_oModule->_oDb->getPrices(array('type' => 'by_level_id_duration', 'level_id' => $iLevel, 'period' => $iPeriod, 'period_unit' => $sPeriodUnit));
  46. if(!empty($aPrice) && is_array($aPrice)) {
  47. echoJson(array('msg' => _t('_bx_acl_err_price_duplicate')));
  48. return;
  49. }
  50. $aLevel = $this->_oModule->_oDb->getLevels(array('type' => 'by_id', 'value' => $iLevel));
  51. $sName = uriGenerate(strtolower(_t($aLevel['name'])) . ' ' . $iPeriod . ' ' . $sPeriodUnit, $CNF['TABLE_PRICES'], 'name');
  52. $iId = (int)$oForm->insert(array('name' => $sName, 'order' => $this->_oModule->_oDb->getPriceOrderMax($this->_iLevelId) + 1));
  53. if($iId != 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(array('Purchasable' => 'yes'), array('ID' => $this->_iLevelId));
  56. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  57. }
  58. else
  59. $aRes = array('msg' => _t('_bx_acl_err_cannot_perform'));
  60. echoJson($aRes);
  61. return;
  62. }
  63. bx_import('BxTemplStudioFunctions');
  64. $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(
  65. 'form_id' => $oForm->aFormAttrs['id'],
  66. 'form' => $oForm->getCode(true),
  67. 'object' => $this->_sObject,
  68. 'action' => $sAction
  69. )));
  70. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  71. }
  72. public function performActionEdit()
  73. {
  74. $CNF = &$this->_oModule->_oConfig->CNF;
  75. $sAction = 'edit';
  76. $aIds = bx_get('ids');
  77. if(!$aIds || !is_array($aIds)) {
  78. $iId = (int)bx_get('id');
  79. if(!$iId) {
  80. echoJson(array());
  81. exit;
  82. }
  83. $aIds = array($iId);
  84. }
  85. $iId = $aIds[0];
  86. $aItem = $this->_oModule->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  87. if(!is_array($aItem) || empty($aItem)) {
  88. echoJson(array());
  89. exit;
  90. }
  91. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PRICE'], $CNF['OBJECT_FORM_PRICE_DISPLAY_EDIT']);
  92. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&level=' . $this->_iLevelId;
  93. $oForm->initChecker($aItem);
  94. if($oForm->isSubmittedAndValid()) {
  95. if($oForm->update($aItem['id']) !== false)
  96. $aRes = array('grid' => $this->getCode(false), 'blink' => $aItem['id']);
  97. else
  98. $aRes = array('msg' => _t('_bx_acl_err_cannot_perform'));
  99. echoJson($aRes);
  100. return;
  101. }
  102. bx_import('BxTemplStudioFunctions');
  103. $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(
  104. 'form_id' => $oForm->aFormAttrs['id'],
  105. 'form' => $oForm->getCode(true),
  106. 'object' => $this->_sObject,
  107. 'action' => $sAction
  108. )));
  109. $aRes = array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  110. echoJson($aRes);
  111. }
  112. public function performActionDelete()
  113. {
  114. $CNF = &$this->_oModule->_oConfig->CNF;
  115. $aIds = bx_get('ids');
  116. if(!$aIds || !is_array($aIds)) {
  117. echoJson(array());
  118. return;
  119. }
  120. $iAffected = 0;
  121. $aIdsAffected = array();
  122. foreach($aIds as $iId)
  123. if($this->_oModule->_oDb->deletePrices(array('id' => $iId))) {
  124. $aIdsAffected[] = $iId;
  125. $iAffected++;
  126. }
  127. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_bx_acl_err_cannot_perform')));
  128. }
  129. protected function _addJsCss()
  130. {
  131. parent::_addJsCss();
  132. $this->_oModule->_oTemplate->addStudioJs(array('jquery.form.min.js', 'administration.js'));
  133. $this->_oModule->_oTemplate->addStudioCss(array('administration.css'));
  134. $oForm = new BxTemplFormView(array());
  135. $oForm->addCssJs();
  136. }
  137. protected function _getCellPeriodUnit($mixedValue, $sKey, $aField, $aRow)
  138. {
  139. $mixedValue = _t('_bx_acl_pre_values_' . $mixedValue);
  140. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  141. }
  142. protected function _getCellPrice($mixedValue, $sKey, $aField, $aRow)
  143. {
  144. $aCurrency = $this->_oModule->_oConfig->getCurrency();
  145. return parent::_getCellDefault($aCurrency['sign'] . $mixedValue, $sKey, $aField, $aRow);
  146. }
  147. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  148. {
  149. if(empty($this->_iLevelId))
  150. $isDisabled = true;
  151. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  152. }
  153. protected function _getFilterControls()
  154. {
  155. parent::_getFilterControls();
  156. $sContent = '';
  157. $oForm = new BxTemplFormView(array());
  158. $aInputLevels = array(
  159. 'type' => 'select',
  160. 'name' => 'level',
  161. 'attrs' => array(
  162. 'id' => 'bx-grid-level-' . $this->_sObject,
  163. 'onChange' => 'javascript:' . $this->_oModule->_oConfig->getJsObject('administration') . '.onChangeLevel()'
  164. ),
  165. 'value' => $this->_iLevelId,
  166. 'values' => array(0 => _t('_bx_acl_txt_select_level'))
  167. );
  168. $aLevels = $this->_oModule->_oDb->getLevels(array('type' => 'for_selector'));
  169. foreach($aLevels as $iId => $sTitle)
  170. $aInputLevels['values'][$iId] = _t($sTitle);
  171. $sContent .= $oForm->genRow($aInputLevels);
  172. $aInputSearch = array(
  173. 'type' => 'text',
  174. 'name' => 'keyword',
  175. 'attrs' => array(
  176. 'id' => 'bx-grid-search-' . $this->_sObject,
  177. ),
  178. 'tr_attrs' => array(
  179. 'style' => 'display:none;'
  180. )
  181. );
  182. $sContent .= $oForm->genRow($aInputSearch);
  183. return $sContent;
  184. }
  185. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  186. {
  187. if(empty($this->_iLevelId) && strpos($sFilter, $this->_sParamsDivider) !== false)
  188. list($this->_iLevelId, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  189. if(empty($this->_iLevelId))
  190. return array();
  191. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `level_id`=? ", $this->_iLevelId);
  192. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  193. }
  194. }
  195. /** @} */