Completed
Pull Request — master (#15)
by Jason
11:42
created

Job::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 14
ccs 3
cts 3
cp 1
crap 1
rs 9.9666
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
    public function populateDefaults()
128
    {
129
        $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
        parent::populateDefaults();
132
    }
133
134
    /**
135
     * @param bool $includerelations
136
     * @return array
137
     */
138
    public function fieldLabels($includerelations = true)
139
    {
140
        $labels = parent::fieldLabels($includerelations);
141
142 14
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Position Name');
143
        $labels['Content'] = _t(__CLASS__ . '.ContentLabel', 'Introduction');
144 14
        $labels['PositionType'] = _t(__CLASS__ . '.PositionTypeLabel', 'Position Type');
145
        $labels['PostDate'] = _t(__CLASS__ . '.PostDateLabel', 'Post Start Date');
146 14
        $labels['EndPostDate'] = _t(__CLASS__ . '.EndPostDateLabel', 'Post End Date');
147
        $labels['Details'] =  _t(__CLASS__ . '.DetailsTab', "Details");
148
        $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
        return $labels;
152
    }
153
154 1
    /**
155 1
     * @return FieldList
156 1
     */
157
    public function getCMSFields()
158 1
    {
159 1
        $this->beforeUpdateCMSFields(function ($fields) {
160 1
            $fields->dataFieldByName('Content')
161
                ->setTitle($this->fieldLabel('Content'))
162 1
                ->setRows(10);
163 1
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
                        __CLASS__ . '.PostDateDescription',
173
                        'Date position should appear on website'
174 1
                    )),
175
                DateField::create('EndPostDate', $this->fieldLabel('EndPostDate'))
176 1
                    ->setDescription(_t(
177 1
                        __CLASS__ . '.EndPostDateDescription',
178 1
                        'Date position should be removed from website'
179 1
                    )),
180
            ]);
181
182 1
            if ($this->ID) {
183 1
                // categories
184
                $categoriesField = ListboxField::create(
185
                    'Categories',
186
                    _t(JobCategory::class . '.PLURALNAME', 'Categories'),
187 1
                    JobCategory::get()->map()
188
                );
189 1
190 1
                $fields->addFieldsToTab('Root.' . $this->fieldLabel('Details'), [
191 1
                    $categoriesField,
192 1
                ]);
193
194 1
                // sections
195 1
                $config = GridFieldConfig_RelationEditor::create();
196 1
                $config
197 1
                    ->addComponent(new GridFieldOrderableRows('Sort'))
198 1
                    ->addComponent(new GridFieldDeleteAction(false))
199
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
200
                    ->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
                $sectionsField = GridField::create(
204 1
                    'Sections',
205 1
                    _t(JobSection::class . '.PLURALNAME', 'Sections'),
206 1
                    $sections,
207
                    $config
208 1
                )
209 1
                    ->setDescription(_t(
210 1
                        __CLASS__ . '.SectionsDescription',
211 1
                        'ex: Requirements, Education, Duties, etc.'
212
                    ));
213
214 1
                $fields->addFieldsToTab('Root.Main', [
215
                    $sectionsField,
216 1
                ]);
217
218
                // submissions
219
                $config = GridFieldConfig_RelationEditor::create();
220
                $config
221
                    ->addComponent(new GridFieldDeleteAction(false))
222 1
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
223
                    ->removeComponentsByType(GridFieldDeleteAction::class);
224 1
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
                    'Submissions',
228 1
                    _t(JobSubmission::class . '.PLURALNAME', 'Submissions'),
229
                    $submissions,
230
                    $config)
231
                ;
232
                $fields->addFieldsToTab('Root.' . $this->fieldLabel('Submissions'), [
233
                    $submissionsField,
234 1
                ]);
235
            }
236 1
        });
237
238
        return parent::getCMSFields();
239 1
    }
240
241
    /**
242
     * @return string
243
     */
244
    public function getApplyButton()
245
    {
246
        $apply = Controller::join_links(
247
            $this->Link(),
248
            'apply'
249
        );
250
        return $apply;
251
    }
252
253
    /**
254
     * @return mixed
255
     */
256
    public function getApplicationLink()
257
    {
258
        if ($this->parent()->Application()->ID != 0) {
259
            return $this->parent()->Application()->URL;
260
        }
261
        return false;
262
    }
263
264 1
    /**
265
     * @return mixed
266
     */
267 1
    public function getCategoryList()
268
    {
269
        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

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