Passed
Pull Request — master (#20)
by Nic
12:06
created

ElementJobListings::getSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
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
        $now = DBDatetime::now();
119
120
        $jobsRestricted = Job::get()
121
            ->filter([
122
                'PostDate:LessThanOrEqual' => $now,
123
                'EndPostDate:GreaterThanOrEqual' => $now,
124
            ]);
125
126
        $jobsUnrestricted = Job::get()->filter([
127
            'PostDate:LessThanOrEqual' => $now,
128
            'EndPostDate' => [null, ''],
129
        ]);
130
131
        $jobs = Job::get()
132
            ->byIDs(array_merge(
133
                array_values($jobsRestricted->column()),
134
                array_values($jobsUnrestricted->column())
135
            ));
136
137
        /** Specific parent to pull from */
138
        if ($this->JobCollectionID) {
139
            $jobs = $jobs->filter('ParentID', $this->JobCollectionID);
140
        }
141
142
        /** Specific category to pull from */
143
        if ($this->CategoryID) {
144
            $jobs = $jobs->filter('Categories.ID', [$this->CategoryID]);
145
        }
146
147
        $this->extend('updateGetPostsList', $jobs);
148
149
        return $jobs->count()
150
            ? $jobs->sort('PostDate DESC')
151
            : ArrayList::create();
152
    }
153
154
155
    /**
156
     * @return DBHTMLText
157
     */
158
    public function getSummary()
159
    {
160
        $count = $this->getPostsList()->count();
161
        $label = _t(
162
            Job::class . '.PLURALS',
163
            'A Job Posting|{count} Job Postings',
164
            ['count' => $count]
165
        );
166
        return DBField::create_field('HTMLText', $label)->Summary(20);
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    protected function provideBlockSchema()
173
    {
174
        $blockSchema = parent::provideBlockSchema();
175
        $blockSchema['content'] = $this->getSummary();
176
        return $blockSchema;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getType()
183
    {
184
        return _t(__CLASS__ . '.BlockType', 'Job Posts');
185
    }
186
}
187