| 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_escape(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 `'.$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 `'.$table.'` FROM `'.$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 |
$fieldClass = new FieldClass(); |
| 145 |
$newField = $this->setFieldToSave(); |
| 146 |
$fieldId = empty($newField->name) ? null : $fieldClass->save($newField); |
| 147 |
if (!empty($fieldId)) { |
| 148 |
acym_setVar('fieldId', $fieldId); |
| 149 |
acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); |
| 150 |
} else { |
| 151 |
acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private function setFieldToSave(): object |
| 156 |
{ |
| 157 |
$fieldClass = new FieldClass(); |
| 158 |
$languageFieldId = $fieldClass->getLanguageFieldId(); |
| 159 |
$field = acym_getVar('array', 'field'); |
| 160 |
$fieldDB = json_encode(acym_getVar('array', 'fieldDB')); |
| 161 |
$fieldId = acym_getVar('int', 'fieldId'); |
| 162 |
if (in_array($fieldId, [2, $languageFieldId])) { |
| 163 |
$field['required'] = 1; |
| 164 |
} |
| 165 |
|
| 166 |
$newField = new \stdClass(); |
| 167 |
if (empty($field['name'])) { |
| 168 |
return $newField; |
| 169 |
} |
| 170 |
|
| 171 |
if (in_array($fieldId, [1, 2])) { |
| 172 |
$field['type'] = 'text'; |
| 173 |
} elseif ($fieldId == $languageFieldId) { |
| 174 |
$field['type'] = 'language'; |
| 175 |
} |
| 176 |
|
| 177 |
$i = 0; |
| 178 |
$value = []; |
| 179 |
$fieldValues = empty($field['value']) ? [] : $field['value']; |
| 180 |
if (!empty($fieldValues['value'])) { |
| 181 |
foreach ($fieldValues['value'] as $one) { |
| 182 |
if (empty($one) && $one != '0' && ($i != 0 || !in_array($field['type'], ['single_dropdown', 'multiple_dropdown']))) { |
| 183 |
$i++; |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
$value[$i] = [ |
| 188 |
'value' => $one, |
| 189 |
'title' => $fieldValues['title'][$i], |
| 190 |
'disabled' => $fieldValues['disabled'][$i], |
| 191 |
]; |
| 192 |
$i++; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$field['name'] = strip_tags($field['name'], '<i><b><strong>'); |
| 197 |
$field['namekey'] = empty($field['namekey']) ? $fieldClass->generateNamekey($field['name']) : $field['namekey']; |
| 198 |
$field['option']['format'] = $field['type'] === 'date' && empty($field['option']['format']) ? '%d%m%y' : strtolower($field['option']['format']); |
| 199 |
$field['option']['rows'] = ($field['type'] == 'textarea' && empty($field['option']['rows'])) ? '5' : $field['option']['rows']; |
| 200 |
$field['option']['columns'] = ($field['type'] == 'textarea' && empty($field['option']['columns'])) ? '30' : $field['option']['columns']; |
| 201 |
|
| 202 |
$field['value'] = json_encode($value); |
| 203 |
$field['option']['fieldDB'] = $fieldDB; |
| 204 |
if (!empty($field['option']['format'])) { |
| 205 |
$field['option']['format'] = preg_replace('/[^a-z\%]/', '', $field['option']['format']); |
| 206 |
$field['option']['format'] = preg_replace('/\%[^ymd]/', '', $field['option']['format']); |
| 207 |
} |
| 208 |
|
| 209 |
$newField->name = $field['name']; |
| 210 |
$newField->active = $field['active']; |
| 211 |
$newField->namekey = $field['namekey']; |
| 212 |
$newField->type = $field['type']; |
| 213 |
$newField->required = $field['required']; |
| 214 |
$newField->option = json_encode($field['option']); |
| 215 |
$newField->value = $field['value']; |
| 216 |
$newField->default_value = $field['default_value']; |
| 217 |
if (ACYM_CMS == 'joomla') { |
| 218 |
$newField->frontend_edition = $field['frontend_edition']; |
| 219 |
$newField->frontend_listing = $field['frontend_listing']; |
| 220 |
} |
| 221 |
$newField->backend_edition = $field['backend_edition']; |
| 222 |
$newField->backend_listing = $field['backend_listing']; |
| 223 |
|
| 224 |
if (empty($fieldId)) { |
| 225 |
$newField->ordering = $fieldClass->getOrdering() + 1; |
| 226 |
} else { |
| 227 |
$newField->id = $fieldId; |
| 228 |
} |
| 229 |
|
| 230 |
if (!empty($field['translation'])) { |
| 231 |
$newField->translation = $field['translation']; |
| 232 |
} |
| 233 |
|
| 234 |
return $newField; |
| 235 |
} |
| 236 |
} |
| 237 |
|