BxBaseStudioPermissionsLevels.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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('BxDolStudioPermissionsLevels');
  12. bx_import('BxTemplStudioFormView');
  13. class BxBaseStudioPermissionsLevels extends BxDolStudioPermissionsLevels {
  14. public static $iBinMB = 1048576;
  15. function __construct($aOptions, $oTemplate = false) {
  16. parent::__construct($aOptions, $oTemplate);
  17. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_prm_btn_level_edit');
  18. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_prm_btn_level_delete');
  19. }
  20. public function performActionAdd() {
  21. $sAction = 'add';
  22. $aForm = array(
  23. 'form_attrs' => array(
  24. 'id' => 'adm-prm-level-create',
  25. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  26. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT,
  27. 'enctype' => 'multipart/form-data',
  28. ),
  29. 'params' => array (
  30. 'db' => array(
  31. 'table' => 'sys_acl_levels',
  32. 'key' => 'ID',
  33. 'uri' => '',
  34. 'uri_title' => '',
  35. 'submit_name' => 'do_submit'
  36. ),
  37. ),
  38. 'inputs' => array (
  39. 'Active' => array(
  40. 'type' => 'hidden',
  41. 'name' => 'Active',
  42. 'value' => 'yes',
  43. 'db' => array (
  44. 'pass' => 'Xss',
  45. ),
  46. ),
  47. 'Purchasable' => array(
  48. 'type' => 'hidden',
  49. 'name' => 'Purchasable',
  50. 'value' => 'no',
  51. 'db' => array (
  52. 'pass' => 'Xss',
  53. ),
  54. ),
  55. 'Removable' => array(
  56. 'type' => 'hidden',
  57. 'name' => 'Removable',
  58. 'value' => 'yes',
  59. 'db' => array (
  60. 'pass' => 'Xss',
  61. ),
  62. ),
  63. 'Name' => array(
  64. 'type' => 'text_translatable',
  65. 'name' => 'Name',
  66. 'caption' => _t('_adm_prm_txt_level_name'),
  67. 'info' => _t('_adm_prm_dsc_level_name'),
  68. 'value' => '_adm_prm_txt_level',
  69. 'required' => '1',
  70. 'db' => array (
  71. 'pass' => 'Xss',
  72. ),
  73. 'checker' => array (
  74. 'func' => 'LengthTranslatable',
  75. 'params' => array(3, 100, 'Name'),
  76. 'error' => _t('_adm_prm_err_level_name'),
  77. ),
  78. ),
  79. 'Description' => array(
  80. 'type' => 'textarea_translatable',
  81. 'name' => 'Description',
  82. 'caption' => _t('_adm_prm_txt_level_description'),
  83. 'info' => _t('_adm_prm_dsc_level_description'),
  84. 'value' => '_adm_prm_txt_level',
  85. 'db' => array (
  86. 'pass' => 'XssHtml',
  87. )
  88. ),
  89. 'QuotaSize' => array(
  90. 'type' => 'text',
  91. 'name' => 'QuotaSize',
  92. 'caption' => _t('_adm_prm_txt_level_quota_size'),
  93. 'info' => _t('_adm_prm_dsc_level_quota_size'),
  94. 'value' => '0',
  95. 'required' => '1',
  96. 'db' => array (
  97. 'pass' => 'Float',
  98. ),
  99. 'checker' => array (
  100. 'func' => 'preg',
  101. 'params' => array('/^[0-9\.]+$/'),
  102. 'error' => _t('_adm_prm_err_level_quota_size'),
  103. ),
  104. ),
  105. 'QuotaMaxFileSize' => array(
  106. 'type' => 'text',
  107. 'name' => 'QuotaMaxFileSize',
  108. 'caption' => _t('_adm_prm_txt_level_quota_max_file_size'),
  109. 'info' => _t('_adm_prm_dsc_level_quota_max_file_size'),
  110. 'value' => '',
  111. 'required' => '1',
  112. 'db' => array (
  113. 'pass' => 'Float',
  114. ),
  115. 'checker' => array (
  116. 'func' => 'preg',
  117. 'params' => array('/^[0-9\.]+$/'),
  118. 'error' => _t('_adm_prm_err_level_quota_max_file_size'),
  119. ),
  120. ),
  121. 'QuotaNumber' => array(
  122. 'type' => 'text',
  123. 'name' => 'QuotaNumber',
  124. 'caption' => _t('_adm_prm_txt_level_quota_number'),
  125. 'info' => _t('_adm_prm_dsc_level_quota_number'),
  126. 'value' => '0',
  127. 'required' => '1',
  128. 'db' => array (
  129. 'pass' => 'Int',
  130. ),
  131. 'checker' => array (
  132. 'func' => 'preg',
  133. 'params' => array('/^[0-9]+$/'),
  134. 'error' => _t('_adm_prm_err_level_quota_number'),
  135. ),
  136. ),
  137. 'Icon' => array(
  138. 'type' => 'file',
  139. 'name' => 'Icon',
  140. 'caption' => _t('_adm_prm_txt_level_icon'),
  141. 'value' => '',
  142. 'checker' => array (
  143. 'func' => '',
  144. 'params' => '',
  145. 'error' => _t('_adm_prm_err_level_icon'),
  146. ),
  147. ),
  148. 'controls' => array(
  149. 'name' => 'controls',
  150. 'type' => 'input_set',
  151. array(
  152. 'type' => 'submit',
  153. 'name' => 'do_submit',
  154. 'value' => _t('_adm_prm_btn_level_add'),
  155. ),
  156. array (
  157. 'type' => 'reset',
  158. 'name' => 'close',
  159. 'value' => _t('_adm_prm_btn_level_cancel'),
  160. 'attrs' => array(
  161. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  162. 'class' => 'bx-def-margin-sec-left',
  163. ),
  164. )
  165. )
  166. )
  167. );
  168. $oForm = new BxTemplStudioFormView($aForm);
  169. $oForm->initChecker();
  170. if($oForm->isSubmittedAndValid()) {
  171. if(($iId = $this->getAvailableId()) === false) {
  172. $this->_echoResultJson(array('msg' => _t('_adm_prm_err_level_id')), true);
  173. return;
  174. }
  175. $mixedIcon = 'acl-unconfirmed.png';
  176. if(!empty($_FILES['Icon']['tmp_name'])) {
  177. bx_import('BxDolStorage');
  178. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  179. $mixedIcon = $oStorage->storeFileFromForm($_FILES['Icon'], false, 0);
  180. if($mixedIcon === false) {
  181. $this->_echoResultJson(array('msg' => _t('_adm_prm_err_level_icon') . $oStorage->getErrorString()), true);
  182. return;
  183. }
  184. $oStorage->afterUploadCleanup($mixedIcon, 0);
  185. }
  186. $fQuotaSize = round($oForm->getCleanValue('QuotaSize'), 1);
  187. BxDolForm::setSubmittedValue('QuotaSize', self::$iBinMB * $fQuotaSize, $aForm['form_attrs']['method']);
  188. $fQuotaMaxFileSize = round($oForm->getCleanValue('QuotaMaxFileSize'), 1);
  189. BxDolForm::setSubmittedValue('QuotaMaxFileSize', self::$iBinMB * $fQuotaMaxFileSize, $aForm['form_attrs']['method']);
  190. $iId = (int)$oForm->insert(array('ID' => $iId, 'Icon' => $mixedIcon, 'Order' => $this->oDb->getLevelOrderMax() + 1));
  191. if($iId != 0)
  192. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  193. else
  194. $aRes = array('msg' => _t('_adm_prm_err_level_create'));
  195. $this->_echoResultJson($aRes, true);
  196. }
  197. else {
  198. bx_import('BxTemplStudioFunctions');
  199. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-prm-level-create-popup', _t('_adm_prm_txt_level_create_popup'), $this->_oTemplate->parseHtmlByName('prm_add_level.html', array(
  200. 'form_id' => $aForm['form_attrs']['id'],
  201. 'form' => $oForm->getCode(true),
  202. 'object' => $this->_sObject,
  203. 'action' => $sAction
  204. )));
  205. $this->_echoResultJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))), true);
  206. }
  207. }
  208. public function performActionEdit() {
  209. $sAction = 'edit';
  210. $aIds = bx_get('ids');
  211. if(!$aIds || !is_array($aIds)) {
  212. $iId = (int)bx_get('id');
  213. if(!$iId) {
  214. $this->_echoResultJson(array());
  215. exit;
  216. }
  217. $aIds = array($iId);
  218. }
  219. $iId = $aIds[0];
  220. $aLevel = array();
  221. $iLevel = $this->oDb->getLevels(array('type' => 'by_id', 'value' => $iId), $aLevel);
  222. if($iLevel != 1 || empty($aLevel)){
  223. $this->_echoResultJson(array());
  224. exit;
  225. }
  226. $aForm = array(
  227. 'form_attrs' => array(
  228. 'id' => 'adm-prm-level-edit',
  229. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  230. 'method' => 'post',
  231. 'enctype' => 'multipart/form-data',
  232. ),
  233. 'params' => array (
  234. 'db' => array(
  235. 'table' => 'sys_acl_levels',
  236. 'key' => 'ID',
  237. 'uri' => '',
  238. 'uri_title' => '',
  239. 'submit_name' => 'do_submit'
  240. ),
  241. ),
  242. 'inputs' => array (
  243. 'id' => array(
  244. 'type' => 'hidden',
  245. 'name' => 'id',
  246. 'value' => $iId,
  247. 'db' => array (
  248. 'pass' => 'Int',
  249. ),
  250. ),
  251. 'Description' => array(
  252. 'type' => 'textarea_translatable',
  253. 'name' => 'Description',
  254. 'caption' => _t('_adm_prm_txt_level_description'),
  255. 'info' => _t('_adm_prm_dsc_level_description'),
  256. 'value' => $aLevel['description'],
  257. 'db' => array (
  258. 'pass' => 'XssHtml',
  259. ),
  260. ),
  261. 'QuotaSize' => array(
  262. 'type' => 'text',
  263. 'name' => 'QuotaSize',
  264. 'caption' => _t('_adm_prm_txt_level_quota_size'),
  265. 'info' => _t('_adm_prm_dsc_level_quota_size'),
  266. 'value' => round($aLevel['quota_size'] / self::$iBinMB, 1),
  267. 'required' => '1',
  268. 'db' => array (
  269. 'pass' => 'Float',
  270. ),
  271. 'checker' => array (
  272. 'func' => 'preg',
  273. 'params' => array('/^[0-9\.]+$/'),
  274. 'error' => _t('_adm_prm_err_level_quota_size'),
  275. ),
  276. ),
  277. 'QuotaMaxFileSize' => array(
  278. 'type' => 'text',
  279. 'name' => 'QuotaMaxFileSize',
  280. 'caption' => _t('_adm_prm_txt_level_quota_max_file_size'),
  281. 'info' => _t('_adm_prm_dsc_level_quota_max_file_size'),
  282. 'value' => round($aLevel['quota_max_file_size'] / self::$iBinMB, 1),
  283. 'required' => '1',
  284. 'db' => array (
  285. 'pass' => 'Float',
  286. ),
  287. 'checker' => array (
  288. 'func' => 'preg',
  289. 'params' => array('/^[0-9\.]+$/'),
  290. 'error' => _t('_adm_prm_err_level_quota_max_file_size'),
  291. ),
  292. ),
  293. 'QuotaNumber' => array(
  294. 'type' => 'text',
  295. 'name' => 'QuotaNumber',
  296. 'caption' => _t('_adm_prm_txt_level_quota_number'),
  297. 'info' => _t('_adm_prm_dsc_level_quota_number'),
  298. 'value' => $aLevel['quota_number'],
  299. 'required' => '1',
  300. 'db' => array (
  301. 'pass' => 'Int',
  302. ),
  303. 'checker' => array (
  304. 'func' => 'preg',
  305. 'params' => array('/^[0-9]+$/'),
  306. 'error' => _t('_adm_prm_err_level_quota_number'),
  307. ),
  308. ),
  309. 'Icon' => array(
  310. 'type' => 'file',
  311. 'name' => 'Icon',
  312. 'caption' => _t('_adm_prm_txt_level_icon'),
  313. 'value' => '',
  314. 'checker' => array (
  315. 'func' => '',
  316. 'params' => '',
  317. 'error' => _t('_adm_prm_err_level_icon'),
  318. ),
  319. ),
  320. 'controls' => array(
  321. 'name' => 'controls',
  322. 'type' => 'input_set',
  323. array(
  324. 'type' => 'submit',
  325. 'name' => 'do_submit',
  326. 'value' => _t('_adm_prm_btn_level_save'),
  327. ),
  328. array (
  329. 'type' => 'reset',
  330. 'name' => 'close',
  331. 'value' => _t('_adm_prm_btn_level_cancel'),
  332. 'attrs' => array(
  333. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  334. 'class' => 'bx-def-margin-sec-left',
  335. ),
  336. )
  337. )
  338. )
  339. );
  340. bx_import('BxTemplStudioFormView');
  341. $oForm = new BxTemplStudioFormView($aForm);
  342. $oForm->initChecker();
  343. if($oForm->isSubmittedAndValid()) {
  344. $mixedIcon = 0;
  345. if(!empty($_FILES['Icon']['tmp_name'])) {
  346. bx_import('BxDolStorage');
  347. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  348. if(is_numeric($aLevel['icon']) && (int)$aLevel['icon'] != 0 && !$oStorage->deleteFile((int)$aLevel['icon'], 0)) {
  349. $this->_echoResultJson(array('msg' => _t('_adm_prm_err_level_icon_remove')), true);
  350. return;
  351. }
  352. $mixedIcon = $oStorage->storeFileFromForm($_FILES['Icon'], false, 0);
  353. if($mixedIcon === false) {
  354. $this->_echoResultJson(array('msg' => _t('_adm_prm_err_level_icon') . $oStorage->getErrorString()), true);
  355. return;
  356. }
  357. $oStorage->afterUploadCleanup($mixedIcon, 0);
  358. }
  359. $fQuotaSize = round($oForm->getCleanValue('QuotaSize'), 1);
  360. BxDolForm::setSubmittedValue('QuotaSize', self::$iBinMB * $fQuotaSize, $aForm['form_attrs']['method']);
  361. $fQuotaMaxFileSize = round($oForm->getCleanValue('QuotaMaxFileSize'), 1);
  362. BxDolForm::setSubmittedValue('QuotaMaxFileSize', self::$iBinMB * $fQuotaMaxFileSize, $aForm['form_attrs']['method']);
  363. if($oForm->update($iId, (int)$mixedIcon != 0 ? array('Icon' => $mixedIcon) : array()) !== false)
  364. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  365. else
  366. $aRes = array('msg' => _t('_adm_prm_err_level_edit'));
  367. $this->_echoResultJson($aRes, true);
  368. }
  369. else {
  370. bx_import('BxTemplStudioFunctions');
  371. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-prm-level-edit-popup', _t('_adm_prm_txt_level_edit_popup', _t($aLevel['name'])), $this->_oTemplate->parseHtmlByName('prm_add_level.html', array(
  372. 'form_id' => $aForm['form_attrs']['id'],
  373. 'form' => $oForm->getCode(true),
  374. 'object' => $this->_sObject,
  375. 'action' => $sAction
  376. )));
  377. $this->_echoResultJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))), true);
  378. }
  379. }
  380. public function performActionDelete() {
  381. $iAffected = 0;
  382. $aIds = bx_get('ids');
  383. if(!$aIds || !is_array($aIds)) {
  384. $this->_echoResultJson(array());
  385. exit;
  386. }
  387. $aIdsAffected = array ();
  388. foreach($aIds as $iId) {
  389. if(!$this->delete($iId))
  390. continue;
  391. $aIdsAffected[] = $iId;
  392. $iAffected++;
  393. }
  394. $this->_echoResultJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_prm_err_level_delete')));
  395. }
  396. protected function _addJsCss() {
  397. parent::_addJsCss();
  398. $this->_oTemplate->addJs(array('jquery.form.js'));
  399. bx_import('BxTemplStudioFormView');
  400. $oForm = new BxTemplStudioFormView(array());
  401. $oForm->addCssJs();
  402. }
  403. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow) {
  404. bx_import('BxDolAcl');
  405. if(in_array($aRow['ID'], array(MEMBERSHIP_ID_NON_MEMBER, MEMBERSHIP_ID_STANDARD, MEMBERSHIP_ID_UNCONFIRMED, MEMBERSHIP_ID_PENDING, MEMBERSHIP_ID_SUSPENDED)))
  406. return parent::_getCellDefault('', $sKey, $aField, $aRow);;
  407. return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
  408. }
  409. protected function _getCellIcon ($mixedValue, $sKey, $aField, $aRow) {
  410. $mixedValue = $this->_oTemplate->getImage($mixedValue, array('class' => 'bx-prm-level-icon bx-def-border'));
  411. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  412. }
  413. protected function _getCellActionsList ($mixedValue, $sKey, $aField, $aRow) {
  414. $aActions = array();
  415. $iActions = $this->oDb->getActions(array('type' => 'by_level_id', 'value' => $aRow['ID']), $aActions);
  416. $mixedValue = $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  417. 'href' => BX_DOL_URL_STUDIO . 'builder_permissions.php?page=actions&level=' . $aRow['ID'],
  418. 'title' => _t('_adm_prm_txt_manage_actions'),
  419. 'bx_repeat:attrs' => array(),
  420. 'content' => _t('_adm_prm_txt_n_actions', $iActions)
  421. ));
  422. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  423. }
  424. protected function _getCellQuotaSize ($mixedValue, $sKey, $aField, $aRow) {
  425. return parent::_getCellDefault ($mixedValue > 0 ? _t_format_size($mixedValue) : '&infin;', $sKey, $aField, $aRow);
  426. }
  427. protected function _getCellQuotaNumber ($mixedValue, $sKey, $aField, $aRow) {
  428. return parent::_getCellDefault ($mixedValue > 0 ? $mixedValue : '&infin;', $sKey, $aField, $aRow);
  429. }
  430. protected function _getCellQuotaMaxFileSize ($mixedValue, $sKey, $aField, $aRow) {
  431. return parent::_getCellDefault ($mixedValue > 0 ? _t_format_size($mixedValue) : '&infin;', $sKey, $aField, $aRow);
  432. }
  433. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  434. if ($sType == 'single' && $aRow['Removable'] != 'yes')
  435. return '';
  436. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  437. }
  438. protected function getAvailableId() {
  439. $aLevels = array();
  440. $this->oDb->getLevels(array('type' =>'all_order_id'), $aLevels, false);
  441. $iId = 1;
  442. foreach($aLevels as $aLevel) {
  443. if($iId != (int)$aLevel['id'])
  444. break;
  445. $iId++;
  446. }
  447. return $iId <= BX_DOL_STUDIO_PERMISSIONS_LEVEL_ID_INT_MAX ? $iId : false;
  448. }
  449. }
  450. /** @} */