123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace app\modules\admin\controllers;
- use app\common\controllers\BController;
- use app\modules\admin\models\Resource;
- use app\modules\admin\models\RoleResource;
- use app\common\components\FormElements;
- use Yii;
- class ResourceController extends BController
- {
- public $layout = 'main';
- public function actionList()
- {
- if (Yii::$app->request->isAjax) {
- $data = [];
- $resultList = Resource::find()->orderBy(['parent_hash'=>SORT_ASC,'list_order'=>SORT_ASC])->all();
- foreach($resultList as $resource)
- {
- $data[] = array('hash'=>$resource->hash,'parent_hash'=>$resource->parent_hash,'name'=>Yii::t('resource',$resource->name),'en_name'=>$resource->name,'url'=>$resource->action?$resource->module.'/'.$resource->controller.'/'.$resource->action:'','menu_type'=>Resource::menutypeOptions($resource->menu_type),'pop_large'=>$resource->pop_large,'icon_class'=>$resource->icon_class, 'btn_class'=>$resource->btn_class, 'list_order'=>$resource->list_order,'disabledTxt'=>Resource::disableOptions($resource->disabled),'disabled'=>$resource->disabled,'block'=>$this->resourceBlock[$resource->block],'is_system'=>$resource->is_system);
- }
- echo_json($data);
- }
- $this->tableTitle = array(
- array('field'=>'hash','checkbox'=>false,'formatter'=>'checkboxFormatter'),
- array('field'=>'name','title'=>Yii::t('attr','resource.zh-name'),'align'=>'left'),
- array('field'=>'en_name','title'=>Resource::getAttributeName('name'),'align'=>'center'),
- array('field'=>'url','title'=>Resource::getAttributeName('url'),'align'=>'center'),
- array('field'=>'block','title'=>Resource::getAttributeName('block'),'sortable'=>true,'align'=>'center'),
- array('field'=>'disabled','title'=>Resource::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter'),
- array('field'=>'list_order','title'=>Resource::getAttributeName('list_order'),'align'=>'center','formatter'=>'editFormatter'),
- array('field'=>'operate','title'=>Resource::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter'),
- );
-
- $this->tableConfig = array('table'=>Resource::shortTableName(),'url'=>$this->createRealUrl(['admin/resource/list']),'setFieldUrl'=>$this->createRealUrl(['admin/resource/setfield']),'idField'=>Resource::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'tree'=>1,'parentIdField'=>'parent_hash','treeShowField'=>'name','expand'=>true,'refresh'=>true);
- return $this->render('list',array());
- }
- function actionAdd()
- {
- $model = new Resource();
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- $model->hash = md5($model->name.$model->module.$model->controller.$model->action.$model->data.$model->url);
- $exist = Resource::find()->where("hash='".$model->hash."'")->exists();
- if($exist)
- {
- $msgdata = ['error' => 1,'msg' => '已存在相同配置菜单,添加失败'];
- echo_json($msgdata);
- }
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- //更新语言表数据
- $this->updateLang($model->name,safe_replace($_POST['zh-name']),'resource');
- $this->updateMessageFile('resource');
- $msgdata = ['error' => 0,'msg' => '成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '失败'];
- }
- }
- echo_json($msgdata);
- }
- $resourceList = Resource::find()->where("disabled=0")->All();
- $datas = $this->serializeTreeData($resourceList,'hash','parent_hash');
- $parentStr = FormElements::getInstCls()->tree_select($datas,'',$model->parent_hash,'Resource[parent_hash]',Yii::t('admin','as first level resource'),'',' class=\'form-select \' data-dselect-clearable=\'true\'');
- return $this->renderAjax('add',array('model'=>$model,'parentStr'=>$parentStr));
- }
- function actionEdit()
- {
- $hash = $this->getKeyId('hash');
- $model = Resource::findOne($hash);
- check_record_exists($model);
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- $post = Yii::$app->request->post();
- $model->hash = md5($model->name.$model->module.$model->controller.$model->action.$model->data.$model->url);
- $model->is_system = $post['Resource']['is_system']?$post['Resource']['is_system']:0;
- $model->pop_large = $post['Resource']['pop_large']?$post['Resource']['pop_large']:0;
- $model->disabled = $post['Resource']['disabled']?$post['Resource']['disabled']:0;
- Resource::updateAll(['parent_hash' => $model->hash], ['parent_hash'=>$hash]);
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- //更新语言表数据
- $this->updateLang($model->name,safe_replace($_POST['zh-name']),'resource');
- $this->updateMessageFile('resource');
- $msgdata = ['error' => 0,'msg' => '成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '失败'];
- }
- }
- echo_json($msgdata);
- }
- $resourceList = Resource::find()->where("disabled=0")->All();
- $datas = $this->serializeTreeData($resourceList,'hash','parent_hash');
- $parentStr = FormElements::getInstCls()->tree_select($datas,'',$model->parent_hash,'Resource[parent_hash]',Yii::t('admin','as first level resource'),'',' class=\'form-select \' data-dselect-clearable=\'true\'');
- return $this->renderAjax('add',array('model'=>$model,'parentStr'=>$parentStr));
- }
- //删除
- public function actionDel()
- {
- $hash = $this->getKeyId('hash');
- $model = Resource::findOne($hash);
- check_record_exists($model);
- $count = Resource::find()->where("parent_hash='".$model->hash."'")->count();
- if($count>0)
- {
- $msgdata = ['error' => 1,'msg' => '操作失败,请先删除下级菜单!'];
- }
- else
- {
- if($model->delete())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功!'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败!'];
- }
- }
- echo_json($msgdata);
- }
- }
|