-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAreaController.php
118 lines (99 loc) · 2.67 KB
/
AreaController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Traits\ResourceController;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use App\Models\Area;
use Illuminate\Support\Facades\Response;
class AreaController extends Controller
{
use ResourceController;
/**
* 资源模型
* @var string
*/
protected $resourceModel = 'Area';
//默认排序
protected $orderDefault = [
'left_margin'=>'asc'
];
//条件筛选
protected $sizer=[
'parent_id'=>'=',
'level'=>'=',
'name'=>'like'
];
/**
* 编辑页面显示字段
* @var array
*/
public $editFields = [
'parent' => ['name', 'id']
];
/**
* Index页面字段名称显示
* @var array
*/
public $showIndexFields = [
'parent' => ['name', 'id']
];
/**
* excel导出数据查询字段
* @var array
*/
public $exportFields = [
'parent' => ['name', 'id']
];
//字段导出
public $exportFieldsName = [
'name' => 'Name',
'parent.name' => 'Parent name',
'parent_id' => 'Parent ID',
'id'=>'ID',
];
/**
* 获取条件拼接对象
* @return mixed
*/
protected function getWithOptionModel($fields_key = 'showIndexFields',$unset_order=false)
{
$this->bindModel OR $this->bindModel();
$options = $this->getOptions(); //筛选项+排序项
if($unset_order){
unset($options['order']);
}
$obj = $this->bindModel->with($this->selectWithFields($fields_key))
->withCount(collect($this->getShowIndexFieldsCount())->filter(function ($item, $key) {
return !is_array($item);
})->toArray())
->options($options);
if($optional_parent_id = intval(Request::input('optional_parent_id'))){
$obj = $obj->optionalParent($optional_parent_id ? Area::find($optional_parent_id) : null);
}
return $obj;
}
/**
* 验证规则
* @return array
*/
protected function getValidateRule(){
$validate = [
'name'=>'required',
'parent_id'=>'sometimes|required|exists:areas,id'
];
if(!Area::where('id',1)->value('id') || Request::input('id')==1){
unset($validate['parent_id']);
}
return $validate;
}
/**
* 编辑页面数据返回处理
* @param $id
* @param $data
* @return mixed
*/
protected function handleEditReturn($id,&$data){
$data['no_root'] = !Area::where('id',1)->value('id') || $id==1;
return $data;
}
}