Brand::defineProperties()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace VojtaSvoboda\Brands\Components;
2
3
use Cms\Classes\ComponentBase;
4
use Cms\Classes\Page;
5
use VojtaSvoboda\Brands\Models\Brand as Model;
6
7
class Brand extends ComponentBase
8
{
9
    /** @var Model $item */
10
    public $item;
11
12
    /** @var string $categoryPage Reference to the page name for linking to categories. */
13
    public $categoryPage;
14
15
    public function componentDetails()
16
    {
17
        return [
18
            'name' => 'Brand detail',
19
            'description' => 'Show brand detail',
20
        ];
21
    }
22
23
    public function defineProperties()
24
    {
25
        return [
26
            'slug' => [
27
                'title' => 'Brand slug',
28
                'description' => 'Slug for display one particular brand',
29
                'type' => 'string',
30
                'default' => '{{ :slug }}',
31
            ],
32
            'categoryPage' => [
33
                'title' => 'Category page',
34
                'description' => 'Page for showing brand category',
35
                'type' => 'dropdown',
36
                'default' => 'brands',
37
            ],
38
        ];
39
    }
40
41
    public function onRun()
42
    {
43
        $this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage');
44
        $this->item = $this->page['item'] = $this->getItem();
45
    }
46
47
    protected function getItem()
48
    {
49
        $slug = $this->property('slug');
50
        $item = Model::where('slug', $slug)->isEnabled()->first();
51
        $categoryPage = $this->categoryPage;
52
53
        if ($item) {
54
            $item->categories->each(function($category) use ($categoryPage) {
55
                $category->url = $this->controller->pageUrl($categoryPage, [
56
                    'category' => $category->slug,
57
                ]);
58
            });
59
        }
60
61
        return $item;
62
    }
63
64
    /**
65
     * Get options for the dropdown whre the link to the category page can be selected.
66
     *
67
     * @return mixed
68
     */
69
    public function getCategoryPageOptions()
70
    {
71
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
72
    }
73
}
74