Completed
Pull Request — master (#15)
by Jason
03:16
created

Job::canView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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;
23
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
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 $table_name = 'Dynamic_Job';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var array
38
     */
39
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
40
        'PositionType' => "Enum(array('Full-time', 'Part-time', 'Freelance', 'Internship'))",
41
        'PostDate' => 'Date',
42
        'EndPostDate' => 'Date',
43
    ];
44
45
    /**
46
     * @var array
47
     */
48
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
49
        'Sections' => JobSection::class,
50
        'Submissions' => JobSubmission::class,
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
57
        'Categories' => JobCategory::class,
58
    ];
59
60
    /**
61
     * @var array
62
     */
63
    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...
64
        'Categories' => [
65
            'Sort' => 'Int',
66
        ],
67
    ];
68
69
    /**
70
     * @var array
71
     */
72
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
73
        'Title',
74
        'Categories.ID' => [
75
            'title' => 'Category',
76
        ],
77
        'PositionType' => [
78
            'title' => 'Type',
79
        ],
80
    ];
81
82
    /**
83
     * @var string
84
     */
85
    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...
86
87
    /**
88
     * @var bool
89
     */
90
    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...
91
92
    /**
93
     * @var bool
94
     */
95
    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...
96
97
    /**
98
     * @var array
99
     */
100
    private static $allowed_children = [];
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
101
102
    /**
103
     * @return SearchContext
104
     */
105
    public function getCustomSearchContext()
106
    {
107
        $fields = $this->scaffoldSearchFields([
108
            'restrictFields' => [
109
                'Title',
110
                'Categories.ID',
111
                'PositionType',
112
            ],
113
        ]);
114
115
        $filters = $this->defaultSearchFilters();
116
117
        return new SearchContext(
118
            $this->ClassName,
119
            $fields,
120
            $filters
121
        );
122
    }
123
124
    /**
125
     *
126
     */
127 14
    public function populateDefaults()
128
    {
129 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...
130
131 14
        parent::populateDefaults();
132
    }
133
134
    /**
135
     * @param bool $includerelations
136
     * @return array
137
     */
138 1
    public function fieldLabels($includerelations = true)
139
    {
140 1
        $labels = parent::fieldLabels($includerelations);
141
142 1
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Position Name');
143 1
        $labels['Content'] = _t(__CLASS__ . '.ContentLabel', 'Introduction');
144 1
        $labels['PositionType'] = _t(__CLASS__ . '.PositionTypeLabel', 'Position Type');
145 1
        $labels['PostDate'] = _t(__CLASS__ . '.PostDateLabel', 'Post Start Date');
146 1
        $labels['EndPostDate'] = _t(__CLASS__ . '.EndPostDateLabel', 'Post End Date');
147 1
        $labels['Details'] =  _t(__CLASS__ . '.DetailsTab', "Details");
148 1
        $labels['Submissions'] = _t(__CLASS__ . '.SubmissionsTab', 'Submissions');
149
        //$labels['Categories'] = _t(JobCategory::class . '.SlideType', 'Image or Video');
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
151 1
        return $labels;
152
    }
153
154
    /**
155
     * @return FieldList
156
     */
157
    public function getCMSFields()
158
    {
159 1
        $this->beforeUpdateCMSFields(function ($fields) {
160 1
            $fields->dataFieldByName('Content')
161 1
                ->setTitle($this->fieldLabel('Content'))
162 1
                ->setRows(10);
163
164 1
            $fields->addFieldsToTab('Root.' . $this->fieldLabel('Details'), [
165 1
                DropdownField::create(
166 1
                    'PositionType',
167 1
                    $this->fieldLabel('PositionType'),
168 1
                    Job::singleton()->dbObject('PositionType')->enumValues()
169 1
                )->setEmptyString('--select--'),
170 1
                DateField::create('PostDate', $this->fieldLabel('PostDate'))
171 1
                    ->setDescription(_t(
172 1
                        __CLASS__ . '.PostDateDescription',
173 1
                        'Date position should appear on website'
174
                    )),
175 1
                DateField::create('EndPostDate', $this->fieldLabel('EndPostDate'))
176 1
                    ->setDescription(_t(
177 1
                        __CLASS__ . '.EndPostDateDescription',
178 1
                        'Date position should be removed from website'
179
                    )),
180
            ]);
181
182 1
            if ($this->ID) {
183
                // categories
184 1
                $categoriesField = ListboxField::create(
185 1
                    'Categories',
186 1
                    _t(JobCategory::class . '.PLURALNAME', 'Categories'),
187 1
                    JobCategory::get()->map()
188
                );
189
190 1
                $fields->addFieldsToTab('Root.' . $this->fieldLabel('Details'), [
191 1
                    $categoriesField,
192
                ]);
193
194
                // sections
195 1
                $config = GridFieldConfig_RelationEditor::create();
196
                $config
197 1
                    ->addComponent(new GridFieldOrderableRows('Sort'))
198 1
                    ->addComponent(new GridFieldDeleteAction(false))
199 1
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
200 1
                    ->removeComponentsByType(GridFieldDeleteAction::class);
201
202 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

202
                $sections = $this->/** @scrutinizer ignore-call */ Sections()->sort('Sort');
Loading history...
203 1
                $sectionsField = GridField::create(
204 1
                    'Sections',
205 1
                    _t(JobSection::class . '.PLURALNAME', 'Sections'),
206 1
                    $sections,
207 1
                    $config
208
                )
209 1
                    ->setDescription(_t(
210 1
                        __CLASS__ . '.SectionsDescription',
211 1
                        'ex: Requirements, Education, Duties, etc.'
212
                    ));
213
214 1
                $fields->addFieldsToTab('Root.Main', [
215 1
                    $sectionsField,
216
                ]);
217
218
                // submissions
219 1
                $config = GridFieldConfig_RelationEditor::create();
220
                $config
221 1
                    ->addComponent(new GridFieldDeleteAction(false))
222 1
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
223 1
                    ->removeComponentsByType(GridFieldDeleteAction::class);
224
225 1
                $submissions = $this->Submissions();
0 ignored issues
show
Bug introduced by
The method Submissions() 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

225
                /** @scrutinizer ignore-call */ 
226
                $submissions = $this->Submissions();
Loading history...
226 1
                $submissionsField = GridField::create(
227 1
                    'Submissions',
228 1
                    _t(JobSubmission::class . '.PLURALNAME', 'Submissions'),
229 1
                    $submissions,
230 1
                    $config
231
                )
232
                ;
233 1
                $fields->addFieldsToTab('Root.' . $this->fieldLabel('Submissions'), [
234 1
                    $submissionsField,
235
                ]);
236
            }
237 1
        });
238
239 1
        return parent::getCMSFields();
240
    }
241
242
    /**
243
     * @return string
244
     */
245 1
    public function getApplyButton()
246
    {
247 1
        $apply = Controller::join_links(
248 1
            $this->Link(),
249 1
            'apply'
250
        );
251 1
        return $apply;
252
    }
253
254
    /**
255
     * @return mixed
256
     */
257 1
    public function getApplicationLink()
258
    {
259 1
        if ($this->parent()->Application()->ID != 0) {
260
            return $this->parent()->Application()->URL;
261
        }
262 1
        return false;
263
    }
264
265
    /**
266
     * @return mixed
267
     */
268
    public function getCategoryList()
269
    {
270
        return $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

270
        return $this->/** @scrutinizer ignore-call */ Categories()->sort('Sort');
Loading history...
271
    }
272
273
    /**
274
     * @return bool
275
     */
276
    public function getPrimaryCategory()
277
    {
278
        if ($this->Categories()->exists()) {
279
            return $this->Categories()->first();
280
        }
281
        return false;
282
    }
283
284
    /**
285
     * @return array
286
     */
287
    public function providePermissions()
288
    {
289
        return [
290
            'JOB_MANAGE' => [
291
                'name' => _t(
292
                    __CLASS__ . '.JOB_MANAGE',
293
                    'Manage Jobs'
294
                ),
295
                'category' => _t(
296
                    __CLASS__ . '.JOB_MANAGE_CATEGORY',
297
                    'Jobs'
298
                ),
299
                'help' => _t(
300
                    __CLASS__ . '.JOB_MANAGE_HELP',
301
                    'Access to add, edit and delete Jobs'
302
                ),
303
                'sort' => 400,
304
            ],
305
        ];
306
    }
307
308
    /**
309
     * @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...
310
     *
311
     * @return bool|int
312
     */
313 1
    public function canEdit($member = null)
314
    {
315 1
        return Permission::check('JOB_MANAGE', 'any', $member);
316
    }
317
318
    /**
319
     * @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...
320
     *
321
     * @return bool|int
322
     */
323 1
    public function canDelete($member = null)
324
    {
325 1
        return Permission::check('JOB_MANAGE', 'any', $member);
326
    }
327
328
    /**
329
     * @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...
330
     *
331
     * @return bool|int
332
     */
333 1
    public function canCreate($member = null, $context = [])
334
    {
335 1
        return Permission::check('JOB_MANAGE', 'any', $member);
336
    }
337
}
338