| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lagdo\DbAdmin\Db\Page\Dml; |
||
| 4 | |||
| 5 | use Lagdo\DbAdmin\Db\Page\AppPage; |
||
| 6 | use Lagdo\DbAdmin\Driver\DriverInterface; |
||
| 7 | use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
||
| 8 | use Lagdo\DbAdmin\Driver\Entity\UserTypeEntity; |
||
| 9 | use Lagdo\DbAdmin\Driver\Utils\Utils; |
||
| 10 | |||
| 11 | use function count; |
||
| 12 | use function implode; |
||
| 13 | use function is_array; |
||
| 14 | use function is_string; |
||
| 15 | use function json_decode; |
||
| 16 | use function preg_match; |
||
| 17 | use function substr; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Reads data from the user inputs for data row insert and update. |
||
| 21 | */ |
||
| 22 | class DataRowReader |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var array<UserTypeEntity> |
||
| 26 | */ |
||
| 27 | private array $userTypes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The constructor |
||
| 31 | * |
||
| 32 | * @param AppPage $page |
||
| 33 | * @param DriverInterface $driver |
||
| 34 | * @param Utils $utils |
||
| 35 | */ |
||
| 36 | public function __construct(private AppPage $page, |
||
| 37 | private DriverInterface $driver, private Utils $utils) |
||
| 38 | { |
||
| 39 | $this->userTypes = $this->driver->userTypes(true); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the user input values for data save on insert and update |
||
| 44 | * Function process_input() in html.inc.php. |
||
| 45 | * |
||
| 46 | * @param TableFieldEntity $field |
||
| 47 | * @param array $values |
||
| 48 | * |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | private function getInputValue(TableFieldEntity $field, array $values): mixed |
||
| 52 | { |
||
| 53 | if ($field->isDisabled()) { |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | $fieldId = $this->driver->bracketEscape($field->name); |
||
| 58 | $userType = $this->userTypes[$field->type] ?? null; |
||
| 59 | $enumValues = $userType?->enums ?? []; |
||
| 60 | if ($field->type === "enum" || count($enumValues) > 0) { |
||
| 61 | // An enum field with no value selected will have no entry in the values. |
||
| 62 | $value = $values['field_values'][$fieldId][0] ?? ''; |
||
| 63 | if ($value === "orig") { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | if ($value === "null") { |
||
| 67 | return "NULL"; |
||
| 68 | } |
||
| 69 | |||
| 70 | $value = substr($value, 4); // 4 - strlen("val-") |
||
| 71 | // There's no function on enum fields. |
||
| 72 | return $this->page->getUnconvertedFieldValue($field, $value); |
||
| 73 | } |
||
| 74 | |||
| 75 | $value = $values['field_values'][$fieldId] ?? ''; |
||
| 76 | |||
| 77 | if ($field->autoIncrement && $value === '') { |
||
| 78 | return null; |
||
| 79 | } |
||
| 80 | |||
| 81 | // The function is not provided for auto-incremented fields or enums. |
||
| 82 | $function = $values['field_functions'][$fieldId] ?? ''; |
||
| 83 | if ($function === 'orig') { |
||
| 84 | return preg_match('~^CURRENT_TIMESTAMP~i', $field->onUpdate) ? |
||
| 85 | $this->driver->escapeId($field->name) : false; |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($function === 'NULL') { |
||
| 89 | return 'NULL'; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($field->type === 'set') { |
||
| 93 | $value = implode(',', (array)$value); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($function === 'json') { |
||
| 97 | $function = ''; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 98 | $value = json_decode($value, true); |
||
| 99 | //! report errors |
||
| 100 | return is_array($value) ? $value : false; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($this->utils->isBlob($field) && $this->utils->iniBool('file_uploads')) { |
||
| 104 | $file = $this->page->getFileContents("fields-$fieldId"); |
||
| 105 | //! report errors |
||
| 106 | return is_string($file) ? $this->driver->quoteBinary($file) : false; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->page->getUnconvertedFieldValue($field, $value, $function); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param array<TableFieldEntity> $fields The table fields |
||
| 114 | * @param array $inputs The user form inputs |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function getInputValues(array $fields, array $inputs): array |
||
| 119 | { |
||
| 120 | // From edit.inc.php |
||
| 121 | $values = []; |
||
| 122 | foreach ($fields as $name => $field) { |
||
| 123 | $value = $this->getInputValue($field, $inputs); |
||
| 124 | if ($value !== false && $value !== null) { |
||
| 125 | $values[$this->driver->escapeId($name)] = $value; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | return $values; |
||
| 130 | } |
||
| 131 | } |
||
| 132 |