Issues (35)

src/Forms/CheckboxSetFieldWithLinks.php (1 issue)

1
<?php
2
3
namespace Sunnysideup\CMSNiceties\Forms;
4
5
use SilverStripe\Forms\CheckboxSetField;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\FieldType\DBField;
8
9
class CheckboxSetFieldWithLinks extends CheckboxSetField
10
{
11
    protected $classNameForLinks = '';
12
13
    protected $isReact = '';
14
15
    protected $linksPerOption = [];
16
17
    public function getTemplate()
18
    {
19
        return self::class;
20
    }
21
22
    public function setClassNameForLinks(string $s): self
23
    {
24
        $this->classNameForLinks = $s;
25
26
        return $this;
27
    }
28
29
    public function getClassNameForLinks(): string
30
    {
31
        return $this->classNameForLinks;
32
    }
33
34
    public function setIsReact(bool $b): self
35
    {
36
        $this->isReact = $b;
37
38
        return $this;
39
    }
40
41
    public function getIsReact(): bool
42
    {
43
        return $this->isReact;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->isReact returns the type string which is incompatible with the type-hinted return boolean.
Loading history...
44
    }
45
46
    /**
47
     * Gets the list of options to render in this formfield.
48
     *
49
     * @return ArrayList
50
     */
51
    public function getOptions()
52
    {
53
        $options = parent::getOptions();
54
        $className = $this->getClassNameForLinks();
55
        if ($className !== '' && $className !== '0') {
56
            foreach ($options as $option) {
57
                $obj = $className::get_by_id($option->Value);
58
                if ($obj && $obj->hasMethod('CMSEditLink')) {
59
                    $link = $obj->CMSEditLink();
60
                    if ($option->isChecked) {
61
                        $this->linksPerOption[] = '<a href="' . $link . '">' . $option->Title . '</a>';
62
                    }
63
64
                    $option->setField('Link', $link);
65
                    $option->setField(
66
                        'Title',
67
                        DBField::create_field('HTMLText', '<a href="' . $link . '">' . $option->Title . '</a>')
68
                    );
69
                }
70
            }
71
        }
72
73
        return $options;
74
    }
75
76
    public function getLinksPerOption(): array
77
    {
78
        return $this->linksPerOption;
79
    }
80
81
    public function getLinksPerOptionAsString(): string
82
    {
83
        return implode(', ', $this->linksPerOption);
84
    }
85
86
    public function Type()
87
    {
88
        return 'optionset checkboxset checkboxsetwithlinks';
89
    }
90
91
    public function getDescription()
92
    {
93
        $this->getOptions();
94
        if ($this->isReact) {
95
            $this->description .= 'Quick Edit: <br />' . $this->getLinksPerOptionAsString();
96
        }
97
98
        return $this->description;
99
    }
100
}
101