|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Facades\Admin as AdminManager; |
|
6
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
|
|
10
|
|
|
class Tree implements Renderable |
|
11
|
|
|
{ |
|
12
|
|
|
protected $items = []; |
|
13
|
|
|
|
|
14
|
|
|
protected $script; |
|
15
|
|
|
|
|
16
|
|
|
protected $elementId = 'tree-'; |
|
17
|
|
|
|
|
18
|
|
|
protected $model; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Model $model = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->model = $model; |
|
23
|
|
|
|
|
24
|
|
|
$this->path = app('router')->current()->getPath(); |
|
|
|
|
|
|
25
|
|
|
$this->elementId .= uniqid(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Variables in tree template. |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function variables() |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
'items' => $this->model->toTree(), |
|
37
|
|
|
'id' => $this->elementId, |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Build tree grid scripts. |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function buildupScript() |
|
47
|
|
|
{ |
|
48
|
|
|
$confirm = trans('admin::lang.delete_confirm'); |
|
49
|
|
|
$token = csrf_token(); |
|
50
|
|
|
|
|
51
|
|
|
$this->script = <<<SCRIPT |
|
52
|
|
|
|
|
53
|
|
|
$('#{$this->elementId}').nestable({}); |
|
54
|
|
|
|
|
55
|
|
|
$('._delete').click(function() { |
|
56
|
|
|
var id = $(this).data('id'); |
|
57
|
|
|
if(confirm("{$confirm}")) { |
|
58
|
|
|
$.post('/{$this->path}/' + id, {_method:'delete','_token':'{$token}'}, function(data){ |
|
59
|
|
|
$.pjax.reload('#pjax-container'); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
$('.{$this->elementId}-save').click(function () { |
|
65
|
|
|
var serialize = $('#{$this->elementId}').nestable('serialize'); |
|
66
|
|
|
$.get('/{$this->path}', {'_tree':JSON.stringify(serialize)}, function(data){ |
|
67
|
|
|
$.pjax.reload('#pjax-container'); |
|
68
|
|
|
}); |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
$('.{$this->elementId}-refresh').click(function () { |
|
72
|
|
|
$.pjax.reload('#pjax-container'); |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
SCRIPT; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Render a tree. |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Illuminate\Http\JsonResponse|string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function render() |
|
85
|
|
|
{ |
|
86
|
|
|
if (Request::capture()->has('_tree')) { |
|
87
|
|
|
return response()->json([ |
|
88
|
|
|
'status' => $this->saveTree(Request::capture()->get('_tree')), |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->buildupScript(); |
|
93
|
|
|
|
|
94
|
|
|
AdminManager::script($this->script); |
|
95
|
|
|
|
|
96
|
|
|
view()->share(['path' => $this->path]); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return view('admin::tree', $this->variables())->render(); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Build menu tree presented by array. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $serialize |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function saveTree($serialize) |
|
109
|
|
|
{ |
|
110
|
|
|
$tree = json_decode($serialize, true); |
|
111
|
|
|
|
|
112
|
|
|
if (json_last_error() != JSON_ERROR_NONE) { |
|
113
|
|
|
throw new \InvalidArgumentException(json_last_error_msg()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->model->saveTree($tree); |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get the string contents of the grid view. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __toString() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->render(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: