Completed
Push — master ( a7d5a7...2e2685 )
by Robbie
9s
created

SegmentField::setHelpText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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