Skip to content

Commit

Permalink
Merge pull request #58 from lara-zeus/replicate-action
Browse files Browse the repository at this point in the history
add Replicate Action for forms with it's sections and fields
  • Loading branch information
atmonshi authored Jun 12, 2023
2 parents d63c38e + 6696613 commit 2241a57
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ included a UI for the frontend bilt with filament
- 🔥 show responses for each form
- 🔥 search in all responses filtering on the dynamic fields (soon)
- 🔥 set status for each response
- 🔥 exporting
- 🔥 clone form with `ReplicateAction` (soon)
- 🔥 export or print form with its fields
- 🔥 Replicate forms with its sections and fields
- 🔥 form reports
- 🔥 exam module (set correct answer, the mark for each question, auto correction for all responses, send the mark to the user, and display the result) (soon)
- 🔥 poll module (custom layout to show the form as a poll, and display the result as chart) (soon)
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,6 @@
"ipv4": "رقم IP 4",
"ipv6": "رقم IP 6",
"mac address": "عنوان ماك",
"Replicate": "تكرار",
"Export": "تصدير"
}
24 changes: 24 additions & 0 deletions src/Filament/Resources/FormResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace LaraZeus\Bolt\Filament\Resources;

use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Table;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ReplicateAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
Expand Down Expand Up @@ -93,6 +95,28 @@ public static function table(Table $table): Table
->tooltip(__('view form'))
->url(fn (ZeusForm $record): string => route('bolt.form.show', $record))
->openUrlInNewTab(),

ReplicateAction::make()
->label(__('Replicate'))
->excludeAttributes(['name', 'slug'])
->form([
TextInput::make('name.' . app()->getLocale())->required(),
TextInput::make('slug')->required(),
])
->beforeReplicaSaved(function (ZeusForm $replica, ZeusForm $record, array $data): void {
$repForm = $replica->fill($data);
$repForm->save();
$formID = $repForm->id;
$record->sections->each(function ($item) use ($formID) {
$repSec = $item->replicate()->fill(['form_id' => $formID]);
$repSec->save();
$sectionID = $repSec->id;
$item->fields->each(function ($item) use ($sectionID) {
$repField = $item->replicate()->fill(['section_id' => $sectionID]);
$repField->save();
});
});
}),
]),
])
->filters([
Expand Down
6 changes: 4 additions & 2 deletions src/Models/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Database\Factories\SectionFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Translatable\HasTranslations;

Expand All @@ -26,9 +27,10 @@ protected static function newFactory()
return SectionFactory::new();
}

public function fields()
/** @phpstan-return hasMany<Field> */
public function fields(): HasMany
{
return $this->hasMany(config('zeus-bolt.models.Field'), 'section_id', 'id')->orderBy('ordering');
return $this->hasMany(config('zeus-bolt.models.Field'), 'section_id', 'id');
}

public function form()
Expand Down

0 comments on commit 2241a57

Please sign in to comment.