Skip to content

Commit

Permalink
Merge pull request #248 from lara-zeus/embed-with-extension
Browse files Browse the repository at this point in the history
Embed with extension and allow to customize the user attribute
  • Loading branch information
atmonshi authored Feb 5, 2024
2 parents 5e04ecf + bb27534 commit cb68457
Show file tree
Hide file tree
Showing 13 changed files with 498 additions and 451 deletions.
1 change: 1 addition & 0 deletions .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"pest_2.33.4","defects":[],"times":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.187,"P\\Tests\\FormsTest::__pest_evaluable_it_can_list_Form":0.006,"P\\Tests\\FormsTest::__pest_evaluable_it_can_render_Form_List":0.045,"P\\Tests\\FormsTest::__pest_evaluable_it_can_not_edit":0.032,"P\\Tests\\FormsTest::__pest_evaluable_it_can_render_create_form_page":0.085,"P\\Tests\\FormsTest::__pest_evaluable_it_can_create":0.007,"P\\Tests\\FormsTest::__pest_evaluable_it_see_form_when_not_require_login":0.008,"P\\Tests\\FormsTest::__pest_evaluable_it_see_form_when_not_require_login_for_guest":0.008,"P\\Tests\\FormsTest::__pest_evaluable_it_see_form_require_login_for_guest_user":0.009,"P\\Tests\\FormsTest::__pest_evaluable_it_can_retrieve_data":0.049,"P\\Tests\\FormsTest::__pest_evaluable_it_can_render_list_Forms":0.008,"P\\Tests\\FormsTest::__pest_evaluable_it_the_form_can_be_rendered":0.008,"P\\Tests\\FormsTest::__pest_evaluable_it_can_render_show_Form":0.013,"P\\Tests\\FormsTest::__pest_evaluable_it_see_form_require_login_for_logged_in_user":0.008,"P\\Tests\\FormsTest::__pest_evaluable_it_can_save":1.199,"P\\Tests\\FormsTest::__pest_evaluable_it_see_form_date_is_valid":0.01,"P\\Tests\\FormsTest::__pest_evaluable_it_see_ended_form_date":0.027,"P\\Tests\\ResourcesTest::__pest_evaluable_it_can_render_Form_List":0.049,"P\\Tests\\ResourcesTest::__pest_evaluable_it_can_render_Category_List":0.126,"P\\Tests\\ResourcesTest::__pest_evaluable_it_can_render_Collection_List":0.027,"P\\Tests\\ResourcesTest::__pest_evaluable_it_can_test":0.004}}
798 changes: 386 additions & 412 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/usage/embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ to embed the Form in any blade page, simply use:
<livewire:bolt.fill-form slug="feedback" inline="true" />
```

if you have an extension linked to your form, you can pass in the `extensionSlug`

```blade
<livewire:bolt.fill-form slug="feedback" extensionSlug="printers-department" inline="true" />
```

and [this](https://demo.larazeus.com/embed) is how the form look in an empty page.

## Embed in the Sky
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
class="rounded-full"
size="lg"
:src="$getRecord->user->avatar"
:alt="($getRecord->user->name) ?? ''"
:alt="($getRecord->user->{config('auth.providers.users.model')::getUserFullNameAttribute()}) ?? ''"
/>
<p class="flex flex-col gap-1">
<span>{{ ($getRecord->user->name) ?? '' }}</span>
<span>{{ ($getRecord->user->{config('auth.providers.users.model')::getUserFullNameAttribute()}) ?? '' }}</span>
<span>{{ ($getRecord->user->email) ?? '' }}</span>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
class="rounded-full"
size="lg"
:src="$response->user->avatar"
:alt="($response->user->name) ?? ''"
:alt="($response->user->{config('auth.providers.users.model')::getUserFullNameAttribute()}) ?? ''"
/>
<p class="flex flex-col gap-1">
<span>{{ ($response->user->name) ?? '' }}</span>
<span>{{ ($response->user->{config('auth.providers.users.model')::getUserFullNameAttribute()}) ?? '' }}</span>
<span>{{ ($response->user->email) ?? '' }}</span>
</p>
</div>
Expand Down
64 changes: 33 additions & 31 deletions resources/views/themes/zeus/bolt/fill-forms.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,38 @@

<div class="not-prose" style="{{ $colors }}">

@include($boltTheme.'.loading')
@if(!$inline)
@if(!class_exists(\LaraZeus\BoltPro\BoltProServiceProvider::class) || (optional($zeusForm->options)['logo'] === null && optional($zeusForm->options)['cover'] === null))
<x-slot name="header">
<h2>{{ $zeusForm->name ?? '' }}</h2>
<p class="text-gray-400 text-mdd my-2">{{ $zeusForm->description ?? '' }}</p>

@if($zeusForm->start_date !== null)
<div class="text-gray-400 text-sm">
@svg('heroicon-o-calendar','h-4 w-4 inline-flex')
<span>{{ __('Available from') }}:</span>
<span>{{ optional($zeusForm->start_date)->format('Y/m/d') }}</span>,
<span>{{ __('to') }}:</span>
<span>{{ optional($zeusForm->end_date)->format('Y/m/d') }}</span>
</div>
@endif
</x-slot>
@endif

<x-slot name="breadcrumbs">
<li class="flex items-center">
<a href="{{ route('bolt.forms.list') }}">{{ __('Forms') }}</a>
@svg('iconpark-rightsmall-o','fill-current w-4 h-4 mx-3 rtl:rotate-180')
</li>
<li class="flex items-center">
{{ $zeusForm->name }}
</li>
</x-slot>
@endif

@if(!$inline)
@include($boltTheme.'.loading')
@endif

@if(class_exists(\LaraZeus\BoltPro\BoltProServiceProvider::class) && optional($zeusForm->options)['logo'] !== null && optional($zeusForm)->options['cover'] !== null)
<div style="background-image: url('{{ \Illuminate\Support\Facades\Storage::disk(config('zeus-bolt.uploadDisk'))->url($zeusForm->options['cover']) }}')"
Expand Down Expand Up @@ -42,39 +73,10 @@ class="bg-white rounded-full shadow-md shadow-custom-100 sm:w-24 object-cover"
</div>
@endif

@if(!$inline)
@if(!class_exists(\LaraZeus\BoltPro\BoltProServiceProvider::class) || (optional($zeusForm->options)['logo'] === null && optional($zeusForm->options)['cover'] === null))
<x-slot name="header">
<h2>{{ $zeusForm->name ?? '' }}</h2>
<p class="text-gray-400 text-mdd my-2">{{ $zeusForm->description ?? '' }}</p>

@if($zeusForm->start_date !== null)
<div class="text-gray-400 text-sm">
@svg('heroicon-o-calendar','h-4 w-4 inline-flex')
<span>{{ __('Available from') }}:</span>
<span>{{ optional($zeusForm->start_date)->format('Y/m/d') }}</span>,
<span>{{ __('to') }}:</span>
<span>{{ optional($zeusForm->end_date)->format('Y/m/d') }}</span>
</div>
@endif
</x-slot>
@endif

<x-slot name="breadcrumbs">
<li class="flex items-center">
<a href="{{ route('bolt.forms.list') }}">{{ __('Forms') }}</a>
@svg('iconpark-rightsmall-o','fill-current w-4 h-4 mx-3 rtl:rotate-180')
</li>
<li class="flex items-center">
{{ $zeusForm->name }}
</li>
</x-slot>
@endif

@if($sent)
@include($boltTheme.'.submitted')
@else
<x-filament-panels::form wire:submit.prevent="store" class="mx-2">
<x-filament-panels::form wire:submit.prevent="store" class="@if(!$inline) mx-2 @endif">
@if(!$inline)
{{ \LaraZeus\Bolt\Facades\Bolt::renderHookBlade('zeus-form.before') }}
@endif
Expand Down
17 changes: 17 additions & 0 deletions src/Contracts/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public function render(Form $form, array $data): ?string;
*/
public function formComponents(Form $form): ?array;

/*
* will triggered before saving the form, if you want to perform some validation
*
* you can throw an exception or return true
*/
public function preStore(Form $form, array $data): bool;

/**
* the store logic for the app, insert ticket or any DB ONLY calls, don't send here anything,
* and you must return the saved app, if you want to depend on it in the postStore
Expand All @@ -59,4 +66,14 @@ public function postStore(Form $form, array $data): void;
* @param Form $form Bolt form
*/
public function SubmittedRender(Form $form, array $data): ?string;

/*
* list all items connected to a form
*/
public function getItems(Form $form): array;

/*
* return the url to the form, when clicking on `open`. append any parameters you need to your Extension
*/
public function getUrl(Form $form, array $extension): string;
}
6 changes: 5 additions & 1 deletion src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public function appendFilamentComponentsOptions($component, $zeusField, bool $ha
return $relatedFieldValues === $get('zeusData.' . $relatedField);
});

return $component->live(onBlur: $hasVisibility, condition: $hasVisibility);
if ($hasVisibility) {
return $component->live(onBlur: $hasVisibility);
}

return $component;
}

public function getCollectionsValuesForResponse(Field $field, FieldResponse $resp): string
Expand Down
19 changes: 19 additions & 0 deletions src/Filament/Resources/FormResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,28 @@ public static function infolist(Infolist $infolist): Infolist
->schema([
Section::make()->schema([
TextEntry::make('name'),

/*SimpleListEntry::make('links')
->inline(true)
->openUrlInNewTab()
->listStyle('inline')
->badge('-')
->badgeColor('gray')
->emptyStateHeading('item not available')
->emptyStateIcon('heroicon-o-exclamation-triangle')
->itemUrl(fn ($record) => $record['url'])
->itemLabel(fn ($record) => $record['label'])
->getStateUsing(fn (ZeusForm $record) => $record->getUrl())
->visible(fn (ZeusForm $record) => $record->extensions !== null)
->itemIcon('heroicon-o-arrow-top-right-on-square')
->label(__('Form Links')),*/

TextEntry::make('slug')
->url(fn (ZeusForm $record) => route('bolt.form.show', ['slug' => $record->slug]))
->visible(fn (ZeusForm $record) => $record->extensions === null)
->icon('heroicon-o-arrow-top-right-on-square')
->openUrlInNewTab(),

TextEntry::make('description'),
IconEntry::make('is_active')
->icon(fn (string $state): string => match ($state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class ManageResponses extends ManageRelatedRecords

public function table(Table $table): Table
{
$getUserModel = config('auth.providers.users.model')::getUserFullNameAttribute();

$mainColumns = [
ImageColumn::make('user.avatar')
->label(__('Avatar'))
->circular()
->toggleable(),

TextColumn::make('user.name')
TextColumn::make('user.' . $getUserModel)
->label(__('Name'))
->toggleable()
->sortable()
Expand Down
1 change: 1 addition & 0 deletions src/Filament/Resources/FormResource/Pages/ViewForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function getHeaderActions(): array
->tooltip(__('open form'))
->color('warning')
->url(fn () => route('bolt.form.show', $this->record))
->visible(fn (Form $record) => $record->extensions === null)
->openUrlInNewTab(),
];
}
Expand Down
6 changes: 4 additions & 2 deletions src/Livewire/FillForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function getFormModel(): Form
/**
* @throws \Throwable
*/
public function mount(string $slug, ?string $extensionSlug = null, bool $inline = false): void
public function mount(string $slug, ?string $extensionSlug = null, bool $inline = false, array $extensionData = []): void
{
$this->inline = $inline;

Expand All @@ -69,7 +69,7 @@ public function mount(string $slug, ?string $extensionSlug = null, bool $inline
->where('is_active', true)
->firstOrFail();

$this->extensionData = Extensions::init($this->zeusForm, 'canView', ['extensionSlug' => $extensionSlug]) ?? [];
$this->extensionData = Extensions::init($this->zeusForm, 'canView', ['extensionSlug' => $extensionSlug, 'extensionData' => $extensionData]) ?? [];

foreach ($this->zeusForm->fields as $field) {
$this->zeusData[$field->id] = '';
Expand All @@ -84,6 +84,8 @@ public function store(): void
{
$this->validate();

Extensions::init($this->zeusForm, 'preStore', $this->extensionData);

$response = config('zeus-bolt.models.Response')::create([
'form_id' => $this->zeusForm->id,
'user_id' => (auth()->check()) ? auth()->user()->id : null,
Expand Down
19 changes: 19 additions & 0 deletions src/Models/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use LaraZeus\Bolt\Concerns\HasActive;
use LaraZeus\Bolt\Concerns\HasUpdates;
use LaraZeus\Bolt\Database\Factories\FormFactory;
use LaraZeus\Bolt\Facades\Extensions;
use Spatie\Translatable\HasTranslations;

/**
Expand Down Expand Up @@ -170,4 +171,22 @@ public function onePerUser(): bool
&& optional($this->options)['one-entry-per-user']
&& $this->responses()->where('user_id', auth()->user()->id)->exists();
}

public function getUrl(): string | array
{
if ($this->extensions === null) {
return route('bolt.form.show', ['slug' => $this->slug]);
}

return collect(Extensions::init($this, 'getItems', ['form' => $this]))
->mapWithKeys(function ($key, $item) {
return [
$key => [
'label' => $key,
'url' => Extensions::init($this, 'getUrl', ['slug' => $item]),
],
];
})
->toArray();
}
}

0 comments on commit cb68457

Please sign in to comment.