Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -96,6 +98,13 @@ class Form implements Renderable
*/
protected $inputs = [];

/**
* Submitted data.
*
* @var array
*/
protected $submittedData = [];

/**
* @var Layout
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -578,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();
Expand Down Expand Up @@ -850,6 +863,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)) {
Expand All @@ -863,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)) {
Expand Down Expand Up @@ -1533,4 +1552,24 @@ public function getLayout(): Layout
{
return $this->layout;
}

/**
* @return mixed
*/
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(
config('admin.upload.directory.file'), $this->submittedData[$field], $this->submittedData[$field]->hashName()
);
return $path;
}else{
return $this->submittedData[$field];
}

}
return null;
}
}
9 changes: 5 additions & 4 deletions src/Form/EmbeddedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ protected function formatColumn($column = '')
*/
protected function formatId($column)
{
return str_replace('.', '_', $column);
return str_replace(['.', ':'], '_', $column);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Form/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -216,7 +217,7 @@ protected function setupScripts()
$this->options['storageUrl'] = $this->storageUrl();
$json_options = json_encode($this->options);
$this->script = <<<JS
var FileUpload_{$id} = new FileUpload(document.querySelector('#{$id}'),{$json_options});
var {$this->fileObjName($id)} = new FileUpload(document.getElementById('{$id}'),{$json_options});
JS;
}

Expand Down Expand Up @@ -250,5 +251,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, '_');
}
}
15 changes: 14 additions & 1 deletion src/Form/Field/MultipleFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -451,7 +452,7 @@ protected function setupScripts()
$this->options['storageUrl'] = $this->storageUrl();
$json_options = json_encode($this->options);
$this->script = <<<JS
var FileUpload_{$id} = new FileUpload(document.querySelector('#{$id}'),{$json_options});
var {$this->fileObjName($id)} = new FileUpload(document.getElementById('{$id}'),{$json_options});
JS;
}

Expand Down Expand Up @@ -492,4 +493,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, '_');
}
}
15 changes: 8 additions & 7 deletions src/Form/Field/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public function load($field, $url, $idField = 'id', $textField = 'text', bool $a
} else {
$class = $field;
}
$unique = uniqid();

$this->additional_script .= <<<JS

let elm = document.querySelector("{$this->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){
Expand Down Expand Up @@ -226,11 +226,12 @@ public function ajax($url, $idField = 'id', $textField = 'text')
'allowHTML' => true,
'placeholder' => $this->label,
], $this->config);
$unique = uniqid();

$this->additional_script = <<<JS
let elm = document.querySelector("{$this->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;
Expand All @@ -240,7 +241,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;
Expand Down Expand Up @@ -310,7 +311,7 @@ public function choicesObjName($field = false)
$field = str_replace([' ', '-'], ['_', '_'], $this->getElementClassString());
}

return 'choices_'.$field;
return 'choices_' . Str::slug($field, '_');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Form/Field/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/Grid/Filter/Presenter/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,12 @@ public function ajax($url, $idField = 'id', $textField = 'text')
'removeItemButton' => true,
'placeholder' => $this->label,
], $this->config);
$unique = uniqid();

$this->additional_script = <<<JS
let elm = document.querySelector(".{$this->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;
Expand All @@ -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;
Expand Down