ResourceController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\modules\admin\models\Resource;
  5. use app\modules\admin\models\RoleResource;
  6. use app\common\components\FormElements;
  7. use Yii;
  8. class ResourceController extends BController
  9. {
  10. public $layout = 'main';
  11. public function actionList()
  12. {
  13. if (Yii::$app->request->isAjax) {
  14. $data = [];
  15. $resultList = Resource::find()->orderBy(['parent_hash'=>SORT_ASC,'list_order'=>SORT_ASC])->all();
  16. foreach($resultList as $resource)
  17. {
  18. $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);
  19. }
  20. echo_json($data);
  21. }
  22. $this->tableTitle = array(
  23. array('field'=>'hash','checkbox'=>false,'formatter'=>'checkboxFormatter'),
  24. array('field'=>'name','title'=>Yii::t('attr','resource.zh-name'),'align'=>'left'),
  25. array('field'=>'en_name','title'=>Resource::getAttributeName('name'),'align'=>'center'),
  26. array('field'=>'url','title'=>Resource::getAttributeName('url'),'align'=>'center'),
  27. array('field'=>'block','title'=>Resource::getAttributeName('block'),'sortable'=>true,'align'=>'center'),
  28. array('field'=>'disabled','title'=>Resource::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter'),
  29. array('field'=>'list_order','title'=>Resource::getAttributeName('list_order'),'align'=>'center','formatter'=>'editFormatter'),
  30. array('field'=>'operate','title'=>Resource::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter'),
  31. );
  32. $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);
  33. return $this->render('list',array());
  34. }
  35. function actionAdd()
  36. {
  37. $model = new Resource();
  38. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  39. $model->hash = md5($model->name.$model->module.$model->controller.$model->action.$model->data.$model->url);
  40. $exist = Resource::find()->where("hash='".$model->hash."'")->exists();
  41. if($exist)
  42. {
  43. $msgdata = ['error' => 1,'msg' => '已存在相同配置菜单,添加失败'];
  44. echo_json($msgdata);
  45. }
  46. if(!$model->validate())
  47. {
  48. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  49. }
  50. else
  51. {
  52. if($model->save())
  53. {
  54. //更新语言表数据
  55. $this->updateLang($model->name,safe_replace($_POST['zh-name']),'resource');
  56. $this->updateMessageFile('resource');
  57. $msgdata = ['error' => 0,'msg' => '成功'];
  58. }
  59. else
  60. {
  61. $msgdata = ['error' => 1,'msg' => '失败'];
  62. }
  63. }
  64. echo_json($msgdata);
  65. }
  66. $resourceList = Resource::find()->where("disabled=0")->All();
  67. $datas = $this->serializeTreeData($resourceList,'hash','parent_hash');
  68. $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\'');
  69. return $this->renderAjax('add',array('model'=>$model,'parentStr'=>$parentStr));
  70. }
  71. function actionEdit()
  72. {
  73. $hash = $this->getKeyId('hash');
  74. $model = Resource::findOne($hash);
  75. check_record_exists($model);
  76. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  77. $post = Yii::$app->request->post();
  78. $model->hash = md5($model->name.$model->module.$model->controller.$model->action.$model->data.$model->url);
  79. $model->is_system = $post['Resource']['is_system']?$post['Resource']['is_system']:0;
  80. $model->pop_large = $post['Resource']['pop_large']?$post['Resource']['pop_large']:0;
  81. $model->disabled = $post['Resource']['disabled']?$post['Resource']['disabled']:0;
  82. Resource::updateAll(['parent_hash' => $model->hash], ['parent_hash'=>$hash]);
  83. if(!$model->validate())
  84. {
  85. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  86. }
  87. else
  88. {
  89. if($model->save())
  90. {
  91. //更新语言表数据
  92. $this->updateLang($model->name,safe_replace($_POST['zh-name']),'resource');
  93. $this->updateMessageFile('resource');
  94. $msgdata = ['error' => 0,'msg' => '成功'];
  95. }
  96. else
  97. {
  98. $msgdata = ['error' => 1,'msg' => '失败'];
  99. }
  100. }
  101. echo_json($msgdata);
  102. }
  103. $resourceList = Resource::find()->where("disabled=0")->All();
  104. $datas = $this->serializeTreeData($resourceList,'hash','parent_hash');
  105. $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\'');
  106. return $this->renderAjax('add',array('model'=>$model,'parentStr'=>$parentStr));
  107. }
  108. //删除
  109. public function actionDel()
  110. {
  111. $hash = $this->getKeyId('hash');
  112. $model = Resource::findOne($hash);
  113. check_record_exists($model);
  114. $count = Resource::find()->where("parent_hash='".$model->hash."'")->count();
  115. if($count>0)
  116. {
  117. $msgdata = ['error' => 1,'msg' => '操作失败,请先删除下级菜单!'];
  118. }
  119. else
  120. {
  121. if($model->delete())
  122. {
  123. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  124. }
  125. else
  126. {
  127. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  128. }
  129. }
  130. echo_json($msgdata);
  131. }
  132. }