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

TemplateVariable::getMIGXInputPropertyHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jgulledge
5
 * Date: 9/30/2017
6
 * Time: 2:44 PM
7
 */
8
9
namespace LCI\Blend\Blendable;
10
11
12
use LCI\Blend\Helpers\MIGXTemplateVariableInput;
13
use LCI\Blend\Helpers\TemplateVariableInput;
14
use LCI\Blend\Helpers\TVInput\OptionValues;
15
16
class TemplateVariable extends Element
17
{
18
    protected $template_names = [];
19
20
    protected $detach_template_names = [];
21
22
    /** @var string  */
23
    protected $opt_cache_key = 'elements/template-variables';
24
25
    /** @var string ~ the xPDO class name */
26
    protected $xpdo_simple_object_class = 'modTemplateVar';
27
28
    /**
29
     * @return TemplateVariable
30
     */
31
    public function getCurrentVersion()
32
    {
33
        /** @var TemplateVariable $element */
34
        $element = new self($this->modx, $this->blender, $this->getFieldName());
35
        return $element->setSeedsDir($this->getSeedsDir());
36
    }
37
38
    /**
39
     * @param string $template_name
40
     * @param int $rank
41
     *
42
     * @return $this
43
     */
44
    public function attachToTemplate($template_name, $rank = 0)
45
    {
46
        $this->template_names[] = [
47
            'name' => $template_name,
48
            'rank' => $rank
49
        ];
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $template_name detach
55
     * @return $this
56
     */
57
    public function detachFromTemplate($template_name)
58
    {
59
        $this->detach_template_names[] = $template_name;
60
        return $this;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getFieldCaption()
67
    {
68
        return $this->blendable_xpdo_simple_object_data['caption'];
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getFieldDefaultText()
75
    {
76
        return $this->blendable_xpdo_simple_object_data['default_text'];
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getFieldDisplay()
83
    {
84
        return $this->blendable_xpdo_simple_object_data['display'];
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getFieldElements()
91
    {
92
        return $this->blendable_xpdo_simple_object_data['elements'];
93
    }
94
95
    /**
96
     * @return TemplateVariableInput
97
     */
98
    public function getInputPropertyHelper()
99
    {
100
        return new TemplateVariableInput($this->getFieldType());
101
    }
102
103
    /**
104
     * @param string $type
105
     * @return MIGXTemplateVariableInput
106
     */
107
    public function getMIGXInputPropertyHelper(string $type='migx')
108
    {
109
        $this->setFieldType($type);
110
        return new MIGXTemplateVariableInput($this->getFieldType());
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getFieldInputProperties()
117
    {
118
        return $this->blendable_xpdo_simple_object_data['input_properties'];
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getFieldOutputProperties()
125
    {
126
        return $this->blendable_xpdo_simple_object_data['output_properties'];
127
    }
128
129
    /**
130
     * @return int
131
     */
132
    public function getFieldRank()
133
    {
134
        return $this->blendable_xpdo_simple_object_data['rank'];
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getFieldType()
141
    {
142
        return $this->blendable_xpdo_simple_object_data['type'];
143
    }
144
145
    // Setters:
146
    /**
147
     * @param string $value  max characters: 80
148
     * @return $this
149
     */
150
    public function setFieldCaption($value)
151
    {
152
        $this->blendable_xpdo_simple_object_data['caption'] = $value;
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $value
158
     * @return $this
159
     */
160
    public function setFieldDefaultText($value)
161
    {
162
        $this->blendable_xpdo_simple_object_data['default_text'] = $value;
163
        return $this;
164
    }
165
166
    /**
167
     * @param string $value  max characters: 20
168
     * @return $this
169
     */
170
    public function setFieldDisplay($value)
171
    {
172
        $this->blendable_xpdo_simple_object_data['display'] = $value;
173
        return $this;
174
    }
175
176
    /**
177
     * @param string $value ~ this is Input Option Values, this is setting it raw
178
     * @deprecated use setInputOptionValues()
179
     * @return $this
180
     */
181
    public function setFieldElements($value)
182
    {
183
        $this->blendable_xpdo_simple_object_data['elements'] = $value;
184
        return $this;
185
    }
186
187
    /**
188
     * @return OptionValues
189
     */
190
    public function makeInputOptionValues()
191
    {
192
        $this->blendable_xpdo_simple_object_data['elements'] = new OptionValues();
193
194
        return $this->blendable_xpdo_simple_object_data['elements'];
195
    }
196
197
    /**
198
     * @param array $value
199
     * @return $this
200
     */
201
    public function setFieldInputProperties($value)
202
    {
203
        $this->blendable_xpdo_simple_object_data['input_properties'] = $value;
204
        return $this;
205
    }
206
207
    /**
208
     * @param string $value
209
     * @return $this
210
     */
211
    public function setFieldOutputProperties($value)
212
    {
213
        $this->blendable_xpdo_simple_object_data['output_properties'] = $value;
214
        return $this;
215
    }
216
217
    /**
218
     * @param int $value
219
     * @return $this
220
     */
221
    public function setFieldRank($value)
222
    {
223
        $this->blendable_xpdo_simple_object_data['rank'] = $value;
224
        return $this;
225
    }
226
227
    /**
228
     * @param string $value  max characters: 20, default options:
229
     *      autotag, checkbox, date, listbox, listbox-multiple, email, file,
230
     *      hidden, image, number, option [radio], resourcelist, richtext, tag, text, textarea, url
231
     * @see https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types
232
     * @return $this
233
     */
234
    public function setFieldType($value)
235
    {
236
        $this->blendable_xpdo_simple_object_data['type'] = $value;
237
        return $this;
238
    }
239
240
    protected function attachRelatedPieces()
241
    {
242
        if (count($this->template_names) > 0) {
243
            $tvs = [];
244
            foreach ($this->template_names as $template_name_data) {
245
                // get the TV:
246
                $template = $this->modx->getObject('modTemplateVar', ['templatename' => $template_name_data['name']]);
247
                if ($template) {
248
                    $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $this->xPDOSimpleObject->getPrimaryKey(), 'templateid' => $template->getPrimaryKey()]);
0 ignored issues
show
Bug introduced by
The method getPrimaryKey() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
                    $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $this->xPDOSimpleObject->/** @scrutinizer ignore-call */ getPrimaryKey(), 'templateid' => $template->getPrimaryKey()]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
249
250
                    if (!$tvt) {
251
                        $tvt = $this->modx->newObject('modTemplateVarTemplate');
252
                    }
253
                    $tvt->set('templateid', $template->get('id'));
254
                    $tvt->set('rand', $template_name_data['rank']);
255
256
                    $tvs[] = $tvt;
257
                } else {
258
                    $this->error = true;
259
260
                }
261
262
            }
263
            $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates');
264
        }
265
266
267
        if (count($this->detach_template_names) > 0) {
268
            $tvs = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tvs is dead and can be removed.
Loading history...
269
            foreach ($this->detach_template_names as $template_name) {
270
                // get the TV:
271
                $template = $this->modx->getObject('modTemplateVar', ['templatename' => $template_name]);
272
                if ($template) {
273
                    $tvt = $this->modx->getObject(
274
                        'modTemplateVarTemplate',
275
                        [
276
                            'templateid' => $template->get('id'),
277
                            'tmplvarid' => $this->xPDOSimpleObject->get('id')
278
                        ]
279
                    );
280
281
                    if (is_object($tvt)) {
282
                        $tvt->remove();
283
                    }
284
                } else {
285
                    $this->error = true;
286
                }
287
            }
288
        }
289
    }
290
}
291