Passed
Push — master ( 708480...b0c213 )
by Josh
02:45
created

TemplateVariable::getInputPropertyHelper()   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: 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\TemplateVariableInput;
13
14
class TemplateVariable extends Element
15
{
16
    protected $template_names = [];
17
18
    protected $detach_template_names = [];
19
20
    /** @var string  */
21
    protected $opt_cache_key = 'elements/template-variables';
22
23
    /** @var string ~ the xPDO class name */
24
    protected $xpdo_simple_object_class = 'modTemplateVar';
25
26
    /**
27
     * @return TemplateVariable
28
     */
29
    public function getCurrentVersion()
30
    {
31
        /** @var TemplateVariable $element */
32
        $element = new self($this->modx, $this->blender, $this->getFieldName());
33
        return $element->setSeedsDir($this->getSeedsDir());
34
    }
35
36
    /**
37
     * @param string $template_name
38
     * @param int $rank
39
     *
40
     * @return $this
41
     */
42
    public function attachToTemplate($template_name, $rank = 0)
43
    {
44
        $this->template_names[] = [
45
            'name' => $template_name,
46
            'rank' => $rank
47
        ];
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $template_name detach
53
     * @return $this
54
     */
55
    public function detachFromTemplate($template_name)
56
    {
57
        $this->detach_template_names[] = $template_name;
58
        return $this;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getFieldCaption()
65
    {
66
        return $this->blendable_xpdo_simple_object_data['caption'];
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getFieldDefaultText()
73
    {
74
        return $this->blendable_xpdo_simple_object_data['default_text'];
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getFieldDisplay()
81
    {
82
        return $this->blendable_xpdo_simple_object_data['display'];
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getFieldElements()
89
    {
90
        return $this->blendable_xpdo_simple_object_data['elements'];
91
    }
92
93
    /**
94
     * @return TemplateVariableInput
95
     */
96
    public function getInputPropertyHelper()
97
    {
98
        return new TemplateVariableInput($this->getFieldType());
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getFieldInputProperties()
105
    {
106
        return $this->blendable_xpdo_simple_object_data['input_properties'];
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getFieldOutputProperties()
113
    {
114
        return $this->blendable_xpdo_simple_object_data['output_properties'];
115
    }
116
117
    /**
118
     * @return int
119
     */
120
    public function getFieldRank()
121
    {
122
        return $this->blendable_xpdo_simple_object_data['rank'];
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getFieldType()
129
    {
130
        return $this->blendable_xpdo_simple_object_data['type'];
131
    }
132
133
    // Setters:
134
    /**
135
     * @param string $value  max characters: 80
136
     * @return $this
137
     */
138
    public function setFieldCaption($value)
139
    {
140
        $this->blendable_xpdo_simple_object_data['caption'] = $value;
141
        return $this;
142
    }
143
144
    /**
145
     * @param string $value
146
     * @return $this
147
     */
148
    public function setFieldDefaultText($value)
149
    {
150
        $this->blendable_xpdo_simple_object_data['default_text'] = $value;
151
        return $this;
152
    }
153
154
    /**
155
     * @param string $value  max characters: 20
156
     * @return $this
157
     */
158
    public function setFieldDisplay($value)
159
    {
160
        $this->blendable_xpdo_simple_object_data['display'] = $value;
161
        return $this;
162
    }
163
164
    /**
165
     * @param string $value
166
     * @return $this
167
     */
168
    public function setFieldElements($value)
169
    {
170
        $this->blendable_xpdo_simple_object_data['elements'] = $value;
171
        return $this;
172
    }
173
174
    /**
175
     * @param string $value
176
     * @return $this
177
     */
178
    public function setFieldInputProperties($value)
179
    {
180
        $this->blendable_xpdo_simple_object_data['input_properties'] = $value;
181
        return $this;
182
    }
183
184
    /**
185
     * @param string $value
186
     * @return $this
187
     */
188
    public function setFieldOutputProperties($value)
189
    {
190
        $this->blendable_xpdo_simple_object_data['output_properties'] = $value;
191
        return $this;
192
    }
193
194
    /**
195
     * @param int $value
196
     * @return $this
197
     */
198
    public function setFieldRank($value)
199
    {
200
        $this->blendable_xpdo_simple_object_data['rank'] = $value;
201
        return $this;
202
    }
203
204
    /**
205
     * @param string $value  max characters: 20, default options:
206
     *      autotag, checkbox, date, listbox, listbox-multiple, email, file,
207
     *      hidden, image, number, option [radio], resourcelist, richtext, tag, text, textarea, url
208
     * @see https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types
209
     * @return $this
210
     */
211
    public function setFieldType($value)
212
    {
213
        $this->blendable_xpdo_simple_object_data['type'] = $value;
214
        return $this;
215
    }
216
217
    protected function attachRelatedPieces()
218
    {
219
        if (count($this->template_names) > 0) {
220
            $tvs = [];
221
            foreach ($this->template_names as $template_name_data) {
222
                // get the TV:
223
                $template = $this->modx->getObject('modTemplateVar', ['templatename' => $template_name_data['name']]);
224
                if ($template) {
225
                    $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

225
                    $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...
226
227
                    if (!$tvt) {
228
                        $tvt = $this->modx->newObject('modTemplateVarTemplate');
229
                    }
230
                    $tvt->set('templateid', $template->get('id'));
231
                    $tvt->set('rand', $template_name_data['rank']);
232
233
                    $tvs[] = $tvt;
234
                } else {
235
                    $this->error = true;
236
237
                }
238
239
            }
240
            $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates');
241
        }
242
243
244
        if (count($this->detach_template_names) > 0) {
245
            $tvs = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tvs is dead and can be removed.
Loading history...
246
            foreach ($this->detach_template_names as $template_name) {
247
                // get the TV:
248
                $template = $this->modx->getObject('modTemplateVar', ['templatename' => $template_name]);
249
                if ($template) {
250
                    $tvt = $this->modx->getObject(
251
                        'modTemplateVarTemplate',
252
                        [
253
                            'templateid' => $template->get('id'),
254
                            'tmplvarid' => $this->xPDOSimpleObject->get('id')
255
                        ]
256
                    );
257
258
                    if (is_object($tvt)) {
259
                        $tvt->remove();
260
                    }
261
                } else {
262
                    $this->error = true;
263
                }
264
            }
265
        }
266
    }
267
}
268