/
Menu.php
73 lines (49 loc) · 1.82 KB
/
Menu.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
<?php namespace Model;
use DB;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Request;
class Menu extends Model
{
use SoftDeletes, Traits\Tree, Traits\Act;
protected $table = 'menu';
protected $visible = array('id', 'parent', 'name', 'preview', 'path', 'autopath');
public function active($parent = false)
{
$path = substr(Request::fullUrl(), strlen(url('/')) + 1);
if ($parent) {
$p = explode('/', $path);
return !empty($p[0]) && $p[0] == $this->path;
}
return $this->path == $path;
}
public function site()
{
return $this->belongsTo('Model\Site', 'element_id');
}
public function link()
{
return url('/').'/'.$this->path;
}
public function save(array $options = array())
{
if (empty($this->id)) {
$max_sort = DB::table($this->table)
->where('parent', !empty($this->parent) ? $this->parent : 0)
->where('deleted_at', null)
->max('sort');
$this->sort = is_numeric($max_sort) ? $max_sort + 1 : 0;
}
if (!empty($this->element_id)) {
if (empty($this->module)) {
$this->element_id = 0;
} else {
$module = 'Model\\'.ucfirst($this->module);
$element = $module::find($this->element_id);
if (!empty($element->id)) {
$this->path = $element->path;
}
}
}
return parent::save($options);
}
}