BxBaseStudioPermissionsPrices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  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 DolphinView Dolphin Studio Representation classes
  7. * @ingroup DolphinStudio
  8. * @{
  9. */
  10. defined('BX_DOL') or die('hack attempt');
  11. bx_import('BxDolStudioPermissionsPrices');
  12. bx_import('BxTemplStudioFormView');
  13. class BxBaseStudioPermissionsPrices extends BxDolStudioPermissionsPrices {
  14. function __construct($aOptions, $oTemplate = false) {
  15. parent::__construct($aOptions, $oTemplate);
  16. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_prm_btn_price_edit');
  17. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_prm_btn_price_delete');
  18. }
  19. public function performActionAdd() {
  20. $sAction = 'add';
  21. $sFilter = bx_get('filter');
  22. if(strpos($sFilter, $this->sParamsDivider) !== false)
  23. list($this->iLevel, $sFilter) = explode($this->sParamsDivider, $sFilter);
  24. if((int)$this->iLevel == 0)
  25. $this->iLevel = (int)bx_get('IDLevel');
  26. $aForm = array(
  27. 'form_attrs' => array(
  28. 'id' => 'adm-prm-price-add',
  29. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  30. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  31. ),
  32. 'params' => array (
  33. 'db' => array(
  34. 'table' => 'sys_acl_level_prices',
  35. 'key' => 'id',
  36. 'uri' => '',
  37. 'uri_title' => '',
  38. 'submit_name' => 'do_submit'
  39. ),
  40. ),
  41. 'inputs' => array (
  42. 'IDLevel' => array(
  43. 'type' => 'hidden',
  44. 'name' => 'IDLevel',
  45. 'value' => $this->iLevel,
  46. 'db' => array (
  47. 'pass' => 'Int',
  48. ),
  49. ),
  50. 'Days' => array(
  51. 'type' => 'text',
  52. 'name' => 'Days',
  53. 'caption' => _t('_adm_prm_txt_price_add_days'),
  54. 'info' => _t('_adm_prm_dsc_price_add_days'),
  55. 'value' => '',
  56. 'db' => array (
  57. 'pass' => 'Int',
  58. ),
  59. ),
  60. 'Price' => array(
  61. 'type' => 'text',
  62. 'name' => 'Price',
  63. 'caption' => _t('_adm_prm_txt_price_add_price'),
  64. 'info' => '',
  65. 'value' => '',
  66. 'db' => array (
  67. 'pass' => 'Float',
  68. ),
  69. ),
  70. 'controls' => array(
  71. 'name' => 'controls',
  72. 'type' => 'input_set',
  73. array(
  74. 'type' => 'submit',
  75. 'name' => 'do_submit',
  76. 'value' => _t('_adm_prm_btn_price_add'),
  77. ),
  78. array (
  79. 'type' => 'reset',
  80. 'name' => 'close',
  81. 'value' => _t('_adm_prm_btn_price_cancel'),
  82. 'attrs' => array(
  83. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  84. 'class' => 'bx-def-margin-sec-left',
  85. ),
  86. )
  87. )
  88. )
  89. );
  90. $oForm = new BxTemplStudioFormView($aForm);
  91. $oForm->initChecker();
  92. if($oForm->isSubmittedAndValid()) {
  93. $aPrice = array();
  94. $iPrice = $this->oDb->getPrices(array('type' => 'by_level_id_duration', 'level_id' => $oForm->getCleanValue('IDLevel'), 'days' => $oForm->getCleanValue('Days')), $aPrice);
  95. if($iPrice != 0) {
  96. $this->_echoResultJson(array('msg' => _t('_adm_prm_err_price_duplicate')), true);
  97. return;
  98. }
  99. $iId = (int)$oForm->insert(array('Order' => $this->oDb->getPriceOrderMax($this->iLevel) + 1));
  100. if($iId != 0) {
  101. $this->oDb->updateLevels($this->iLevel, array('Purchasable' => 'yes'));
  102. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  103. }
  104. else
  105. $aRes = array('msg' => _t('_adm_prm_err_price_create'));
  106. $this->_echoResultJson($aRes, true);
  107. }
  108. else {
  109. bx_import('BxTemplStudioFunctions');
  110. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-prm-price-add-popup', _t('_adm_prm_txt_price_add_popup'), $this->_oTemplate->parseHtmlByName('prm_add_price.html', array(
  111. 'form_id' => $aForm['form_attrs']['id'],
  112. 'form' => $oForm->getCode(true),
  113. 'object' => $this->_sObject,
  114. 'action' => $sAction
  115. )));
  116. $this->_echoResultJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))), true);
  117. }
  118. }
  119. public function performActionEdit() {
  120. $sAction = 'edit';
  121. $aIds = bx_get('ids');
  122. if(!$aIds || !is_array($aIds)) {
  123. $iId = (int)bx_get('id');
  124. if(!$iId) {
  125. $this->_echoResultJson(array());
  126. exit;
  127. }
  128. $aIds = array($iId);
  129. }
  130. $iId = $aIds[0];
  131. $aPrice = array();
  132. $iPrice = $this->oDb->getPrices(array('type' => 'by_id', 'value' => $iId), $aPrice);
  133. if($iPrice != 1 || empty($aPrice)) {
  134. $this->_echoResultJson(array());
  135. exit;
  136. }
  137. if((int)$this->iLevel == 0)
  138. $this->iLevel = (int)$aPrice['level_id'];
  139. $aForm = array(
  140. 'form_attrs' => array(
  141. 'id' => 'adm-prm-price-edit',
  142. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  143. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  144. ),
  145. 'params' => array (
  146. 'db' => array(
  147. 'table' => 'sys_acl_level_prices',
  148. 'key' => 'id',
  149. 'uri' => '',
  150. 'uri_title' => '',
  151. 'submit_name' => 'do_submit'
  152. ),
  153. ),
  154. 'inputs' => array (
  155. 'id' => array(
  156. 'type' => 'hidden',
  157. 'name' => 'id',
  158. 'value' => $iId,
  159. 'db' => array (
  160. 'pass' => 'Int',
  161. ),
  162. ),
  163. 'IDLevel' => array(
  164. 'type' => 'hidden',
  165. 'name' => 'IDLevel',
  166. 'value' => $aPrice['level_id'],
  167. 'db' => array (
  168. 'pass' => 'Int',
  169. ),
  170. ),
  171. 'Days' => array(
  172. 'type' => 'text',
  173. 'name' => 'Days',
  174. 'caption' => _t('_adm_prm_txt_price_add_days'),
  175. 'info' => _t('_adm_prm_dsc_price_add_days'),
  176. 'value' => $aPrice['days'],
  177. 'db' => array (
  178. 'pass' => 'Int',
  179. ),
  180. ),
  181. 'Price' => array(
  182. 'type' => 'text',
  183. 'name' => 'Price',
  184. 'caption' => _t('_adm_prm_txt_price_add_price'),
  185. 'info' => '',
  186. 'value' => round($aPrice['price'], 2),
  187. 'db' => array (
  188. 'pass' => 'Float',
  189. ),
  190. ),
  191. 'controls' => array(
  192. 'name' => 'controls',
  193. 'type' => 'input_set',
  194. array(
  195. 'type' => 'submit',
  196. 'name' => 'do_submit',
  197. 'value' => _t('_adm_prm_btn_price_save'),
  198. ),
  199. array (
  200. 'type' => 'reset',
  201. 'name' => 'close',
  202. 'value' => _t('_adm_prm_btn_price_cancel'),
  203. 'attrs' => array(
  204. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  205. 'class' => 'bx-def-margin-sec-left',
  206. ),
  207. )
  208. )
  209. )
  210. );
  211. $oForm = new BxTemplStudioFormView($aForm);
  212. $oForm->initChecker();
  213. if($oForm->isSubmittedAndValid()) {
  214. $iId = $oForm->getCleanValue('id');
  215. $aPrice = array();
  216. $iPrice = $this->oDb->getPrices(array('type' => 'by_level_id_duration', 'level_id' => $oForm->getCleanValue('IDLevel'), 'days' => $oForm->getCleanValue('Days')), $aPrice);
  217. if($iPrice != 0 && !empty($aPrice) && $aPrice['id'] != $iId) {
  218. $this->_echoResultJson(array('msg' => _t('_adm_prm_err_price_duplicate')), true);
  219. return;
  220. }
  221. if((int)$oForm->update($iId) > 0)
  222. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  223. else
  224. $aRes = array('msg' => _t('_adm_prm_err_price_update'));
  225. $this->_echoResultJson($aRes, true);
  226. }
  227. else {
  228. bx_import('BxTemplStudioFunctions');
  229. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-prm-price-edit-popup', _t('_adm_prm_txt_price_edit_popup', $aPrice['days']), $this->_oTemplate->parseHtmlByName('prm_add_price.html', array(
  230. 'form_id' => $aForm['form_attrs']['id'],
  231. 'form' => $oForm->getCode(true),
  232. 'object' => $this->_sObject,
  233. 'action' => $sAction
  234. )));
  235. $this->_echoResultJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))), true);
  236. }
  237. }
  238. function performActionDelete() {
  239. $aIds = bx_get('ids');
  240. if (!$aIds || !is_array($aIds)) {
  241. $this->_echoResultJson(array());
  242. exit;
  243. }
  244. $aPrice = array();
  245. $this->oDb->getPrices(array('type' => 'by_id', 'value' => $aIds[0]), $aPrice, false);
  246. parent::performActionDelete();
  247. $aPrices = array();
  248. $iPrices = $this->oDb->getPrices(array('type' => 'by_level_id', 'value' => $aPrice['level_id']), $aPrices);
  249. if($iPrices == 0)
  250. $this->oDb->updateLevels($this->iLevel, array('Purchasable' => 'no'));
  251. }
  252. function getJsObject() {
  253. return 'oBxDolStudioPermissionsPrices';
  254. }
  255. function getCode($isDisplayHeader = true) {
  256. return $this->_oTemplate->parseHtmlByName('prm_prices.html', array(
  257. 'content' => parent::getCode($isDisplayHeader),
  258. 'js_object' => $this->getJsObject(),
  259. 'grid_object' => $this->_sObject,
  260. 'params_divider' => $this->sParamsDivider
  261. ));
  262. }
  263. protected function _addJsCss() {
  264. parent::_addJsCss();
  265. $this->_oTemplate->addJs(array('jquery.form.min.js', 'permissions_prices.js'));
  266. bx_import('BxTemplStudioFormView');
  267. $oForm = new BxTemplStudioFormView(array());
  268. $oForm->addCssJs();
  269. }
  270. protected function _getCellPrice ($mixedValue, $sKey, $aField, $aRow) {
  271. return parent::_getCellDefault (_t_format_currency($mixedValue), $sKey, $aField, $aRow);
  272. }
  273. protected function _getCellDays ($mixedValue, $sKey, $aField, $aRow) {
  274. return parent::_getCellDefault (_t('_sys_x_days', $mixedValue), $sKey, $aField, $aRow);
  275. }
  276. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  277. if($this->iLevel == 0)
  278. $isDisabled = true;
  279. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  280. }
  281. protected function _getFilterControls () {
  282. parent::_getFilterControls();
  283. $sContent = "";
  284. bx_import('BxTemplStudioFormView');
  285. $oForm = new BxTemplStudioFormView(array());
  286. $aInputLevels = array(
  287. 'type' => 'select',
  288. 'name' => 'level',
  289. 'attrs' => array(
  290. 'id' => 'bx-grid-level-' . $this->_sObject,
  291. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeLevel()'
  292. ),
  293. 'value' => 'id-' . $this->iLevel,
  294. 'values' => array()
  295. );
  296. $aLevels = $aCounter = array();
  297. $this->oDb->getLevels(array('type' => 'all'), $aLevels, false);
  298. $this->oDb->getPrices(array('type' => 'counter_by_levels'), $aCounter, false);
  299. foreach($aLevels as $aLevel)
  300. $aInputLevels['values']['id-' . $aLevel['id']] = _t($aLevel['name']) . " (" . (isset($aCounter[$aLevel['id']]) ? $aCounter[$aLevel['id']] : "0") . ")";
  301. asort($aInputLevels['values']);
  302. $aInputLevels['values'] = array_merge(array('id-0' => _t('_adm_prm_txt_select_level')), $aInputLevels['values']);
  303. $sContent .= $oForm->genRow($aInputLevels);
  304. if($this->iLevel == 0)
  305. return $sContent;
  306. $aInputSearch = array(
  307. 'type' => 'text',
  308. 'name' => 'keyword',
  309. 'attrs' => array(
  310. 'id' => 'bx-grid-search-' . $this->_sObject
  311. )
  312. );
  313. $sContent .= $oForm->genRow($aInputSearch);
  314. return $sContent;
  315. }
  316. }
  317. /** @} */