SegmentField::getHelpText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\View\Requirements;
6
7
class SegmentField extends TextField
8
{
9
    /**
10
     * @var array
11
     */
12
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
13
        'suggest',
14
    );
15
16
    /**
17
     * @var string
18
     */
19
    protected $template = __CLASS__;
20
21
    /**
22
     * @var array
23
     */
24
    protected $modifiers = array();
25
26
    /**
27
     * @var string
28
     */
29
    protected $preview = '';
30
31
    /**
32
     * @var string
33
     */
34
    protected $helpText;
35
36
    /**
37
     * @param array $modifiers
38
     *
39
     * @return $this
40
     */
41
    public function setModifiers(array $modifiers)
42
    {
43
        $this->modifiers = $modifiers;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getModifiers()
52
    {
53
        return $this->modifiers;
54
    }
55
56
    /**
57
     * @param string $preview
58
     *
59
     * @return $this
60
     */
61
    public function setPreview($preview)
62
    {
63
        $this->preview = $preview;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function Preview()
72
    {
73
        return $this->preview;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     *
79
     * @param array $properties
80
     *
81
     * @return string
82
     */
83
    public function Field($properties = array())
84
    {
85
        Requirements::javascript('silverstripe/segment-field:client/dist/js/segment-field.js');
86
        Requirements::css('silverstripe/segment-field:client/dist/styles/segment-field.css');
87
88
        return parent::Field($properties);
89
    }
90
91
    public function Type()
92
    {
93
        return 'segment text';
94
    }
95
96
    /**
97
     * @inheritdoc
98
     *
99
     * @param mixed $request
100
     *
101
     * @return string
102
     */
103
    public function suggest($request)
104
    {
105
        $form = $this->getForm();
106
107
        $preview = $suggestion = '';
108
109
        foreach ($this->modifiers as $modifier) {
110
            if ($modifier instanceof SegmentFieldModifier) {
111
                $this->setModifierProperties($modifier, $form, $request);
112
113
                $preview = $modifier->getPreview($preview);
114
                $suggestion = $modifier->getSuggestion($suggestion);
115
            }
116
117
            if (is_array($modifier)) {
118
                $preview .= $modifier[0];
119
                $suggestion .= $modifier[1];
120
            }
121
        }
122
123
        return json_encode(array(
124
            'suggestion' => $suggestion,
125
            'preview' => $preview,
126
        ));
127
    }
128
129
    /**
130
     * @param SegmentFieldModifier $modifier
131
     * @param mixed $form
132
     * @param mixed $request
133
     *
134
     * @return $this
135
     */
136
    protected function setModifierProperties(SegmentFieldModifier $modifier, $form, $request)
137
    {
138
        $modifier->setField($this);
139
140
        if ($request) {
141
            $modifier->setRequest($request);
142
        }
143
144
        if ($form) {
145
            $modifier->setForm($form);
146
        }
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $string The secondary text to show
153
     * @return $this
154
     */
155
    public function setHelpText($string)
156
    {
157
        $this->helpText = $string;
158
        return $this;
159
    }
160
161
    /**
162
     * @return string the secondary text to show in the template
163
     */
164
    public function getHelpText()
165
    {
166
        return $this->helpText;
167
    }
168
}
169