BxBaseStudioPermissionsLevels.php 22 KB

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