Passed
Pull Request — master (#20)
by Nic
10:46
created

ElementJobListings::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 28
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\Jobs\Element;
4
5
use DNADesign\Elemental\Models\BaseElement;
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Models\BaseElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Dynamic\Jobs\Model\JobCategory;
7
use Dynamic\Jobs\Page\Job;
8
use Dynamic\Jobs\Page\JobCollection;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\ORM\ArrayList;
12
use SilverStripe\ORM\DataList;
13
use SilverStripe\ORM\FieldType\DBDatetime;
14
use SilverStripe\ORM\FieldType\DBField;
15
use SilverStripe\ORM\FieldType\DBHTMLText;
16
17
if (!class_exists(BaseElement::class)) {
18
    return;
19
}
20
21
/**
22
 * Class ElementJobListings
23
 * @package Dynamic\Jobs\Element
24
 *
25
 * @property int $Limit
26
 * @property string $Content
27
 *
28
 * @property int $JobCollectionID
29
 * @property int $CategoryID
30
 * @method JobCollection Blog()
31
 * @method JobCategory Category()
32
 */
33
class ElementJobListings extends BaseElement
34
{
35
    /**
36
     * @var string
37
     */
38
    private static $icon = 'font-icon-menu-campaigns';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var string
42
     */
43
    private static $table_name = 'ElementJobPosts';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var array
47
     */
48
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
49
        'Limit' => 'Int',
50
        'Content' => 'HTMLText',
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
57
        'JobCollection' => JobCollection::class,
58
        'Category' => JobCategory::class,
59
    ];
60
61
    /**
62
     * @var string[]
63
     */
64
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
65
        'Jobs' => Job::class,
66
    ];
67
68
    /**
69
     * @var array
70
     */
71
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
72
        'Limit' => 3,
73
    ];
74
75
    /**
76
     * @var bool
77
     */
78
    private static $inline_editable = false;
0 ignored issues
show
introduced by
The private property $inline_editable is not used, and could be removed.
Loading history...
79
80
    /**
81
     * @return FieldList
82
     */
83
    public function getCMSFields()
84
    {
85
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
86
            $fields->dataFieldByName('Content')
87
                ->setRows(8);
88
89
            $fields->dataFieldByName('Limit')
90
                ->setTitle(_t(__CLASS__ . 'LimitLabel', 'Posts to show'));
91
92
            $fields->insertBefore(
93
                'Limit',
94
                $fields->dataFieldByName('JobCollectionID')
95
                    ->setTitle(_t(__CLASS__ . 'JobCollectionLabel', 'Jobs Listing Page'))
96
                    ->setEmptyString('')
97
            );
98
99
            $fields->insertAfter(
100
                'BlogID',
101
                DropdownField::create('CategoryID', _t(
102
                    __CLASS__ . 'CategoryLabel',
103
                    'Category'
104
                ))
105
                    ->setHasEmptyDefault(true)
106
                    ->setEmptyString('')
107
            );
108
        });
109
110
        return parent::getCMSFields();
111
    }
112
113
    /**
114
     * @return ArrayList|DataList
115
     */
116
    public function getPostsList()
117
    {
118
        $jobs = Job::get()
119
            ->filter([
120
                'PostDate:LessThanOrEqual' => DBDatetime::now(),
121
                'EndPostDate:GreaterThanOrEqual' => DBDatetime::now(),
122
            ]);
123
124
        /** Specific parent to pull from */
125
        if ($this->JobCollectionID) {
126
            $jobs = $jobs->filter('ParentID', $this->JobCollectionID);
127
        }
128
129
        /** Specific category to pull from */
130
        if ($this->CategoryID) {
131
            $jobs = $jobs->filter('Categories.ID', [$this->CategoryID]);
132
        }
133
134
        $this->extend('updateGetPostsList', $posts);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $posts seems to be never defined.
Loading history...
135
136
        return $jobs->count()
137
            ? $jobs->sort('PostDate DESC')
138
            : ArrayList::create();
139
    }
140
141
142
    /**
143
     * @return DBHTMLText
144
     */
145
    public function getSummary()
146
    {
147
        $count = $this->getPostsList()->count();
148
        $label = _t(
149
            Job::class . '.PLURALS',
150
            'A Job Posting|{count} Job Postings',
151
            ['count' => $count]
152
        );
153
        return DBField::create_field('HTMLText', $label)->Summary(20);
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    protected function provideBlockSchema()
160
    {
161
        $blockSchema = parent::provideBlockSchema();
162
        $blockSchema['content'] = $this->getSummary();
163
        return $blockSchema;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getType()
170
    {
171
        return _t(__CLASS__ . '.BlockType', 'Job Posts');
172
    }
173
}
174