Skip to content

Commit

Permalink
Merge pull request #50 from lara-zeus/multi-response-value
Browse files Browse the repository at this point in the history
Multi response value and more
  • Loading branch information
atmonshi authored Jun 9, 2023
2 parents 3766cbc + 5a8242b commit 1498264
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<x-filament::widget>
<x-filament::card>
<div class="text-center font-semibold">
any changes in the values of these items will affect the responses for the forms in
<span class="text-primary-600">
{{ \LaraZeus\Bolt\Models\Field::whereJsonContains('options->dataSource', "$record->id")->count() }}
</span>
field
</div>
</x-filament::card>
</x-filament::widget>
15 changes: 15 additions & 0 deletions src/Facades/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,19 @@ public static function renderHookBlade($hook)
return Filament::renderHook($hook);
}
}

public static function jsJson($string): bool
{
if ($string === '') {
return false;
}

json_decode($string);

if (json_last_error()) {
return false;
}

return true;
}
}
7 changes: 1 addition & 6 deletions src/Fields/Classes/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static function getOptions(): array
return [
Select::make('options.dataSource')->required()->options(config('zeus-bolt.models.Collection')::pluck('name', 'id'))->label(__('Data Source'))->columnSpan(2),
Toggle::make('options.is_required')->label(__('Is Required')),
\Filament\Forms\Components\TextInput::make('options.html_id')->label(__('Html ID')),
\Filament\Forms\Components\TextInput::make('options.htmlId')
->default(str()->random(6))
->label(__('HTML ID')),
Expand All @@ -31,11 +30,7 @@ public static function getOptions(): array

public function getResponse($field, $resp): string
{
if (! empty($resp->response)) {
return collect(config('zeus-bolt.models.Collection')::find($field->options['dataSource'])->values)->where('itemKey', $resp->response)->first()['itemValue'];
}

return '';
return $this->getCollectionsValuesForResponse($field, $resp);
}

public function appendFilamentComponentsOptions($component, $zeusField)
Expand Down
6 changes: 1 addition & 5 deletions src/Fields/Classes/MultiSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public static function getOptions(): array

public function getResponse($field, $resp): string
{
if (! empty($resp->response)) {
return collect(config('zeus-bolt.models.Collection')::find($field->options['dataSource'])->values)->where('itemKey', $resp->response)->first()['itemValue'];
}

return '';
return $this->getCollectionsValuesForResponse($field, $resp);
}

public function appendFilamentComponentsOptions($component, $zeusField)
Expand Down
21 changes: 1 addition & 20 deletions src/Fields/Classes/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,7 @@ public static function getOptions(): array

public function getResponse($field, $resp): string
{
if (empty($resp->response)) {
return '';
}

$collection = config('zeus-bolt.models.Collection')::find($field->options['dataSource']);
if ($collection === null) {
return $resp->response;
}

$getResponFromCollection = collect($collection->values)->where('itemKey', $resp->response)->first();

if ($getResponFromCollection === null) {
return $resp->response;
}

if (! isset($getResponFromCollection['itemValue'])) {
return $resp->response;
}

return $getResponFromCollection['itemValue'];
return $this->getCollectionsValuesForResponse($field, $resp);
}

public function appendFilamentComponentsOptions($component, $zeusField)
Expand Down
35 changes: 35 additions & 0 deletions src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaraZeus\Bolt\Fields;

use Illuminate\Contracts\Support\Arrayable;
use LaraZeus\Bolt\Facades\Bolt;

abstract class FieldsContract implements Fields, Arrayable
{
Expand Down Expand Up @@ -73,4 +74,38 @@ public function appendFilamentComponentsOptions($component, $zeusField)

return $component;
}

public function getCollectionsValuesForResponse($field, $resp): string
{
$items = $resp->response;

if (empty($items)) {
return $items;
}

if (Bolt::jsJson($resp->response)) {
$items = json_decode($resp->response);
} else {
$items = [$items];
}

$options = config('zeus-bolt.models.Collection')::find($field->options['dataSource']);
if ($options === null) {
return $items;
}

$options = collect($options->values);

if (is_array($items)) {
$options = $options->whereIn('itemKey', $items)->pluck('itemValue')->join(', ');
} else {
$options = $options->where('itemKey', $items)->pluck('itemValue');
}

if (empty($options)) {
$options = implode('-', $items);
}

return $options;
}
}
10 changes: 10 additions & 0 deletions src/Filament/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use LaraZeus\Bolt\Filament\Resources\CollectionResource\Pages;
use LaraZeus\Bolt\Filament\Resources\FormResource\Widgets\BetaNote;
use LaraZeus\Bolt\Filament\Resources\FormResource\Widgets\EditCollectionWarning;

class CollectionResource extends BoltResource
{
Expand Down Expand Up @@ -94,4 +96,12 @@ public static function getPages(): array
'edit' => Pages\EditCollection::route('/{record}/edit'),
];
}

public static function getWidgets(): array
{
return [
BetaNote::class,
EditCollectionWarning::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Filament\Resources\Pages\EditRecord;
use LaraZeus\Bolt\Filament\Resources\CollectionResource;
use LaraZeus\Bolt\Filament\Resources\FormResource\Widgets\BetaNote;
use LaraZeus\Bolt\Filament\Resources\FormResource\Widgets\EditCollectionWarning;

class EditCollection extends EditRecord
{
Expand All @@ -14,6 +15,7 @@ protected function getHeaderWidgets(): array
{
return [
BetaNote::class,
EditCollectionWarning::class,
];
}
}
3 changes: 3 additions & 0 deletions src/Filament/Resources/FormResource/Pages/ViewForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaraZeus\Bolt\Filament\Resources\FormResource\Pages;

use Filament\Pages\Actions\Action;
use Filament\Pages\Actions\EditAction;
use Filament\Pages\Actions\LocaleSwitcher;
use Filament\Resources\Pages\ViewRecord;
use LaraZeus\Bolt\Filament\Resources\FormResource;
Expand All @@ -22,10 +23,12 @@ protected function getActions(): array
{
return [
LocaleSwitcher::make(),
EditAction::make(),
Action::make('entries')
->label(__('Entries'))
->icon('clarity-data-cluster-line')
->tooltip(__('view all entries'))
->color('secondary')
->url(fn () => url('admin/responses?form_id=' . $this->record->id)),

Action::make('view')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace LaraZeus\Bolt\Filament\Resources\FormResource\Widgets;

use Filament\Widgets\Widget;
use Illuminate\Database\Eloquent\Model;

class EditCollectionWarning extends Widget
{
protected int|string|array $columnSpan = 'full';

public ?Model $record = null;

protected static string $view = 'zeus-bolt::filament.resources.form-resource.widgets.edit-collection-warning';
}
17 changes: 12 additions & 5 deletions src/Http/Livewire/FillForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ public function store()
]);

foreach ($this->form->getState()['zeusData'] as $field => $value) {
$fieldResponse['response'] = $value ?? '';
$fieldResponse['response_id'] = $response->id;
$fieldResponse['form_id'] = $this->zeusForm->id;
$fieldResponse['field_id'] = $field;
config('zeus-bolt.models.FieldResponse')::create($fieldResponse);
$setValue = $value;

if (! empty($setValue) && is_array($setValue)) {
$value = json_encode($value);
}

config('zeus-bolt.models.FieldResponse')::create([
'response' => $value ?? '',
'response_id' => $response->id,
'form_id' => $this->zeusForm->id,
'field_id' => $field,
]);
}

event(new FormSent($response, $this->item, $this->form->getState()['itemData'] ?? null));
Expand Down

0 comments on commit 1498264

Please sign in to comment.