Completed
Push — master ( a449a7...7f0061 )
by Gino
12:11
created

Series::scopeListFrontend()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Models;
4
5
use Model;
6
use Cms\Classes\Controller;
7
use RainLab\Blog\Models\Post;
8
use GinoPane\BlogTaxonomy\Plugin;
9
use Illuminate\Support\Facades\DB;
10
use October\Rain\Database\Traits\Sluggable;
11
use October\Rain\Database\Traits\Validation;
12
13
/**
14
 * Class Series
15
 *
16
 * @package GinoPane\BlogTaxonomy\Models
17
 */
18
class Series extends Model
19
{
20
    use Validation;
21
    use Sluggable;
22
    use PostsRelationScopeTrait;
23
24
    const TABLE_NAME = 'ginopane_blogtaxonomy_series';
25
26
    /**
27
     * The database table used by the model
28
     *
29
     * @var string
30
     */
31
    public $table = self::TABLE_NAME;
32
33
    /**
34
     * Relations
35
     *
36
     * @var array
37
     */
38
    public $hasMany = [
39
        'posts' => [
40
            Post::class,
41
            'key' => self::TABLE_NAME . "_id"
42
        ],
43
    ];
44
45
    /**
46
     * Validation rules
47
     *
48
     * @var array
49
     */
50
    public $rules = [
51
        'title' => "required|unique:" . self::TABLE_NAME . "|min:3|regex:/^[a-z0-9\- ]+$/i",
52
        'slug'  => "required|unique:" . self::TABLE_NAME . "|min:3|regex:/^[a-z0-9\-]+$/i"
53
    ];
54
55
    /**
56
     * Validation messages
57
     *
58
     * @var array
59
     */
60
    public $customMessages = [
61
        'title.required' => Plugin::LOCALIZATION_KEY . 'lang.form.name_required',
62
        'title.unique'   => Plugin::LOCALIZATION_KEY . 'lang.form.name_unique',
63
        'title.regex'    => Plugin::LOCALIZATION_KEY . 'lang.form.name_invalid',
64
        'title.min'      => Plugin::LOCALIZATION_KEY . 'lang.form.name_too_short',
65
    ];
66
67
    /**
68
     * The attributes on which the post list can be ordered
69
     *
70
     * @var array
71
     */
72
    //@todo localize sorting options
73
    public static $sortingOptions = [
74
        'title asc' => 'Title (ascending)',
75
        'title desc' => 'Title (descending)',
76
        'created_at asc' => 'Created (ascending)',
77
        'created_at desc' => 'Created (descending)',
78
        'posts_count asc' => 'Post Count (ascending)',
79
        'posts_count desc' => 'Post Count (descending)',
80
        'random' => 'Random'
81
    ];
82
83
    /**
84
     * @var array
85
     */
86
    protected $slugs = ['slug' => 'title'];
87
88
    /**
89
     * @return mixed
90
     */
91
    public function getPostCountAttribute()
92
    {
93
        return $this->posts()->isPublished()->count();
94
    }
95
96
    /**
97
     * Sets the URL attribute with a URL to this object
98
     *
99
     * @param string                $pageName
100
     * @param Controller            $controller
101
     *
102
     * @return void
103
     */
104
    public function setUrl($pageName, $controller): void
105
    {
106
        $params = [
107
            'slug' => $this->slug,
108
        ];
109
110
        $this->url = $controller->pageUrl($pageName, $params);
111
    }
112
}
113