From 0a0ba9fb59b15e088d2eeb7b0ea17e78c73f8615 Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Thu, 3 Aug 2023 09:31:32 +0100 Subject: [PATCH 1/8] added form data bag --- src/Form.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Form.php b/src/Form.php index f61e7990..e2bf8d5e 100644 --- a/src/Form.php +++ b/src/Form.php @@ -28,6 +28,8 @@ use OpenAdmin\Admin\Traits\ShouldSnakeAttributes; use Spatie\EloquentSortable\Sortable; use Symfony\Component\HttpFoundation\Response; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Storage; /** * Class Form. @@ -96,6 +98,13 @@ class Form implements Renderable */ protected $inputs = []; + /** + * Submitted data. + * + * @var array + */ + protected $submittedData = []; + /** * @var Layout */ @@ -475,6 +484,8 @@ protected function applayFieldDisplay() */ protected function prepare($data = []) { + $this->submittedData = array_merge($data, $this->submittedData); + if (($response = $this->callSubmitted()) instanceof Response) { return $response; } @@ -850,6 +861,9 @@ protected function prepareUpdate(array $updates, $oneToOneRelation = false, $isR /** @var Field $field */ foreach ($this->fields() as $field) { $columns = $field->column(); + if (!Arr::has($updates, $columns)) { + continue; + } if ($this->isInvalidColumn($columns, $oneToOneRelation || $field->isJsonType) || (in_array($columns, $this->relation_fields) && !$isRelationUpdate)) { @@ -1533,4 +1547,23 @@ public function getLayout(): Layout { return $this->layout; } + + /** + * @return mixed + */ + public function getData(String $field) + { + if(array_key_exists($field,$this->submittedData)){ + if(is_a($this->submittedData[$field],UploadedFile::class)){ + $path = Storage::disk(config('admin.upload.disk'))-> putFileAs( + config('admin.upload.directory.file'), $this->submittedData[$field], $this->submittedData[$field]->hashName() + ); + return $path; + }else{ + return $this->submittedData[$field]; + } + + } + return null; + } } From 1b8a83b4ab81fe5d922ee95428432892c5b4cefd Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Thu, 10 Aug 2023 22:49:30 +0100 Subject: [PATCH 2/8] fixed unique id js --- src/Form/Field/Select.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Form/Field/Select.php b/src/Form/Field/Select.php index da3ac940..e6d6af0d 100644 --- a/src/Form/Field/Select.php +++ b/src/Form/Field/Select.php @@ -114,12 +114,13 @@ public function load($field, $url, $idField = 'id', $textField = 'text', bool $a } else { $class = $field; } + $unique = uniqid(); $this->additional_script .= <<getElementClassSelector()}"); + let elm_{$unique} = document.querySelector("{$this->getElementClassSelector()}"); var lookupTimeout; - elm.addEventListener('change', function(event) { + elm_{$unique}.addEventListener('change', function(event) { var query = {$this->choicesObjName()}.getValue().value; var current_value = {$this->choicesObjName($field)}.getValue().value; admin.ajax.post("{$url}",{query:query},function(data){ @@ -226,11 +227,12 @@ public function ajax($url, $idField = 'id', $textField = 'text') 'allowHTML' => true, 'placeholder' => $this->label, ], $this->config); + $unique = uniqid(); $this->additional_script = <<getElementClassSelector()}"); + let elm_ajax_{$unique} = document.querySelector("{$this->getElementClassSelector()}"); var lookupTimeout; - elm.addEventListener('search', function(event) { + elm_ajax_{$unique}.addEventListener('search', function(event) { clearTimeout(lookupTimeout); lookupTimeout = setTimeout(function(){ var query = {$this->choicesObjName()}.input.value; @@ -240,7 +242,7 @@ public function ajax($url, $idField = 'id', $textField = 'text') }, 250); }); - elm.addEventListener('choice', function(event) { + elm_ajax_{$unique}.addEventListener('choice', function(event) { {$this->choicesObjName()}.setChoices([], '{$idField}', '{$textField}', true); }); JS; From eb6ff9f2097180116db92ced8e5251f017517003 Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Wed, 6 Sep 2023 19:29:05 +0100 Subject: [PATCH 3/8] Fixed js id --- src/Form/Field/Select.php | 1 - src/Grid/Filter/Presenter/Select.php | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Form/Field/Select.php b/src/Form/Field/Select.php index e6d6af0d..62f1b963 100644 --- a/src/Form/Field/Select.php +++ b/src/Form/Field/Select.php @@ -117,7 +117,6 @@ public function load($field, $url, $idField = 'id', $textField = 'text', bool $a $unique = uniqid(); $this->additional_script .= <<getElementClassSelector()}"); var lookupTimeout; elm_{$unique}.addEventListener('change', function(event) { diff --git a/src/Grid/Filter/Presenter/Select.php b/src/Grid/Filter/Presenter/Select.php index 58f7a624..dc91e67c 100644 --- a/src/Grid/Filter/Presenter/Select.php +++ b/src/Grid/Filter/Presenter/Select.php @@ -186,11 +186,12 @@ public function ajax($url, $idField = 'id', $textField = 'text') 'removeItemButton' => true, 'placeholder' => $this->label, ], $this->config); + $unique = uniqid(); $this->additional_script = <<getElementClass()}"); + let elm_{$unique} = document.querySelector(".{$this->getElementClass()}"); var lookupTimeout; - elm.addEventListener('search', function(event) { + elm_{$unique}.addEventListener('search', function(event) { clearTimeout(lookupTimeout); lookupTimeout = setTimeout(function(){ var query = {$this->choicesObjName()}.input.value; @@ -200,7 +201,7 @@ public function ajax($url, $idField = 'id', $textField = 'text') }, 250); }); - elm.addEventListener('choice', function(event) { + elm_{$unique}.addEventListener('choice', function(event) { {$this->choicesObjName()}.setChoices([], '{$idField}', '{$textField}', true); }); JS; From f53db65df92b78346a17515233a1cb94cb19279f Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Tue, 12 Dec 2023 18:36:49 +0100 Subject: [PATCH 4/8] change to support multilang --- src/Form.php | 7 ++++++- src/Form/EmbeddedForm.php | 9 +++++---- src/Form/Field/Select.php | 2 +- src/Form/Field/Tags.php | 3 +++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Form.php b/src/Form.php index e2bf8d5e..538e283e 100644 --- a/src/Form.php +++ b/src/Form.php @@ -589,7 +589,9 @@ public function update($id, $data = null) foreach ($updates as $column => $value) { /* @var Model $this ->model */ - $this->model->setAttribute($column, $value); + if ($column) { + $this->model->setAttribute($column, $value ?? null); + } } $this->model->save(); @@ -877,6 +879,9 @@ protected function prepareUpdate(array $updates, $oneToOneRelation = false, $isR if ($value !== false) { if (is_array($columns)) { foreach ($columns as $name => $column) { + if (empty($value[$name])) { + $value[$name] = request($name); + } Arr::set($prepared, $column, $value[$name]); } } elseif (is_string($columns)) { diff --git a/src/Form/EmbeddedForm.php b/src/Form/EmbeddedForm.php index 61b74555..21ae64c9 100644 --- a/src/Form/EmbeddedForm.php +++ b/src/Form/EmbeddedForm.php @@ -159,11 +159,12 @@ public function setOriginal($data) */ public function prepare($input) { - foreach ($input as $key => $record) { - $this->setFieldOriginalValue($key); - $input[$key] = $this->prepareValue($key, $record); + if(is_array($input)){ + foreach ($input as $key => $record) { + $this->setFieldOriginalValue($key); + $input[$key] = $this->prepareValue($key, $record); + } } - return $input; } diff --git a/src/Form/Field/Select.php b/src/Form/Field/Select.php index 62f1b963..372af507 100644 --- a/src/Form/Field/Select.php +++ b/src/Form/Field/Select.php @@ -311,7 +311,7 @@ public function choicesObjName($field = false) $field = str_replace([' ', '-'], ['_', '_'], $this->getElementClassString()); } - return 'choices_'.$field; + return 'choices_' . Str::slug($field, '_'); } /** diff --git a/src/Form/Field/Tags.php b/src/Form/Field/Tags.php index a7d49016..1026dfc8 100644 --- a/src/Form/Field/Tags.php +++ b/src/Form/Field/Tags.php @@ -142,6 +142,9 @@ public function saving(\Closure $saveAction) public function prepare($value) { $value = parent::prepare($value); + if (!is_array($value)) { + return $value; + } $value = array_filter($value, 'strlen'); if ($this->keyAsValue) { From b6466b28a62f96ae0d3d02e5e75ad81c65b0a1c0 Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Tue, 12 Dec 2023 19:08:38 +0100 Subject: [PATCH 5/8] variable for file upload --- src/Form/Field.php | 2 +- src/Form/Field/File.php | 13 ++++++++++++- src/Form/Field/MultipleFile.php | 14 +++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Form/Field.php b/src/Form/Field.php index 14793e39..11b15a91 100644 --- a/src/Form/Field.php +++ b/src/Form/Field.php @@ -330,7 +330,7 @@ protected function formatColumn($column = '') */ protected function formatId($column) { - return str_replace('.', '_', $column); + return str_replace(['.', ':'], '_', $column); } /** diff --git a/src/Form/Field/File.php b/src/Form/Field/File.php index fb849d2d..e681dfe6 100644 --- a/src/Form/Field/File.php +++ b/src/Form/Field/File.php @@ -216,7 +216,7 @@ protected function setupScripts() $this->options['storageUrl'] = $this->storageUrl(); $json_options = json_encode($this->options); $this->script = <<fileObjName($id)} = new FileUpload(document.querySelector('#{$id}'),{$json_options}); JS; } @@ -250,5 +250,16 @@ public function render() $this->setupScripts(); return parent::render(); + } + /** + * Returns variable name for file object. + */ + public function fileObjName($field = false) + { + if (empty($field)) { + $field = str_replace([' ', '-'], ['_', '_'], $this->getElementClassString()); + } + + return 'FileUpload_' . Str::slug($field, '_'); } } diff --git a/src/Form/Field/MultipleFile.php b/src/Form/Field/MultipleFile.php index f52a66d3..6d4cc356 100644 --- a/src/Form/Field/MultipleFile.php +++ b/src/Form/Field/MultipleFile.php @@ -451,7 +451,7 @@ protected function setupScripts() $this->options['storageUrl'] = $this->storageUrl(); $json_options = json_encode($this->options); $this->script = <<fileObjName($id)} = new FileUpload(document.querySelector('#{$id}'),{$json_options}); JS; } @@ -492,4 +492,16 @@ public function render() return parent::render(); } + + /** + * Returns variable name for file object. + */ + public function fileObjName($field = false) + { + if (empty($field)) { + $field = str_replace([' ', '-'], ['_', '_'], $this->getElementClassString()); + } + + return 'FileUpload_' . Str::slug($field, '_'); + } } From 5565583a604fd55c08704814c02c6ae46736e59a Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Tue, 12 Dec 2023 19:13:16 +0100 Subject: [PATCH 6/8] fixed use Illuminate\Support\Str; --- src/Form/Field/File.php | 1 + src/Form/Field/MultipleFile.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Form/Field/File.php b/src/Form/Field/File.php index e681dfe6..c4b389fc 100644 --- a/src/Form/Field/File.php +++ b/src/Form/Field/File.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use OpenAdmin\Admin\Form\Field; +use Illuminate\Support\Str; use OpenAdmin\Admin\Form\Field\Traits\HasMediaPicker; use OpenAdmin\Admin\Form\Field\Traits\UploadField; use Symfony\Component\HttpFoundation\File\UploadedFile; diff --git a/src/Form/Field/MultipleFile.php b/src/Form/Field/MultipleFile.php index 6d4cc356..647a8289 100644 --- a/src/Form/Field/MultipleFile.php +++ b/src/Form/Field/MultipleFile.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use OpenAdmin\Admin\Form; +use Illuminate\Support\Str; use OpenAdmin\Admin\Form\Field; use OpenAdmin\Admin\Form\Field\Traits\HasMediaPicker; use OpenAdmin\Admin\Form\Field\Traits\UploadField; From 51b62717a817bd46239f39b83f59109d37cfbbf3 Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Tue, 12 Dec 2023 19:22:40 +0100 Subject: [PATCH 7/8] fixed js var --- src/Form/Field/File.php | 2 +- src/Form/Field/MultipleFile.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form/Field/File.php b/src/Form/Field/File.php index c4b389fc..f07d85d4 100644 --- a/src/Form/Field/File.php +++ b/src/Form/Field/File.php @@ -217,7 +217,7 @@ protected function setupScripts() $this->options['storageUrl'] = $this->storageUrl(); $json_options = json_encode($this->options); $this->script = <<fileObjName($id)} = new FileUpload(document.querySelector('#{$id}'),{$json_options}); + var {$this->fileObjName($id)} = new FileUpload(document.getElementById('{$id}'),{$json_options}); JS; } diff --git a/src/Form/Field/MultipleFile.php b/src/Form/Field/MultipleFile.php index 647a8289..cd04fa42 100644 --- a/src/Form/Field/MultipleFile.php +++ b/src/Form/Field/MultipleFile.php @@ -452,7 +452,7 @@ protected function setupScripts() $this->options['storageUrl'] = $this->storageUrl(); $json_options = json_encode($this->options); $this->script = <<fileObjName($id)} = new FileUpload(document.querySelector('#{$id}'),{$json_options}); + var {$this->fileObjName($id)} = new FileUpload(document.getElementById('{$id}'),{$json_options}); JS; } From 98d5253b2ec4a5145a5c51bc5f4b7cdbd16e682f Mon Sep 17 00:00:00 2001 From: Loic NGOU Date: Thu, 14 Dec 2023 17:07:21 +0100 Subject: [PATCH 8/8] update --- src/Form.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Form.php b/src/Form.php index 538e283e..55cb7547 100644 --- a/src/Form.php +++ b/src/Form.php @@ -1558,6 +1558,7 @@ public function getLayout(): Layout */ public function getData(String $field) { + $this->submittedData[$field] = request()->get($field)??request()->file($field); if(array_key_exists($field,$this->submittedData)){ if(is_a($this->submittedData[$field],UploadedFile::class)){ $path = Storage::disk(config('admin.upload.disk'))-> putFileAs(