Passed
Push — master ( 13c395...4d9cd3 )
by Josh
04:14
created

Field::isShowInGrid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 10/26/18
6
 * Time: 11:34 AM
7
 */
8
9
namespace LCI\Blend\Helpers\MIGX;
10
11
use LCI\Blend\Helpers\TVInput\OptionValues;
12
13
class Field
14
{
15
    /** @var string  */
16
    protected $field;
17
18
    /** @var string */
19
    protected $caption;
20
21
    /** @var string */
22
    protected $description;
23
24
    /** @var string */
25
    protected $input_template_variable_name = '';
26
27
    /** @var string */
28
    protected $input_template_variable_type = '';
29
30
    /** @var OptionValues  */
31
    protected $optionValues;
32
33
    /** @var bool  */
34
    protected $migx_media_source = false;
35
36
    /** @var array  */
37
    protected $custom_properties = [];
38
39
    /** @var bool  */
40
    protected $show_in_grid = true;
41
42
    /** @var string  */
43
    protected $grid_header = '';
44
45
    /** @var int  */
46
    protected $grid_width = 100;
47
48
    /** @var bool  */
49
    protected $grid_sortable = true;
50
51
    /** @var string  */
52
    protected $grid_renderer = '';
53
54
    /** @var string "editor": "this.textEditor" */
55
    protected $grid_editor = '';
56
57
    /** @var array  */
58
    protected $grid_custom_properties = [];
59
60
    /**
61
     * Field constructor.
62
     * @param $field ~ this is the name for your placeholder to use with getImageList and a template
0 ignored issues
show
Documentation Bug introduced by
The doc comment ~ at position 0 could not be parsed: Unknown type name '~' at position 0 in ~.
Loading history...
63
     */
64
    public function __construct(string $field)
65
    {
66
        $this->field = $field;
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function toArray()
73
    {
74
        $field = [
75
            'field' => $this->field,
76
            'caption' => $this->caption
77
        ];
78
79
        if (!empty($this->description)) {
80
            $field['description'] = $this->description;
81
        }
82
83
        if (!empty($this->input_template_variable_name)) {
84
            // @TODO throw expection if input_template_variable_type has been set
85
            $field['inputTV'] = $this->input_template_variable_name;
86
        }
87
88
        if (!empty($this->input_template_variable_type)) {
89
            // @TODO throw expection if input_template_variable_name has been set
90
            $field['inputTVtype'] = $this->input_template_variable_type;
91
        }
92
93
        if (!empty($this->optionValues) && $this->optionValues instanceof OptionValues) {
94
            // @TODO review should it throw expection if input_template_variable_name has been set
95
            $field['inputOptionValues'] = $this->optionValues->toString();
96
        }
97
98
        if (!empty($this->migx_media_source)) {
99
            $field['sourceFrom'] = 'migx';
100
        }
101
102
        return array_merge($field, $this->custom_properties);
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function isShowInGrid(): bool
109
    {
110
        return $this->show_in_grid;
111
    }
112
113
    /**
114
     * @return array ~ this is the output grid or viewable data that the content editor will always see
115
     */
116
    public function getGridArray()
117
    {
118
        $grid = [
119
            'header' => $this->grid_header,
120
            'dataIndex' => $this->field,
121
            'width' => $this->grid_width,
122
            'sortable' => $this->grid_sortable,
123
        ];
124
125
        if (!empty($this->grid_editor)) {
126
            $grid['editor'] = $this->grid_editor;
127
        }
128
129
        if (!empty($this->grid_renderer)) {
130
            $grid['renderer'] = $this->grid_renderer;
131
        }
132
133
        return array_merge($grid, $this->grid_custom_properties);
134
    }
135
136
    /**
137
     * @param string $caption ~ Label for the form field
138
     * @return $this
139
     */
140
    public function setCaption(string $caption): self
141
    {
142
        $this->caption = $caption;
143
        return $this;
144
    }
145
146
    /**
147
     * @param string $description ~ Form field description, if empty MIGX will use the description of the inputTV, if any
148
     * @return $this
149
     */
150
    public function setDescription(string $description): self
151
    {
152
        $this->description = $description;
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $input_template_variable_name ~ the Template Variable name that you would like rendered
158
     * This is useful if your data type requires any custom functionality (ie, a default value, output options, etc).
159
     * You can use the same input TV for different fields (ie, if you have an object that has multiple images).
160
     *
161
     * Note can only use either setInputTemplateVariableName() or setInputTemplateVariableType() not both
162
     * @return $this
163
     */
164
    public function setInputTemplateVariableName(string $input_template_variable_name): self
165
    {
166
        $this->input_template_variable_name = $input_template_variable_name;
167
        return $this;
168
    }
169
170
    /**
171
     * @param string $input_template_variable_type ~ any valid MODX template variable type:
172
     * autotag, checkbox, date, listbox, listbox-multiple, email, file,
173
     *      hidden, image, number, option [radio], resourcelist, richtext, tag, text, textarea, url
174
     * @see https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types
175
     * See manager/templates/default/element/tv/renders/input/ files for related code
176
     *
177
     * Note can only use either setInputTemplateVariableName() or setInputTemplateVariableType() not both
178
     * @return $this
179
     */
180
    public function setInputTemplateVariableType(string $input_template_variable_type): self
181
    {
182
        $this->input_template_variable_type = $input_template_variable_type;
183
        return $this;
184
    }
185
186
    /**
187
     * Set to use the Media Source defined on the actual MIGX TV
188
     * @return $this
189
     */
190
    public function useMIGXMediaSource(): self
191
    {
192
        $this->migx_media_source = true;
193
        return $this;
194
    }
195
196
    /**
197
     * @param string $separator ~ the separator between items
198
     * @param string $value_separator ~ the separator between label and value
199
     * @return OptionValues
200
     */
201
    public function loadOptionValues(string $separator='||', string $value_separator='==')
202
    {
203
        $this->optionValues = new OptionValues($separator, $value_separator);
204
205
        return $this->optionValues;
206
    }
207
208
    /**
209
     * Use if a custom MIGX property is needed that is not defined in this object
210
     * @param string $key
211
     * @param mixed $value
212
     * @return $this
213
     */
214
    public function setCustomProperty($key, $value): self
215
    {
216
        $this->custom_properties[$key] = $value;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @param bool $show_in_grid
223
     * @return Field
224
     */
225
    public function setShowInGrid(bool $show_in_grid): self
226
    {
227
        $this->show_in_grid = $show_in_grid;
228
        return $this;
229
    }
230
231
    /**
232
     * @param string $grid_header ~
233
     * @return Field
234
     */
235
    public function setGridHeader(string $grid_header): self
236
    {
237
        $this->grid_header = $grid_header;
238
        return $this;
239
    }
240
241
    /**
242
     * @param int $grid_width
243
     * @return Field
244
     */
245
    public function setGridWidth(int $grid_width): self
246
    {
247
        $this->grid_width = $grid_width;
248
        return $this;
249
    }
250
251
    /**
252
     * @param bool $grid_sortable
253
     * @return Field
254
     */
255
    public function setGridSortable(bool $grid_sortable): self
256
    {
257
        $this->grid_sortable = $grid_sortable;
258
        return $this;
259
    }
260
261
    /**
262
     * @param string $grid_renderer
263
     * @return Field
264
     */
265
    public function setGridRenderer(string $grid_renderer): self
266
    {
267
        $this->grid_renderer = $grid_renderer;
268
        return $this;
269
    }
270
271
    /**
272
     * @param string $grid_editor
273
     * @return Field
274
     */
275
    public function setGridEditor(string $grid_editor): self
276
    {
277
        $this->grid_editor = $grid_editor;
278
        return $this;
279
    }
280
281
    /**
282
     * Use if a custom MIGX grid(columns) property is needed that is not defined in this object
283
     * @param string $key
284
     * @param mixed $value
285
     * @return $this
286
     */
287
    public function setGridCustomProperty($key, $value): self
288
    {
289
        $this->custom_properties[$key] = $value;
290
291
        return $this;
292
    }
293
294
}