Passed
Push — master ( 3396a4...cb1d79 )
by Josh
02:38
created

TemplateVariable::getFieldElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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