BxBaseStudioPermissionsLevels.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. $aForm = array(
  263. 'form_attrs' => array(
  264. 'id' => 'adm-prm-level-create',
  265. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  266. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT,
  267. 'enctype' => 'multipart/form-data',
  268. ),
  269. 'params' => array (
  270. 'db' => array(
  271. 'table' => 'sys_acl_levels',
  272. 'key' => 'ID',
  273. 'uri' => '',
  274. 'uri_title' => '',
  275. 'submit_name' => 'do_submit'
  276. ),
  277. ),
  278. 'inputs' => array (
  279. 'id' => array(
  280. 'type' => 'hidden',
  281. 'name' => 'id',
  282. 'value' => isset($aLevel['id']) ? (int)$aLevel['id'] : 0,
  283. 'db' => array (
  284. 'pass' => 'Int',
  285. ),
  286. ),
  287. 'Active' => array(
  288. 'type' => 'hidden',
  289. 'name' => 'Active',
  290. 'value' => 'yes',
  291. 'db' => array (
  292. 'pass' => 'Xss',
  293. ),
  294. ),
  295. 'Purchasable' => array(
  296. 'type' => 'hidden',
  297. 'name' => 'Purchasable',
  298. 'value' => 'no',
  299. 'db' => array (
  300. 'pass' => 'Xss',
  301. ),
  302. ),
  303. 'Removable' => array(
  304. 'type' => 'hidden',
  305. 'name' => 'Removable',
  306. 'value' => 'yes',
  307. 'db' => array (
  308. 'pass' => 'Xss',
  309. ),
  310. ),
  311. 'Name' => array(
  312. 'type' => 'text_translatable',
  313. 'name' => 'Name',
  314. 'caption' => _t('_adm_prm_txt_level_name'),
  315. 'info' => _t('_adm_prm_dsc_level_name'),
  316. 'value' => '_adm_prm_txt_level',
  317. 'required' => '1',
  318. 'db' => array (
  319. 'pass' => 'Xss',
  320. ),
  321. 'checker' => array (
  322. 'func' => 'LengthTranslatable',
  323. 'params' => array(3, 100, 'Name'),
  324. 'error' => _t('_adm_prm_err_level_name'),
  325. ),
  326. ),
  327. 'Description' => array(
  328. 'type' => 'textarea_translatable',
  329. 'name' => 'Description',
  330. 'caption' => _t('_adm_prm_txt_level_description'),
  331. 'info' => _t('_adm_prm_dsc_level_description'),
  332. 'value' => isset($aLevel['description']) ? $aLevel['description'] : '_adm_prm_txt_level',
  333. 'db' => array (
  334. 'pass' => 'XssHtml',
  335. )
  336. ),
  337. 'QuotaSize' => array(
  338. 'type' => 'text',
  339. 'name' => 'QuotaSize',
  340. 'caption' => _t('_adm_prm_txt_level_quota_size'),
  341. 'info' => _t('_adm_prm_dsc_level_quota_size'),
  342. 'value' => isset($aLevel['quota_size']) ? round($aLevel['quota_size'] / self::$iBinMB, 1) : '0',
  343. 'required' => '1',
  344. 'db' => array (
  345. 'pass' => 'Float',
  346. ),
  347. 'checker' => array (
  348. 'func' => 'preg',
  349. 'params' => array('/^[0-9\.]+$/'),
  350. 'error' => _t('_adm_prm_err_level_quota_size'),
  351. ),
  352. ),
  353. 'QuotaMaxFileSize' => array(
  354. 'type' => 'text',
  355. 'name' => 'QuotaMaxFileSize',
  356. 'caption' => _t('_adm_prm_txt_level_quota_max_file_size'),
  357. 'info' => _t('_adm_prm_dsc_level_quota_max_file_size'),
  358. 'value' => isset($aLevel['quota_max_file_size']) ? round($aLevel['quota_max_file_size'] / self::$iBinMB, 1) : '',
  359. 'required' => '1',
  360. 'db' => array (
  361. 'pass' => 'Float',
  362. ),
  363. 'checker' => array (
  364. 'func' => 'preg',
  365. 'params' => array('/^[0-9\.]+$/'),
  366. 'error' => _t('_adm_prm_err_level_quota_max_file_size'),
  367. ),
  368. ),
  369. 'QuotaNumber' => array(
  370. 'type' => 'text',
  371. 'name' => 'QuotaNumber',
  372. 'caption' => _t('_adm_prm_txt_level_quota_number'),
  373. 'info' => _t('_adm_prm_dsc_level_quota_number'),
  374. 'value' => isset($aLevel['quota_number']) ? (int)$aLevel['quota_number'] : '0',
  375. 'required' => '1',
  376. 'db' => array (
  377. 'pass' => 'Int',
  378. ),
  379. 'checker' => array (
  380. 'func' => 'preg',
  381. 'params' => array('/^[0-9]+$/'),
  382. 'error' => _t('_adm_prm_err_level_quota_number'),
  383. ),
  384. ),
  385. 'Icon' => array(
  386. 'type' => 'text',
  387. 'name' => 'Icon',
  388. 'caption' => _t('_adm_prm_txt_level_icon'),
  389. 'info' => _t('_adm_prm_dsc_level_icon'),
  390. 'value' => '',
  391. 'required' => '0',
  392. 'db' => array (
  393. 'pass' => 'Xss',
  394. ),
  395. 'checker' => array (
  396. 'func' => '',
  397. 'params' => array(),
  398. 'error' => _t('_adm_prm_err_level_icon'),
  399. ),
  400. ),
  401. 'Icon_image' => array(
  402. 'type' => 'file',
  403. 'name' => 'Icon_image',
  404. 'caption' => _t('_adm_prm_txt_level_icon_image'),
  405. 'info' => _t('_adm_prm_dsc_level_icon_image'),
  406. 'value' => '',
  407. 'checker' => array (
  408. 'func' => '',
  409. 'params' => '',
  410. 'error' => _t('_adm_prm_err_level_icon_image'),
  411. ),
  412. ),
  413. 'Icon_preview' => array(
  414. 'type' => 'custom',
  415. 'name' => 'Icon_preview',
  416. 'caption' => _t('_adm_prm_txt_level_icon_image_old'),
  417. 'content' => ''
  418. ),
  419. 'controls' => array(
  420. 'name' => 'controls',
  421. 'type' => 'input_set',
  422. array(
  423. 'type' => 'submit',
  424. 'name' => 'do_submit',
  425. 'value' => _t('_adm_prm_btn_level_add'),
  426. ),
  427. array (
  428. 'type' => 'reset',
  429. 'name' => 'close',
  430. 'value' => _t('_adm_prm_btn_level_cancel'),
  431. 'attrs' => array(
  432. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  433. 'class' => 'bx-def-margin-sec-left',
  434. ),
  435. )
  436. )
  437. )
  438. );
  439. switch($sAction) {
  440. case 'add':
  441. unset($aForm['inputs']['id']);
  442. unset($aForm['inputs']['Icon_preview']);
  443. $aForm['form_attrs']['id'] .= 'create';
  444. break;
  445. case 'edit':
  446. unset($aForm['inputs']['Active']);
  447. unset($aForm['inputs']['Purchasable']);
  448. unset($aForm['inputs']['Removable']);
  449. unset($aForm['inputs']['Name']);
  450. $aForm['form_attrs']['id'] .= 'edit';
  451. $aForm['inputs']['Icon_image']['caption'] = _t('_adm_prm_txt_level_icon_image_new');
  452. $aForm['inputs']['controls'][0]['value'] = _t('_adm_prm_btn_level_save');
  453. $sIconImage = $sIconFont = "";
  454. if(!empty($aLevel['icon'])) {
  455. if(is_numeric($aLevel['icon']) && (int)$aLevel['icon'] != 0) {
  456. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  457. $sIconImage = $oStorage->getFileUrlById((int)$aLevel['icon']);
  458. } else {
  459. $sIconFont = $aLevel['icon'];
  460. $aForm['inputs']['Icon']['value'] = $sIconFont;
  461. }
  462. }
  463. $aForm['inputs']['Icon_preview']['content'] = $this->_getIconPreview($aLevel['id'], $sIconImage, $sIconFont);
  464. break;
  465. }
  466. return new BxTemplStudioFormView($aForm);
  467. }
  468. protected function _getAvailableId()
  469. {
  470. $aLevels = array();
  471. $this->oDb->getLevels(array('type' =>'all_order_id'), $aLevels, false);
  472. $iId = 1;
  473. foreach($aLevels as $aLevel) {
  474. if($iId != (int)$aLevel['id'])
  475. break;
  476. $iId++;
  477. }
  478. return $iId <= BX_DOL_STUDIO_PERMISSIONS_LEVEL_ID_INT_MAX ? $iId : false;
  479. }
  480. }
  481. /** @} */