PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.0.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.0.1
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 10.0.1, at back/Controllers/Fields/Edition.php

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