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

MIGXTemplateVariableInput::getInputProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 10/26/18
6
 * Time: 11:11 AM
7
 */
8
9
namespace LCI\Blend\Helpers;
10
11
use LCI\Blend\Helpers\MIGX\Tab;
12
13
class MIGXTemplateVariableInput implements TemplateVariableInputInterface
14
{
15
    /** @var string  */
16
    protected $type = '';
17
18
    /** @var bool  */
19
    protected $allow_blank = true;
20
21
    /** @var string|mixed */
22
    protected $configs = '';
23
24
    /** @var array  */
25
    protected $forms = [];
26
27
    /** @var array  */
28
    protected $tabs = [];
29
30
    /** @var string|null  */
31
    protected $add_button_text = null;
32
33
    /** @var string  */
34
    protected $preview_url = '';
35
36
    /** @var string  */
37
    protected $json_var_key = '';
38
39
    /** @var bool  */
40
    protected $auto_resource_folders = false;
41
42
    /** @var array  */
43
    protected $custom_properties = [];
44
45
    /**
46
     * TemplateVariableInput constructor.
47
     * @param string $type ~ migx
48
     */
49
    public function __construct(string $type='migx')
50
    {
51
        $this->type = $type;
52
    }
53
54
    /**
55
     * @param string $caption
56
     * @param string|null $wrap_in_form_name ~ advanced usage, HTML form name to wrap tabs in
57
     * @return Tab
58
     */
59
    public function addFormTab(string $caption, $wrap_in_form_name=null)
60
    {
61
        $tab = new Tab($caption);
62
63
        $form = 'no-form-wrap';
64
        if (!empty($wrap_in_form_name)) {
65
            $form = $wrap_in_form_name;
66
            $this->forms[] = $wrap_in_form_name;
67
        }
68
69
        if (!isset($this->tabs[$form])) {
70
            $this->tabs[$form] = [];
71
        }
72
73
        $this->tabs[$form][] = $tab;
74
75
        return $tab;
76
    }
77
78
    /**
79
     * @param bool $pretty_json
80
     * @return array
81
     */
82
    public function getInputProperties(bool $pretty_json=false): array
83
    {
84
        $input_properties = [
85
            'allowBlank' => $this->allow_blank,
86
            'configs' => $this->configs,
87
            'formtabs' => $this->getFormTabsAsJsonString($pretty_json),
88
            'columns' => $this->getColumnsAsJsonString($pretty_json),
89
            'btntext' => $this->add_button_text,
90
            'previewurl' => $this->preview_url,
91
            'jsonvarkey' => $this->json_var_key,
92
            'autoResourceFolders' => $this->auto_resource_folders,
93
        ];
94
95
        return array_merge($input_properties, $this->custom_properties);
96
    }
97
98
    /**
99
     * @param bool $allow_blank
100
     * @return $this
101
     */
102
    public function setAllowBlank(bool $allow_blank): self
103
    {
104
        $this->allow_blank = $allow_blank;
105
        return $this;
106
    }
107
108
    /**
109
     * @param mixed|string $configs
110
     * @return $this
111
     */
112
    public function setConfigs($configs)
113
    {
114
        $this->configs = $configs;
115
        return $this;
116
    }
117
118
    /**
119
     * @param null|string $add_button_text
120
     * @return $this
121
     */
122
    public function setAddButtonText(string $add_button_text): self
123
    {
124
        $this->add_button_text = $add_button_text;
125
        return $this;
126
    }
127
128
    /**
129
     * @param string $preview_url ~ a MODX Resource URL and the resource would have a snippet call
130
     * [[!getImageList? &tvname=`multiitemsgridTv2`]]
131
     * @return $this
132
     */
133
    public function setPreviewUrl(string $preview_url): self
134
    {
135
        $this->preview_url = $preview_url;
136
        return $this;
137
    }
138
139
    /**
140
     * @param string $json_var_key ~ set this if you have multiple calls on the same preview resource
141
     * @return $this
142
     */
143
    public function setJsonVarKey(string $json_var_key): self
144
    {
145
        $this->json_var_key = $json_var_key;
146
        return $this;
147
    }
148
149
    /**
150
     * @param bool $auto_resource_folders
151
     * @return $this
152
     */
153
    public function setAutoResourceFolders(bool $auto_resource_folders): self
154
    {
155
        $this->auto_resource_folders = $auto_resource_folders;
156
        return $this;
157
    }
158
159
    /**
160
     * @param bool $pretty_json
161
     * @return false|string
162
     */
163
    protected function getColumnsAsJsonString($pretty_json=false)
164
    {
165
        $json = [];
166
167
        foreach ($this->tabs as $form => $tabs) {
168
            /** @var Tab $tab */
169
            foreach ($tabs as $tab) {
170
                $json = $tab->getGridColumns($json);
171
            }
172
        }
173
174
        if ($pretty_json) {
175
            return json_encode($json, JSON_PRETTY_PRINT);
176
        }
177
178
        return json_encode($json);
179
    }
180
181
    /**
182
     * @return string|false
183
     */
184
    protected function getFormTabsAsJsonString($pretty_json=false)
185
    {
186
        $json = [];
187
188
        foreach ($this->tabs as $form => $tabs) {
189
            $form_tabs = $this->getFormTabsArray($tabs);
190
191
            if ($form == 'no-form-wrap') {
192
                $json = $form_tabs;
193
            } else {
194
                $json[] = [
195
                    'formname' => $form,
196
                    'formtabs' => $form_tabs
197
                ];
198
            }
199
        }
200
201
        if ($pretty_json) {
202
            return json_encode($json, JSON_PRETTY_PRINT);
203
        }
204
205
        return json_encode($json);
206
    }
207
208
    /**
209
     * @param array $tabs
210
     * @return array
211
     */
212
    protected function getFormTabsArray($tabs)
213
    {
214
        $data = [];
215
216
        /** @var Tab $tab */
217
        foreach ($tabs as $tab) {
218
            $data[] = $tab->toArray();
219
        }
220
221
        return $data;
222
    }
223
224
225
226
}