Completed
Push — master ( 7a1e6c...8794c3 )
by Arjay
11:28
created

Category::lookup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Entities;
4
5
use Yajra\CMS\Contracts\UrlGenerator;
6
use Yajra\CMS\Entities\Traits\CanRequireAuthentication;
7
use Yajra\CMS\Entities\Traits\HasParameters;
8
use Yajra\CMS\Entities\Traits\PublishableTrait;
9
use Yajra\CMS\Presenters\CategoryPresenter;
10
use Baum\Node;
11
use Laracasts\Presenter\PresentableTrait;
12
use Spatie\Sluggable\HasSlug;
13
use Spatie\Sluggable\SlugOptions;
14
use Yajra\Auditable\AuditableTrait;
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 array
61
     */
62
    public static function lists()
63
    {
64
        $root  = static::root();
65
        $items = [];
66
67
        foreach ($root->descendants()->orderBy('lft')->get() as $category) {
68
            $items[] = [
69
                'title' => $category->present()->indentedTitle(),
70
                'id'    => $category->id,
71
            ];
72
        }
73
74
        return array_pluck($items, 'title', 'id');
75
    }
76
77
    /**
78
     * Get list of possible parent node.
79
     *
80
     * @return array
81
     */
82
    public function getParentsList()
83
    {
84
        /** @var static $root */
85
        $root  = static::root();
86
        $items = [
87
            ['id' => '1', 'title' => 'Item Root'],
88
        ];
89
90
        if ($this->exists) {
91
            $parents = $this->ancestors()
92
                            ->withoutRoot()
93
                            ->orWhere(function ($query) {
94
                                $query->where('depth', $this->depth)->where('id', '<>', $this->id);
95
                            })
96
                            ->get();
97
            foreach ($parents as $parent) {
98
                $items[] = [
99
                    'title' => $parent->present()->indentedTitle(),
100
                    'id'    => $parent->id,
101
                ];
102
            }
103
        } else {
104
            foreach ($root->getDescendants() as $parent) {
105
                $items[] = [
106
                    'title' => $parent->present()->indentedTitle(),
107
                    'id'    => $parent->id,
108
                ];
109
            }
110
        }
111
112
        return array_pluck($items, 'title', 'id');
113
    }
114
115
    /**
116
     * Count published articles.
117
     *
118
     * @return int
119
     */
120
    public function countPublished()
121
    {
122
        return $this->articles()->published()->count();
123
    }
124
125
    /**
126
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
127
     */
128
    public function articles()
129
    {
130
        return $this->hasMany(Article::class);
131
    }
132
133
    /**
134
     * Count unpublished articles.
135
     *
136
     * @return int
137
     */
138
    public function countUnpublished()
139
    {
140
        return $this->articles()->unpublished()->count();
141
    }
142
143
    /**
144
     * Get the options for generating the slug.
145
     */
146
    public function getSlugOptions() : SlugOptions
147
    {
148
        return SlugOptions::create()
149
                          ->generateSlugsFrom('title')
150
                          ->saveSlugsTo('alias');
151
    }
152
153
    /**
154
     * Get url from implementing class.
155
     *
156
     * @param mixed $args
157
     * @return string
158
     */
159
    public function getUrl($args)
160
    {
161
        return 'category/' . $this->alias . '/' . $args;
162
    }
163
}
164