| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaView UNA Studio Representation classes
- * @ingroup UnaStudio
- * @{
- */
- class BxBaseStudioPermissionsLevels extends BxDolStudioPermissionsLevels
- {
- public static $iBinMB = 1048576;
- protected $sUrlPage;
- function __construct($aOptions, $oTemplate = false)
- {
- parent::__construct($aOptions, $oTemplate);
- $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_prm_btn_level_edit');
- $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_prm_btn_level_delete');
- $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_permissions.php?page=levels';
- }
- public function performActionAdd()
- {
- $sAction = 'add';
- $oForm = $this->_getFormObject($sAction);
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- if(($iId = $this->_getAvailableId()) === false) {
- echoJson(array('msg' => _t('_adm_prm_err_level_id')));
- return;
- }
- $mixedIcon = '';
- if(!empty($_FILES['Icon_image']['tmp_name'])) {
- $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
- $mixedIcon = $oStorage->storeFileFromForm($_FILES['Icon_image'], false, 0);
- if($mixedIcon === false) {
- echoJson(array('msg' => _t('_adm_prm_err_level_icon_image') . $oStorage->getErrorString()));
- return;
- }
- $oStorage->afterUploadCleanup($mixedIcon, 0);
- }
- if(empty($mixedIcon))
- $mixedIcon = $oForm->getCleanValue('Icon');
- if(empty($mixedIcon))
- $mixedIcon = 'acl-unconfirmed.png';
- BxDolForm::setSubmittedValue('Icon', $mixedIcon, $oForm->aFormAttrs['method']);
- $fQuotaSize = round($oForm->getCleanValue('QuotaSize'), 1);
- BxDolForm::setSubmittedValue('QuotaSize', self::$iBinMB * $fQuotaSize, $oForm->aFormAttrs['method']);
- $fQuotaMaxFileSize = round($oForm->getCleanValue('QuotaMaxFileSize'), 1);
- BxDolForm::setSubmittedValue('QuotaMaxFileSize', self::$iBinMB * $fQuotaMaxFileSize, $oForm->aFormAttrs['method']);
- $iId = (int)$oForm->insert(array('ID' => $iId, 'Icon' => $mixedIcon, 'Order' => $this->oDb->getLevelOrderMax() + 1));
- if($iId != 0) {
- $aLevel = array();
- $this->oDb->getLevels(array('type' => 'by_id', 'value' => (int)$iId), $aLevel, false);
- $iActionsFrom = (int)$oForm->getCleanValue('Actions');
- if($iActionsFrom > 0) {
- $aActions = array();
- $this->oDb->getActions(array('type' => 'by_level_id', 'value' => $iActionsFrom), $aActions, false);
- foreach($aActions as $aAction)
- $this->oDb->switchAction($iId, $aAction['id'], true, array(
- 'AllowedCount' => $aAction['allowed_count'],
- 'AllowedPeriodLen' => $aAction['allowed_period_len'],
- 'AllowedPeriodStart' => $aAction['allowed_period_start'],
- 'AllowedPeriodEnd' => $aAction['allowed_period_end'],
- 'AdditionalParamValue' => $aAction['additional_param_value']
- ));
- }
- // create system event
- bx_alert('acl', 'added', $iId, 0, array('level' => $aLevel));
- $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
- }
- else
- $aRes = array('msg' => _t('_adm_prm_err_level_create'));
- echoJson($aRes);
- }
- else {
- $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-prm-level-create-popup', _t('_adm_prm_txt_level_create_popup'), $this->_oTemplate->parseHtmlByName('prm_add_level.html', array(
- 'form_id' => $oForm->aFormAttrs['id'],
- 'form' => $oForm->getCode(true),
- 'object' => $this->_sObject,
- 'action' => $sAction
- )));
- echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
- }
- }
- public function performActionEdit()
- {
- $sAction = 'edit';
- $aIds = bx_get('ids');
- if(!$aIds || !is_array($aIds)) {
- $iId = (int)bx_get('id');
- if(!$iId) {
- echoJson(array());
- exit;
- }
- $aIds = array($iId);
- }
- $iId = $aIds[0];
- $aLevel = array();
- $iLevel = $this->oDb->getLevels(array('type' => 'by_id', 'value' => $iId), $aLevel);
- if($iLevel != 1 || empty($aLevel)){
- echoJson(array());
- exit;
- }
- $oForm = $this->_getFormObject($sAction, $aLevel);
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- $bIconImageCur = is_numeric($aLevel['icon']) && (int)$aLevel['icon'] != 0;
- $bIconImageNew = !empty($_FILES['Icon_image']['tmp_name']);
- $sIconFont = $oForm->getCleanValue('Icon');
- $bIconFont = !empty($sIconFont);
- if($bIconImageCur && ($bIconImageNew || $bIconFont)) {
- $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
- if(!$oStorage->deleteFile((int)$aLevel['icon'], 0)) {
- echoJson(array('msg' => _t('_adm_prm_err_level_icon_image_remove')));
- return;
- }
- }
- $sIcon = $sIconFont;
- if($bIconImageNew) {
- $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
- $sIcon = $oStorage->storeFileFromForm($_FILES['Icon_image'], false, 0);
- if($sIcon === false) {
- echoJson(array('msg' => _t('_adm_prm_err_level_icon_image') . $oStorage->getErrorString()));
- return;
- }
- $oStorage->afterUploadCleanup($sIcon, 0);
- } else if($bIconImageCur && !$bIconFont)
- $sIcon = $aLevel['icon'];
- BxDolForm::setSubmittedValue('Icon', $sIcon, $oForm->aFormAttrs['method']);
- $fQuotaSize = round($oForm->getCleanValue('QuotaSize'), 1);
- BxDolForm::setSubmittedValue('QuotaSize', self::$iBinMB * $fQuotaSize, $oForm->aFormAttrs['method']);
- $fQuotaMaxFileSize = round($oForm->getCleanValue('QuotaMaxFileSize'), 1);
- BxDolForm::setSubmittedValue('QuotaMaxFileSize', self::$iBinMB * $fQuotaMaxFileSize, $oForm->aFormAttrs['method']);
- if($oForm->update($iId) !== false) {
- $this->oDb->cleanCache('sys_acl_levels' . $iId);
- $aLevel = array();
- $this->oDb->getLevels(array('type' => 'by_id', 'value' => (int)$iId), $aLevel, false);
- // create system event
- bx_alert('acl', 'edited', $iId, 0, array('level' => $aLevel));
- $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
- }
- else
- $aRes = array('msg' => _t('_adm_prm_err_level_edit'));
- echoJson($aRes);
- }
- else {
- $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(
- 'form_id' => $oForm->aFormAttrs['id'],
- 'form' => $oForm->getCode(true),
- 'object' => $this->_sObject,
- 'action' => $sAction
- )));
- echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
- }
- }
- public function performActionDelete()
- {
- $iAffected = 0;
- $aIds = bx_get('ids');
- if(!$aIds || !is_array($aIds)) {
- echoJson(array());
- exit;
- }
- $aIdsAffected = array ();
- foreach($aIds as $iId) {
- if(!$this->delete($iId))
- continue;
- $aIdsAffected[] = $iId;
- $iAffected++;
- }
- echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_prm_err_level_delete')));
- }
- public function performActionDeleteIcon()
- {
- $sAction = 'delete_icon';
- $aIds = bx_get('ids');
- if(empty($aIds[0])) {
- echoJson(array());
- exit;
- }
- $iId = (int)$aIds[0];
- $aLevel = array();
- $iLevel = $this->oDb->getLevels(array('type' => 'by_id', 'value' => $iId), $aLevel);
- if($iLevel != 1 || empty($aLevel)){
- echoJson(array());
- exit;
- }
- if(is_numeric($aLevel['icon']) && (int)$aLevel['icon'] != 0)
- if(!BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES)->deleteFile((int)$aLevel['icon'], 0)) {
- echoJson(array());
- exit;
- }
- if($this->oDb->updateLevels($aLevel['id'], array('icon' => '')) !== false)
- echoJson(array('grid' => $this->getCode(false), 'blink' => $iId, 'preview' => $this->_getIconPreview($aLevel['id']), 'eval' => $this->getJsObject() . ".onDeleteIcon(oData)"));
- }
- public function getJsObject()
- {
- return 'oBxDolStudioPermissionsLevels';
- }
- public function getCode($isDisplayHeader = true)
- {
- return $this->_oTemplate->parseHtmlByName('prm_levels.html', array(
- 'content' => parent::getCode($isDisplayHeader),
- 'js_object' => $this->getJsObject(),
- 'page_url' => $this->sUrlPage,
- 'grid_object' => $this->_sObject,
- 'params_divider' => $this->sParamsDivider
- ));
- }
- protected function _addJsCss()
- {
- parent::_addJsCss();
- $this->_oTemplate->addJs(array('jquery.form.min.js', 'permissions_levels.js'));
- $oForm = new BxTemplStudioFormView(array());
- $oForm->addCssJs();
- }
- protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
- {
- 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)))
- return parent::_getCellDefault('', $sKey, $aField, $aRow);;
- return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
- }
- protected function _getCellIcon ($mixedValue, $sKey, $aField, $aRow)
- {
- $mixedValue = $this->_oTemplate->getImage($mixedValue, array('class' => 'bx-prm-level-icon'));
- return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
- }
- protected function _getCellActionsList ($mixedValue, $sKey, $aField, $aRow)
- {
- $aActions = array();
- $iActions = $this->oDb->getActions(array('type' => 'by_level_id', 'value' => $aRow['ID']), $aActions);
- $sLink = BX_DOL_URL_STUDIO . 'builder_permissions.php?page=actions&level=' . $aRow['ID'];
- $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_prm_txt_n_actions', $iActions), array(
- 'title' => _t('_adm_prm_txt_manage_actions')
- ));
- return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
- }
- protected function _getCellQuotaSize ($mixedValue, $sKey, $aField, $aRow)
- {
- return parent::_getCellDefault ($mixedValue > 0 ? _t_format_size($mixedValue) : '∞', $sKey, $aField, $aRow);
- }
- protected function _getCellQuotaNumber ($mixedValue, $sKey, $aField, $aRow)
- {
- return parent::_getCellDefault ($mixedValue > 0 ? $mixedValue : '∞', $sKey, $aField, $aRow);
- }
- protected function _getCellQuotaMaxFileSize ($mixedValue, $sKey, $aField, $aRow)
- {
- return parent::_getCellDefault ($mixedValue > 0 ? _t_format_size($mixedValue) : '∞', $sKey, $aField, $aRow);
- }
- protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
- {
- if ($sType == 'single' && $aRow['Removable'] != 'yes')
- return '';
- return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
- }
- protected function _getIconPreview($iId, $sIconImage = '', $sIcon = '')
- {
- $bIconImage = !empty($sIconImage);
-
- $aIcons = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
- $sIconHtml = $aIcons[2] . $aIcons[3] . $aIcons[4];
- $bIconHtml = !empty($sIconHtml) && !$bIconImage;
-
- return $this->_oTemplate->parseHtmlByName('prm_level_icon_preview.html', array(
- 'id' => $iId,
- 'bx_if:show_icon_empty' => array(
- 'condition' => !$bIconImage && !$bIconHtml,
- 'content' => array()
- ),
- 'bx_if:show_icon_image' => array(
- 'condition' => $bIconImage,
- 'content' => array(
- 'js_object' => $this->getJsObject(),
- 'url' => $sIconImage,
- 'id' => $iId
- )
- ),
- 'bx_if:show_icon_html' => array(
- 'condition' => $bIconHtml,
- 'content' => array(
- 'icon' => $sIconHtml
- )
- )
- ));
- }
- protected function _getFormObject($sAction, $aLevel = array())
- {
- bx_import('BxTemplStudioFormView');
- $aForm = array(
- 'form_attrs' => array(
- 'id' => 'adm-prm-level-create',
- 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
- 'method' => BX_DOL_STUDIO_METHOD_DEFAULT,
- 'enctype' => 'multipart/form-data',
- ),
- 'params' => array (
- 'db' => array(
- 'table' => 'sys_acl_levels',
- 'key' => 'ID',
- 'uri' => '',
- 'uri_title' => '',
- 'submit_name' => 'do_submit'
- ),
- ),
- 'inputs' => array (
- 'id' => array(
- 'type' => 'hidden',
- 'name' => 'id',
- 'value' => isset($aLevel['id']) ? (int)$aLevel['id'] : 0,
- 'db' => array (
- 'pass' => 'Int',
- ),
- ),
- 'Active' => array(
- 'type' => 'hidden',
- 'name' => 'Active',
- 'value' => 'yes',
- 'db' => array (
- 'pass' => 'Xss',
- ),
- ),
- 'Purchasable' => array(
- 'type' => 'hidden',
- 'name' => 'Purchasable',
- 'value' => 'no',
- 'db' => array (
- 'pass' => 'Xss',
- ),
- ),
- 'Removable' => array(
- 'type' => 'hidden',
- 'name' => 'Removable',
- 'value' => 'yes',
- 'db' => array (
- 'pass' => 'Xss',
- ),
- ),
- 'Name' => array(
- 'type' => 'text_translatable',
- 'name' => 'Name',
- 'caption' => _t('_adm_prm_txt_level_name'),
- 'info' => _t('_adm_prm_dsc_level_name'),
- 'value' => '_adm_prm_txt_level',
- 'required' => '1',
- 'db' => array (
- 'pass' => 'Xss',
- ),
- 'checker' => array (
- 'func' => 'LengthTranslatable',
- 'params' => array(3, 100, 'Name'),
- 'error' => _t('_adm_prm_err_level_name'),
- ),
- ),
- 'Description' => array(
- 'type' => 'textarea_translatable',
- 'name' => 'Description',
- 'caption' => _t('_adm_prm_txt_level_description'),
- 'info' => _t('_adm_prm_dsc_level_description'),
- 'value' => isset($aLevel['description']) ? $aLevel['description'] : '_adm_prm_txt_level',
- 'code' => 1,
- 'db' => array (
- 'pass' => 'XssHtml',
- )
- ),
- 'QuotaSize' => array(
- 'type' => 'text',
- 'name' => 'QuotaSize',
- 'caption' => _t('_adm_prm_txt_level_quota_size'),
- 'info' => _t('_adm_prm_dsc_level_quota_size'),
- 'value' => isset($aLevel['quota_size']) ? round($aLevel['quota_size'] / self::$iBinMB, 1) : '0',
- 'required' => '1',
- 'db' => array (
- 'pass' => 'Float',
- ),
- 'checker' => array (
- 'func' => 'preg',
- 'params' => array('/^[0-9\.]+$/'),
- 'error' => _t('_adm_prm_err_level_quota_size'),
- ),
- ),
- 'QuotaMaxFileSize' => array(
- 'type' => 'text',
- 'name' => 'QuotaMaxFileSize',
- 'caption' => _t('_adm_prm_txt_level_quota_max_file_size'),
- 'info' => _t('_adm_prm_dsc_level_quota_max_file_size'),
- 'value' => isset($aLevel['quota_max_file_size']) ? round($aLevel['quota_max_file_size'] / self::$iBinMB, 1) : '',
- 'required' => '1',
- 'db' => array (
- 'pass' => 'Float',
- ),
- 'checker' => array (
- 'func' => 'preg',
- 'params' => array('/^[0-9\.]+$/'),
- 'error' => _t('_adm_prm_err_level_quota_max_file_size'),
- ),
- ),
- 'QuotaNumber' => array(
- 'type' => 'text',
- 'name' => 'QuotaNumber',
- 'caption' => _t('_adm_prm_txt_level_quota_number'),
- 'info' => _t('_adm_prm_dsc_level_quota_number'),
- 'value' => isset($aLevel['quota_number']) ? (int)$aLevel['quota_number'] : '0',
- 'required' => '1',
- 'db' => array (
- 'pass' => 'Int',
- ),
- 'checker' => array (
- 'func' => 'preg',
- 'params' => array('/^[0-9]+$/'),
- 'error' => _t('_adm_prm_err_level_quota_number'),
- ),
- ),
- 'Icon' => array(
- 'type' => 'textarea',
- 'name' => 'Icon',
- 'caption' => _t('_adm_prm_txt_level_icon'),
- 'info' => _t('_adm_prm_dsc_level_icon'),
- 'value' => '',
- 'required' => '0',
- 'code' => 1,
- 'db' => array (
- 'pass' => 'Xss',
- ),
- 'checker' => array (
- 'func' => '',
- 'params' => array(),
- 'error' => _t('_adm_prm_err_level_icon'),
- ),
- 'attrs' => array('class' => 'bx-form-input-textarea-small'),
- ),
- 'Icon_image' => array(
- 'type' => 'file',
- 'name' => 'Icon_image',
- 'caption' => _t('_adm_prm_txt_level_icon_image'),
- 'info' => _t('_adm_prm_dsc_level_icon_image'),
- 'value' => '',
- 'checker' => array (
- 'func' => '',
- 'params' => '',
- 'error' => _t('_adm_prm_err_level_icon_image'),
- ),
- ),
- 'Icon_preview' => array(
- 'type' => 'custom',
- 'name' => 'Icon_preview',
- 'caption' => _t('_adm_prm_txt_level_icon_image_old'),
- 'content' => ''
- ),
- 'Actions' => array(
- 'type' => 'select',
- 'name' => 'Actions',
- 'caption' => _t('_adm_prm_txt_level_actions_copy'),
- 'values' => array(
- array('key' => 0, 'value' => _t('_sys_txt_empty'))
- ),
- ),
- 'controls' => array(
- 'name' => 'controls',
- 'type' => 'input_set',
- array(
- 'type' => 'submit',
- 'name' => 'do_submit',
- 'value' => _t('_adm_prm_btn_level_add'),
- ),
- array (
- 'type' => 'reset',
- 'name' => 'close',
- 'value' => _t('_adm_prm_btn_level_cancel'),
- 'attrs' => array(
- 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
- 'class' => 'bx-def-margin-sec-left',
- ),
- )
- )
- )
- );
- $aLevels = array();
- $this->oDb->getLevels(array('type' => 'all_pair'), $aLevels, false);
- foreach($aLevels as $iId => $sName)
- $aForm['inputs']['Actions']['values'][] = array('key' => $iId, 'value' => _t($sName));
- switch($sAction) {
- case 'add':
- unset($aForm['inputs']['id']);
- unset($aForm['inputs']['Icon_preview']);
- $aForm['form_attrs']['id'] .= 'create';
- break;
- case 'edit':
- unset($aForm['inputs']['Active']);
- unset($aForm['inputs']['Purchasable']);
- unset($aForm['inputs']['Removable']);
- unset($aForm['inputs']['Name']);
- unset($aForm['inputs']['Actions']);
- $aForm['form_attrs']['id'] .= 'edit';
- $aForm['inputs']['Icon_image']['caption'] = _t('_adm_prm_txt_level_icon_image_new');
- $aForm['inputs']['controls'][0]['value'] = _t('_adm_prm_btn_level_save');
- $sIconImage = $sIconFont = "";
- if(!empty($aLevel['icon'])) {
- if(is_numeric($aLevel['icon']) && (int)$aLevel['icon'] != 0) {
- $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
- $sIconImage = $oStorage->getFileUrlById((int)$aLevel['icon']);
- } else {
- $sIconFont = $aLevel['icon'];
- $aForm['inputs']['Icon']['value'] = $sIconFont;
- }
- }
- $aForm['inputs']['Icon_preview']['content'] = $this->_getIconPreview($aLevel['id'], $sIconImage, $sIconFont);
- break;
- }
- return new BxTemplStudioFormView($aForm);
- }
- protected function _getAvailableId()
- {
- $aLevels = array();
- $this->oDb->getLevels(array('type' =>'all_order_id'), $aLevels, false);
- $iId = 1;
- foreach($aLevels as $aLevel) {
- if($iId != (int)$aLevel['id'])
- break;
- $iId++;
- }
- return $iId <= BX_DOL_STUDIO_PERMISSIONS_LEVEL_ID_INT_MAX ? $iId : false;
- }
- }
- /** @} */
|