Passed
Push — master ( 05e846...9de07a )
by Jason
02:44
created

Job::getCategoryList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Jobs\Page;
4
5
use Dynamic\Jobs\Model\JobCategory;
6
use Dynamic\Jobs\Model\JobSection;
7
use Dynamic\Jobs\Model\JobSubmission;
8
use \Page;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Forms\DateField;
11
use SilverStripe\Forms\DropdownField;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\GridField\GridField;
14
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
15
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
16
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
17
use SilverStripe\Forms\ListboxField;
18
use SilverStripe\Forms\TreeMultiselectField;
19
use SilverStripe\ORM\Search\SearchContext;
20
use SilverStripe\Security\Permission;
21
use SilverStripe\Security\PermissionProvider;
22
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
0 ignored issues
show
Bug introduced by
The type Symbiote\GridFieldExtens...AddExistingSearchButton 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...
23
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
0 ignored issues
show
Bug introduced by
The type Symbiote\GridFieldExtens...\GridFieldOrderableRows 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...
24
25
/**
26
 * Class Job
27
 * @package Dynamic\Jobs\Model
28
 */
29
class Job extends Page implements PermissionProvider
30
{
31
    /**
32
     * @var string
33
     */
34
    private static $singular_name = 'Job';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var string
38
     */
39
    private static $plural_name = 'Jobs';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var string
43
     */
44
    private static $description = 'Job detail page allowing for application submissions';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @var string
48
     */
49
    private static $table_name = 'Dynamic_Job';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var array
53
     */
54
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
55
        'PositionType' => "Enum(array('Full-time', 'Part-time', 'Freelance', 'Internship'))",
56
        'PostDate' => 'Date',
57
        'EndPostDate' => 'Date',
58
    ];
59
60
    /**
61
     * @var array
62
     */
63
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
64
        'Sections' => JobSection::class,
65
        'Submissions' => JobSubmission::class,
66
    ];
67
68
    /**
69
     * @var array
70
     */
71
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
72
        'Categories' => JobCategory::class,
73
    ];
74
75
    /**
76
     * @var array
77
     */
78
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
79
        'Categories' => [
80
            'Sort' => 'Int',
81
        ],
82
    ];
83
84
    /**
85
     * @var array
86
     */
87
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
88
        'Title',
89
        'Categories.ID' => [
90
            'title' => 'Category',
91
        ],
92
        'PositionType' => [
93
            'title' => 'Type',
94
        ],
95
    ];
96
97
    /**
98
     * @var string
99
     */
100
    private static $default_parent = JobCollection::class;
0 ignored issues
show
introduced by
The private property $default_parent is not used, and could be removed.
Loading history...
101
102
    /**
103
     * @var bool
104
     */
105
    private static $can_be_root = false;
0 ignored issues
show
introduced by
The private property $can_be_root is not used, and could be removed.
Loading history...
106
107
    /**
108
     * @var bool
109
     */
110
    private static $show_in_sitetree = false;
0 ignored issues
show
introduced by
The private property $show_in_sitetree is not used, and could be removed.
Loading history...
111
112
    /**
113
     * @var array
114
     */
115
    private static $allowed_children = [];
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
116
117
    /**
118
     * @return SearchContext
119
     */
120
    public function getCustomSearchContext()
121
    {
122
        $fields = $this->scaffoldSearchFields([
123
            'restrictFields' => [
124
                'Title',
125
                'Categories.ID',
126
                'PositionType',
127
            ],
128
        ]);
129
130
        $filters = $this->defaultSearchFilters();
131
132
        return new SearchContext(
133
            $this->ClassName,
134
            $fields,
135
            $filters
136
        );
137
    }
138
139
    /**
140
     *
141
     */
142 14
    public function populateDefaults()
143
    {
144 14
        $this->PostDate = date('Y-m-d');
0 ignored issues
show
Bug Best Practice introduced by
The property PostDate does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
145
146 14
        parent::populateDefaults();
147
    }
148
149
    /**
150
     * @return FieldList
151
     */
152 1
    public function getCMSFields()
153
    {
154 1
        $fields = parent::getCMSFields();
155
156 1
        $fields->addFieldsToTab('Root.Details.Info', [
157 1
            DropdownField::create(
158 1
                'PositionType',
159 1
                'Position Type',
160 1
                Job::singleton()->dbObject('PositionType')->enumValues()
161 1
            )->setEmptyString('--select--'),
162 1
            DateField::create('PostDate', 'Position Post Date'),
163 1
            DateField::create('EndPostDate', 'Position Post End Date'),
164
        ]);
165
166 1
        if ($this->ID) {
167
            // sections
168 1
            $config = GridFieldConfig_RelationEditor::create();
169 1
            if (class_exists(GridFieldOrderableRows::class)) {
170
                $config->addComponent(new GridFieldOrderableRows('Sort'));
171
            }
172 1
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
173 1
            $config->removeComponentsByType(GridFieldDeleteAction::class);
174 1
            $config->addComponent(new GridFieldDeleteAction(false));
175 1
            $sections = $this->Sections()->sort('Sort');
0 ignored issues
show
Bug introduced by
The method Sections() does not exist on Dynamic\Jobs\Page\Job. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
            $sections = $this->/** @scrutinizer ignore-call */ Sections()->sort('Sort');
Loading history...
176 1
            $sectionsField = GridField::create('Sections', 'Sections', $sections, $config);
177 1
            $fields->addFieldsToTab('Root.Details.Sections', [
178 1
                $sectionsField,
179
            ]);
180
181
            // categories
182 1
            $config = GridFieldConfig_RelationEditor::create();
183 1
            if (class_exists(GridFieldOrderableRows::class)) {
184
                $config->addComponent(new GridFieldOrderableRows('Sort'));
185
            }
186 1
            if (class_exists(GridFieldAddExistingSearchButton::class)) {
187
                $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
188
                $config->addComponent(new GridFieldAddExistingSearchButton());
189
            }
190 1
            $categories = $this->Categories()->sort('Sort');
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on Dynamic\Jobs\Page\Job. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
            $categories = $this->/** @scrutinizer ignore-call */ Categories()->sort('Sort');
Loading history...
191 1
            $categoriesField = GridField::create('Categories', 'Categories', $categories, $config);
0 ignored issues
show
Unused Code introduced by
The assignment to $categoriesField is dead and can be removed.
Loading history...
192
193 1
            $categoriesField = ListboxField::create(
194 1
                'Categories',
195 1
                'Categories',
196 1
                JobCategory::get()->map()
197
            );
198
199 1
            $fields->addFieldsToTab('Root.Details.Categories', [
200 1
                $categoriesField,
201
            ]);
202
        }
203
204 1
        return $fields;
205
    }
206
207
    /**
208
     * @return string
209
     */
210 1
    public function getApplyButton()
211
    {
212 1
        $apply = Controller::join_links(
213 1
            $this->Link(),
214 1
            'apply'
215
        );
216 1
        return $apply;
217
    }
218
219
    /**
220
     * @return mixed
221
     */
222 1
    public function getApplicationLink()
223
    {
224 1
        if ($this->parent()->Application()->ID != 0) {
225
            return $this->parent()->Application()->URL;
226
        }
227 1
        return false;
228
    }
229
230
    /**
231
     * @return mixed
232
     */
233
    public function getCategoryList()
234
    {
235
        return $this->Categories()->sort('Sort');
236
    }
237
238
    /**
239
     * @return bool
240
     */
241
    public function getPrimaryCategory()
242
    {
243
        if ($this->Categories()->exists()) {
244
            return $this->Categories()->first();
245
        }
246
        return false;
247
    }
248
249
    /**
250
     * @return array
251
     */
252 1
    public function providePermissions()
253
    {
254
        return [
255 1
            'Job_EDIT' => 'Edit a Job',
256
            'Job_DELETE' => 'Delete a Job',
257
            'Job_CREATE' => 'Create a Job',
258
        ];
259
    }
260
261
    /**
262
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
263
     *
264
     * @return bool|int
265
     */
266 1
    public function canEdit($member = null)
267
    {
268 1
        return Permission::check('Job_EDIT', 'any', $member);
269
    }
270
271
    /**
272
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
273
     *
274
     * @return bool|int
275
     */
276 1
    public function canDelete($member = null)
277
    {
278 1
        return Permission::check('Job_DELETE', 'any', $member);
279
    }
280
281
    /**
282
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
283
     *
284
     * @return bool|int
285
     */
286 1
    public function canCreate($member = null, $context = [])
287
    {
288 1
        return Permission::check('Job_CREATE', 'any', $member);
289
    }
290
291
    /**
292
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
293
     *
294
     * @return bool
295
     */
296 4
    public function canView($member = null)
297
    {
298 4
        return true;
299
    }
300
}
301