EditableTermsAndConditionsCheckbox::getFormField()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * EditableCheckbox
4
 * A user modifiable checkbox on a UserDefinedForm
5
 *
6
 * @package userforms
7
 */
8
9
class EditableTermsAndConditionsCheckbox extends EditableFormField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private static $singular_name = 'Terms and Conditions Checkbox';
0 ignored issues
show
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
    private static $plural_name = 'Terms and Conditions Checkboxes';
0 ignored issues
show
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        "TandCPage" => "SiteTree"
17
    );
18
19
    public function getFieldConfiguration()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21
        $options = parent::getFieldConfiguration();
22
        $options->push(new CheckboxField("Fields[$this->ID][CustomSettings][Default]", _t('EditableFormField.CHECKEDBYDEFAULT', 'Checked by Default?'), $this->getSetting('Default')));
23
24
        $defaultID = ($this->getSetting('TandCPageID')) ? $this->getSetting('TandCPageID') : 0;
25
        $pages = TermsAndConditionsPage::get();
26
        if ($pages->count()) {
27
            $source = TermsAndConditionsPage::get()->map("ID", "Title")->toArray();
28
            $options->push(new DropdownField("Fields[$this->ID][CustomSettings][TandCPageID]", "What is your Terms and Conditions page?  This will be added as a link to the end of your field title.", $source, $defaultID));
29
        } else {
30
            $options->push(new LiteralField("Fields[$this->ID][CustomSettings][TandCPageID]", "<p>You need to add a Terms and Conditions page before you can link to it (which is usually what you do here).</p>"));
31
        }
32
        return $options;
33
    }
34
35
    public function getFormField()
36
    {
37
        $id = intval($this->getSetting('TandCPageID')) - 0;
38
        $page = TermsAndConditionsPage::get()->byID($id);
39
        $extraHTML = '';
40
        if ($page) {
41
            $extraHTML = ' <span class="linkToTermsAndConditionsPage"><a href="'.$page->Link().'" class="externalLink" target="_blank">'.$page->Title.'</a></span>';
42
        }
43
        return new CheckboxField($this->Name, $this->Title.$extraHTML, $this->getSetting('Default'));
44
    }
45
46
    public function getValueFromData($data)
47
    {
48
        $value = (isset($data[$this->Name])) ? $data[$this->Name] : false;
49
        return ($value) ? _t('EditableFormField.YES', 'Yes') : _t('EditableFormField.NO', 'No');
50
    }
51
52
    public function Icon()
53
    {
54
        return 'termsandconditions/images/' . strtolower($this->class) . '.png';
55
    }
56
57
    public function onBeforeWrite()
58
    {
59
        parent::onBeforeWrite();
60
        $this->Required = true;
61
    }
62
}
63