Field::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
144
        if ($this->grid_header === '') {
145
            $this->grid_header = $this->caption;
146
        }
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $description ~ Form field description, if empty MIGX will use the description of the inputTV, if any
153
     * @return $this
154
     */
155
    public function setDescription(string $description): self
156
    {
157
        $this->description = $description;
158
        return $this;
159
    }
160
161
    /**
162
     * @param string $input_template_variable_name ~ the Template Variable name that you would like rendered
163
     * This is useful if your data type requires any custom functionality (ie, a default value, output options, etc).
164
     * You can use the same input TV for different fields (ie, if you have an object that has multiple images).
165
     *
166
     * Note can only use either setInputTemplateVariableName() or setInputTemplateVariableType() not both
167
     * @return $this
168
     */
169
    public function setInputTemplateVariableName(string $input_template_variable_name): self
170
    {
171
        $this->input_template_variable_name = $input_template_variable_name;
172
        return $this;
173
    }
174
175
    /**
176
     * @param string $input_template_variable_type ~ any valid MODX template variable type:
177
     * autotag, checkbox, date, listbox, listbox-multiple, email, file,
178
     *      hidden, image, number, option [radio], resourcelist, richtext, tag, text, textarea, url
179
     * @see https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types
180
     * See manager/templates/default/element/tv/renders/input/ files for related code
181
     *
182
     * Note can only use either setInputTemplateVariableName() or setInputTemplateVariableType() not both
183
     * @return $this
184
     */
185
    public function setInputTemplateVariableType(string $input_template_variable_type): self
186
    {
187
        $this->input_template_variable_type = $input_template_variable_type;
188
        return $this;
189
    }
190
191
    /**
192
     * Set to use the Media Source defined on the actual MIGX TV
193
     * @return $this
194
     */
195
    public function useMIGXMediaSource(): self
196
    {
197
        $this->migx_media_source = true;
198
        return $this;
199
    }
200
201
    /**
202
     * @param string $separator ~ the separator between items
203
     * @param string $value_separator ~ the separator between label and value
204
     * @return OptionValues
205
     */
206
    public function loadOptionValues(string $separator='||', string $value_separator='==')
207
    {
208
        $this->optionValues = new OptionValues($separator, $value_separator);
209
210
        return $this->optionValues;
211
    }
212
213
    /**
214
     * Use if a custom MIGX property is needed that is not defined in this object
215
     * @param string $key
216
     * @param mixed $value
217
     * @return $this
218
     */
219
    public function setCustomProperty($key, $value): self
220
    {
221
        $this->custom_properties[$key] = $value;
222
223
        return $this;
224
    }
225
226
    /**
227
     * @param bool $show_in_grid
228
     * @return Field
229
     */
230
    public function setShowInGrid(bool $show_in_grid): self
231
    {
232
        $this->show_in_grid = $show_in_grid;
233
        return $this;
234
    }
235
236
    /**
237
     * @param string $grid_header ~
238
     * @return Field
239
     */
240
    public function setGridHeader(string $grid_header): self
241
    {
242
        $this->grid_header = $grid_header;
243
        return $this;
244
    }
245
246
    /**
247
     * @param int $grid_width
248
     * @return Field
249
     */
250
    public function setGridWidth(int $grid_width): self
251
    {
252
        $this->grid_width = $grid_width;
253
        return $this;
254
    }
255
256
    /**
257
     * @param bool $grid_sortable
258
     * @return Field
259
     */
260
    public function setGridSortable(bool $grid_sortable): self
261
    {
262
        $this->grid_sortable = $grid_sortable;
263
        return $this;
264
    }
265
266
    /**
267
     * @param string $grid_renderer
268
     *  this.renderImage, this.renderImageFromHtml, this.renderPlaceholder, this.renderFirst, this.renderLimited,
269
     *  this.renderCrossTick, this.renderClickCrossTick, this.renderSwitchStatusOptions, this.renderPositionSelector,
270
     *  this.renderRowActions, this.renderChunk, ImagePlus.MIGX_Renderer, this.renderDate, this.renderOptionSelector
271
     * @see MIGX file: core/components/migx/configs/grid/grid.renderer.inc.php
272
     * @return Field
273
     */
274
    public function setGridRenderer(string $grid_renderer): self
275
    {
276
        $this->grid_renderer = $grid_renderer;
277
        return $this;
278
    }
279
280
    /**
281
     * @param string $grid_editor  this.textEditor & this.listboxEditor
282
     * @see MIGX file: core/components/migx/configs/grid/grid.editors.inc.php
283
     * @return Field
284
     */
285
    public function setGridEditor(string $grid_editor): self
286
    {
287
        $this->grid_editor = $grid_editor;
288
        return $this;
289
    }
290
291
    /**
292
     * Use if a custom MIGX grid(columns) property is needed that is not defined in this object
293
     * @param string $key
294
     * @param mixed $value
295
     * @return $this
296
     */
297
    public function setGridCustomProperty($key, $value): self
298
    {
299
        $this->custom_properties[$key] = $value;
300
301
        return $this;
302
    }
303
304
}