Completed
Push — master ( eb9a96...174aff )
by Gino
01:30
created

TagPosts::prepareVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Rainlab\Blog\Models\Post;
6
use GinoPane\BlogTaxonomy\Plugin;
7
use GinoPane\BlogTaxonomy\Models\Tag;
8
use GinoPane\BlogTaxonomy\Classes\PostListAbstract;
9
10
/**
11
 * Class TagPosts
12
 *
13
 * @package GinoPane\BlogTaxonomy\Components
14
 */
15
class TagPosts extends PostListAbstract
16
{
17
    const NAME = 'postsWithTag';
18
19
    /**
20
     * @var Tag
21
     */
22
    public $tag;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $includeSeriesPosts = false;
28
29
    /**
30
     * Component Registration
31
     * @return array
32
     */
33
    public function componentDetails(): array
34
    {
35
        return [
36
            'name'        => Plugin::LOCALIZATION_KEY . 'components.tag_posts.name',
37
            'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.description'
38
        ];
39
    }
40
41
    /**
42
     * Component Properties
43
     * @return array
44
     */
45
    public function defineProperties(): array
46
    {
47
        return [
48
            'tag' => [
49
                'title'         => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_title',
50
                'description'   => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_description',
51
                'default'       => '{{ :tag }}',
52
                'type'          => 'string'
53
            ],
54
            'includeSeriesPosts' => [
55
                'title'         => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_title',
56
                'description'   => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_description',
57
                'default'       => false,
58
                'type'          => 'checkbox',
59
                'showExternalParam' => false
60
            ]
61
        ] + parent::defineProperties();
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    protected function prepareVars()
68
    {
69
        parent::prepareVars();
70
71
        $this->includeSeriesPosts = $this->property('includeSeriesPosts', false);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    protected function prepareContextItem()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        // load tag
80
        $this->tag = Tag::whereTranslatable('slug', $this->property('tag'))->first();
81
82
        return $this->tag;
83
    }
84
85
    /**
86
     * @return mixed
87
     */
88
    protected function getPostsQuery()
89
    {
90
        $query = Post::whereHas('tags', function ($query) {
91
            $query->whereTranslatable('slug', $this->tag->slug);
92
        });
93
94
        if ($this->includeSeriesPosts) {
95
            $query->orWhereHas('series', function ($query) {
96
                $query->whereHas('tags', function ($query) {
97
                    $query->whereTranslatable('slug', $this->tag->slug);
98
                });
99
            });
100
        }
101
102
        $query->isPublished();
103
104
        return $query;
105
    }
106
}
107