PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Controllers / Fields / Edition.php

Edition.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress trunk, at back/Controllers/Fields/Edition.php

246 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Fields;
4
5 use AcyMailing\Classes\FieldClass;
6 use AcyMailing\Types\OperatorType;
7
8 trait Edition
9 {
10 public function edit(): void
11 {
12 acym_setVar('layout', 'edit');
13 $fieldId = acym_getVar('int', 'fieldId');
14 $fieldClass = new FieldClass();
15 $languageFieldId = $fieldClass->getLanguageFieldId();
16
17 if (empty($fieldId)) {
18 $field = new \stdClass();
19 $field->id = 0;
20 $field->name = '';
21 $field->active = 1;
22 $field->core = 0;
23 $field->type = 'text';
24 $field->value = '';
25 $field->option = '';
26 $field->default_value = '';
27 $field->required = 0;
28 $field->backend_edition = 1;
29 $field->backend_listing = 0;
30 $field->frontend_edition = 1;
31 $field->frontend_listing = 0;
32 $field->fieldDB = new \stdClass();
33 } else {
34 $field = $fieldClass->getOneById($fieldId);
35 $field->option = empty($field->option) ? new \stdClass() : json_decode($field->option);
36 $field->value = empty($field->value) ? [] : json_decode($field->value);
37 $field->fieldDB = empty($field->option->fieldDB) ? new \stdClass() : json_decode($field->option->fieldDB);
38 if (!in_array($fieldId, [1, 2, $languageFieldId]) && !empty($field->fieldDB->table)) {
39 $databaseExists = acym_loadResult('SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '.acym_escapeDB($field->fieldDB->database));
40 if (!empty($databaseExists)) {
41 $tables = acym_loadResultArray('SHOW TABLES FROM `'.acym_secureDBColumn($field->fieldDB->database).'`');
42 $field->fieldDB->tables = [];
43 foreach ($tables as $one) {
44 $field->fieldDB->tables[$one] = $one;
45 }
46 if (empty($field->fieldDB->table)) {
47 $columns = [];
48 } else {
49 $tables = acym_loadResultArray(
50 'SELECT table_name
51 FROM information_schema.tables
52 WHERE table_schema = '.acym_escapeDB($field->fieldDB->database)
53 );
54 if (in_array($field->fieldDB->table, $tables)) {
55 $columns = acym_loadResultArray(
56 'SHOW COLUMNS FROM `'.acym_secureDBColumn($field->fieldDB->table).'` FROM `'.acym_secureDBColumn($field->fieldDB->database).'`'
57 );
58 } else {
59 $columns = [];
60 }
61 }
62 $field->fieldDB->columns = [];
63 foreach ($columns as $one) {
64 $field->fieldDB->columns[$one] = $one;
65 }
66 array_unshift($field->fieldDB->columns, acym_translation('ACYM_CHOOSE_COLUMN'));
67 }
68 }
69 }
70
71 if (!empty($fieldId)) {
72 $this->breadcrumb[acym_translation($field->name)] = acym_completeLink('fields&task=edit&fieldId='.$fieldId);
73 } else {
74 $this->breadcrumb[acym_translation('ACYM_NEW_CUSTOM_FIELD')] = acym_completeLink('fields&task=edit');
75 }
76
77 $allFields = $fieldClass->getAll();
78
79 $allFieldsName = [];
80 foreach ($allFields as $one) {
81 $allFieldsName[$one->id] = $one->name;
82 }
83
84 $data = [
85 'field' => $field,
86 'database' => acym_getDatabases(),
87 'allFields' => $allFieldsName,
88 'operatorType' => new OperatorType(),
89 'languageFieldId' => $languageFieldId,
90 ];
91
92 $data['fieldType'] = [
93 'text' => acym_translation('ACYM_TEXT'),
94 'textarea' => acym_translation('ACYM_TEXTAREA'),
95 'radio' => acym_translation('ACYM_RADIO'),
96 'checkbox' => acym_translation('ACYM_CHECKBOX'),
97 'single_dropdown' => acym_translation('ACYM_SINGLE_DROPDOWN'),
98 'multiple_dropdown' => acym_translation('ACYM_MULTIPLE_DROPDOWN'),
99 'date' => acym_translation('ACYM_DATE'),
100 'file' => acym_translation('ACYM_FILE'),
101 'phone' => acym_translation('ACYM_PHONE'),
102 'custom_text' => acym_translation('ACYM_CUSTOM_TEXT'),
103 'language' => acym_translation('ACYM_LANGUAGE'),
104 ];
105
106 $this->prepareMultilingualOption($data);
107
108 parent::display($data);
109 }
110
111 public function ajaxGetTables(): void
112 {
113 $database = acym_getVar('string', 'database');
114 $allTables = acym_loadResultArray('SHOW TABLES FROM `'.acym_secureDBColumn($database).'`');
115
116 acym_sendAjaxResponse('', ['tables' => $allTables]);
117 }
118
119 public function ajaxGetColumns(): void
120 {
121 $table = acym_getVar('string', 'table');
122 $database = acym_getVar('string', 'database');
123
124 $columns = acym_loadResultArray('SHOW COLUMNS FROM `'.acym_secureDBColumn($table).'` FROM `'.acym_secureDBColumn($database).'`');
125 array_unshift($columns, 'ACYM_CHOOSE_COLUMN');
126
127 acym_sendAjaxResponse('', ['columns' => $columns]);
128 }
129
130 public function apply(): void
131 {
132 $this->saveField();
133 $this->edit();
134 }
135
136 public function save(): void
137 {
138 $this->saveField();
139 $this->listing();
140 }
141
142 protected function saveField(): void
143 {
144 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
145
146 $fieldClass = new FieldClass();
147 $newField = $this->setFieldToSave();
148 $fieldId = empty($newField->name) ? null : $fieldClass->save($newField);
149 if (!empty($fieldId)) {
150 acym_setVar('fieldId', $fieldId);
151 acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED'));
152 } else {
153 acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error');
154 }
155 }
156
157 private function setFieldToSave(): object
158 {
159 $fieldClass = new FieldClass();
160 $languageFieldId = $fieldClass->getLanguageFieldId();
161 $field = acym_getVar('array', 'field');
162 $fieldDB = json_encode(acym_getVar('array', 'fieldDB'));
163 $fieldId = acym_getVar('int', 'fieldId');
164 if (in_array($fieldId, [2, $languageFieldId])) {
165 $field['required'] = 1;
166 }
167
168 $newField = new \stdClass();
169 if (empty($field['name'])) {
170 return $newField;
171 }
172
173 if (in_array($fieldId, [1, 2])) {
174 $field['type'] = 'text';
175 } elseif ($fieldId == $languageFieldId) {
176 $field['type'] = 'language';
177 }
178
179 $i = 0;
180 $value = [];
181 $fieldValues = empty($field['value']) ? [] : $field['value'];
182 if (!empty($fieldValues['value'])) {
183 foreach ($fieldValues['value'] as $one) {
184 if (empty($one) && $one != '0' && ($i != 0 || !in_array($field['type'], ['single_dropdown', 'multiple_dropdown']))) {
185 $i++;
186 continue;
187 }
188
189 $value[$i] = [
190 'value' => $one,
191 'title' => $fieldValues['title'][$i],
192 'disabled' => $fieldValues['disabled'][$i],
193 ];
194 $i++;
195 }
196 }
197
198 $field['name'] = wp_kses(
199 $field['name'],
200 [
201 'i' => [],
202 'b' => [],
203 'strong' => [],
204 ]
205 );
206 $field['namekey'] = empty($field['namekey']) ? $fieldClass->generateNamekey($field['name']) : $field['namekey'];
207 $field['option']['format'] = $field['type'] === 'date' && empty($field['option']['format']) ? '%d%m%y' : strtolower($field['option']['format']);
208 $field['option']['rows'] = ($field['type'] == 'textarea' && empty($field['option']['rows'])) ? '5' : $field['option']['rows'];
209 $field['option']['columns'] = ($field['type'] == 'textarea' && empty($field['option']['columns'])) ? '30' : $field['option']['columns'];
210
211 $field['value'] = json_encode($value);
212 $field['option']['fieldDB'] = $fieldDB;
213 if (!empty($field['option']['format'])) {
214 $field['option']['format'] = preg_replace('/[^a-z\%]/', '', $field['option']['format']);
215 $field['option']['format'] = preg_replace('/\%[^ymd]/', '', $field['option']['format']);
216 }
217
218 $newField->name = $field['name'];
219 $newField->active = $field['active'];
220 $newField->namekey = $field['namekey'];
221 $newField->type = $field['type'];
222 $newField->required = $field['required'];
223 $newField->option = json_encode($field['option']);
224 $newField->value = $field['value'];
225 $newField->default_value = $field['default_value'];
226 if (ACYM_CMS == 'joomla') {
227 $newField->frontend_edition = $field['frontend_edition'];
228 $newField->frontend_listing = $field['frontend_listing'];
229 }
230 $newField->backend_edition = $field['backend_edition'];
231 $newField->backend_listing = $field['backend_listing'];
232
233 if (empty($fieldId)) {
234 $newField->ordering = $fieldClass->getOrdering() + 1;
235 } else {
236 $newField->id = $fieldId;
237 }
238
239 if (!empty($field['translation'])) {
240 $newField->translation = $field['translation'];
241 }
242
243 return $newField;
244 }
245 }
246