|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use Baum\Node; |
|
6
|
|
|
use Laracasts\Presenter\PresentableTrait; |
|
7
|
|
|
use Spatie\Sluggable\HasSlug; |
|
8
|
|
|
use Spatie\Sluggable\SlugOptions; |
|
9
|
|
|
use Yajra\Auditable\AuditableTrait; |
|
10
|
|
|
use Yajra\CMS\Contracts\UrlGenerator; |
|
11
|
|
|
use Yajra\CMS\Entities\Traits\CanRequireAuthentication; |
|
12
|
|
|
use Yajra\CMS\Entities\Traits\HasParameters; |
|
13
|
|
|
use Yajra\CMS\Entities\Traits\PublishableTrait; |
|
14
|
|
|
use Yajra\CMS\Presenters\CategoryPresenter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property int depth |
|
18
|
|
|
* @property string title |
|
19
|
|
|
* @property int id |
|
20
|
|
|
* @property bool published |
|
21
|
|
|
* @property bool authenticated |
|
22
|
|
|
* @property string alias |
|
23
|
|
|
* @property int hits |
|
24
|
|
|
*/ |
|
25
|
|
|
class Category extends Node implements UrlGenerator |
|
26
|
|
|
{ |
|
27
|
|
|
use AuditableTrait, PresentableTrait, PublishableTrait; |
|
28
|
|
|
use HasSlug, CanRequireAuthentication, HasParameters; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Yajra\CMS\Presenters\CategoryPresenter |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $presenter = CategoryPresenter::class; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $table = 'categories'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $fillable = [ |
|
44
|
|
|
'id', |
|
45
|
|
|
'parent_id', |
|
46
|
|
|
'published', |
|
47
|
|
|
'authenticated', |
|
48
|
|
|
'depth', |
|
49
|
|
|
'description', |
|
50
|
|
|
'title', |
|
51
|
|
|
'alias', |
|
52
|
|
|
'parameters', |
|
53
|
|
|
'created_at', |
|
54
|
|
|
'updated_at', |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get lists of categories. |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function lists() |
|
63
|
|
|
{ |
|
64
|
|
|
return static::root()->descendants()->orderBy('lft')->get(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get list of possible parent node. |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getParentsList() |
|
73
|
|
|
{ |
|
74
|
|
|
/** @var static $root */ |
|
75
|
|
|
$root = static::root(); |
|
76
|
|
|
$items = [ |
|
77
|
|
|
['id' => '1', 'title' => 'Item Root'], |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
if ($this->exists) { |
|
81
|
|
|
$parents = $this->ancestors() |
|
82
|
|
|
->withoutRoot() |
|
83
|
|
|
->orWhere(function ($query) { |
|
84
|
|
|
$query->where('depth', $this->depth)->where('id', '<>', $this->id); |
|
85
|
|
|
}) |
|
86
|
|
|
->get(); |
|
87
|
|
|
foreach ($parents as $parent) { |
|
88
|
|
|
$items[] = [ |
|
89
|
|
|
'title' => $parent->present()->indentedTitle(), |
|
90
|
|
|
'id' => $parent->id, |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
foreach ($root->getDescendants() as $parent) { |
|
95
|
|
|
$items[] = [ |
|
96
|
|
|
'title' => $parent->present()->indentedTitle(), |
|
97
|
|
|
'id' => $parent->id, |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return array_pluck($items, 'title', 'id'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Count published articles. |
|
107
|
|
|
* |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
|
|
public function countPublished() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->articles()->published()->count(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
117
|
|
|
*/ |
|
118
|
|
|
public function articles() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->hasMany(Article::class); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Count unpublished articles. |
|
125
|
|
|
* |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
|
|
public function countUnpublished() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->articles()->unpublished()->count(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the options for generating the slug. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getSlugOptions() : SlugOptions |
|
137
|
|
|
{ |
|
138
|
|
|
return SlugOptions::create() |
|
139
|
|
|
->generateSlugsFrom('title') |
|
140
|
|
|
->saveSlugsTo('alias'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get url from implementing class. |
|
145
|
|
|
* |
|
146
|
|
|
* @param mixed $layout |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getUrl($layout = null) |
|
150
|
|
|
{ |
|
151
|
|
|
$layout = $layout ? '?layout=' . $layout : ''; |
|
152
|
|
|
|
|
153
|
|
|
return url($this->present()->alias) . $layout; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get category's route name. |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getRouteName() |
|
162
|
|
|
{ |
|
163
|
|
|
return implode('.', explode('/', $this->present()->alias)); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|