Skip to content

Commit

Permalink
survey consent option
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindRam-Ranium committed Mar 22, 2024
1 parent 8751609 commit 1fa66ff
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 39 deletions.
16 changes: 15 additions & 1 deletion config/filament-user-consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,19 @@
'email-template' => 'vendor.user-consent.layouts.email',

//The mailable class to use for sending consent notification
'mailable' => \Visualbuilder\FilamentUserConsent\Mail\ConsentsUpdatedMail::class
'mailable' => \Visualbuilder\FilamentUserConsent\Mail\ConsentsUpdatedMail::class,

'components' => [
'placeholder' => 'Placeholder',
'likert' => 'Likert Slider',
'text' => 'Free Text Input',
'email' => 'Email Input',
'number' => 'Number Input',
'textarea' => 'Text area',
'select' => 'Select dropdown',
'radio' => 'Radio options',
'check' => 'Checkbox',
'date' => 'Date Picker',
'datetime' => 'Date & Time Picker',
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('organisation_consent_option', function (Blueprint $table) {
$table->id();
$table->foreignId('consent_option_id')->constrained();
$table->foreignId('organisation_id')->constrained();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('organisation_consent_option');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_category_consent_option', function (Blueprint $table) {
$table->id();
$table->foreignId('consent_option_id')->constrained();
$table->foreignId('product_category_id')->constrained();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_category_consent_option');
}
};
33 changes: 33 additions & 0 deletions database/migrations/create_product_consent_option_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_consent_option', function (Blueprint $table) {
$table->id();
$table->foreignId('consent_option_id')->constrained();
$table->foreignId('product_id')->constrained();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_consent_option');
}
};
11 changes: 6 additions & 5 deletions database/migrations/create_user_consent_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ return new class extends Migration
$table->id();
$table->string('key');
$table->unsignedBigInteger('version')->nullable()->default(1);
$table->string('title')->nullable()->default(null);
$table->string('label')->nullable()->default(null);
$table->text('text')->nullable()->default(null);
$table->string('title')->nullable();
$table->string('label')->nullable();
$table->text('text')->nullable();
$table->boolean('is_survey')->default(false);
$table->boolean('additional_info')->default(0);
$table->string('additional_info_title')->nullable();
$table->json('fields')->nullable();
Expand All @@ -28,8 +29,8 @@ return new class extends Migration
$table->boolean('enabled')->default(0);
$table->boolean('force_user_update')->default(0);
$table->integer('sort_order')->default(0);
$table->json('models')->nullable()->default(null);
$table->dateTime('published_at')->default(null);
$table->json('models')->nullable();
$table->dateTime('published_at')->nullable();
$table->timestamps();
});
}
Expand Down
1 change: 0 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
StartSession::class,
VerifyCsrfToken::class,
AuthenticateSession::class,
Authenticate::class,
'auth:admin,practitioner,enduser'
])->group(function () {
Route::get('consent-option-request', ConsentOptionFormBuilder::class)->name('consent-option-request');
Expand Down
2 changes: 1 addition & 1 deletion src/FilamentUserConsentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function getScriptData(): array
protected function getMigrations(): array
{
return [
'create_user_consent_table', 'create_consentables_table',
'create_user_consent_table', 'create_consentables_table', 'create_product_consent_option_table', 'create_product_category_consent_option_table', 'create_organisation_consent_option_table'
];
}

Expand Down
1 change: 0 additions & 1 deletion src/Http/Middleware/ForceRedirectToUnapprovedConsents.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function handle(Request $request, Closure $next)
// Determine if the current route is exempt (a consent route or ends with '.logout')
$route = $request->route()->getName();
$isExemptRoute = str_contains($route, 'consent-options') || str_ends_with($route, '.logout');

// Check for required consents if user is authenticated and not on an exempt or logout route
if (!$isExemptRoute && !$request->ajax() && method_exists($user, 'hasRequiredConsents') && !$user->hasRequiredConsents()) {
// Save the current request URL
Expand Down
30 changes: 17 additions & 13 deletions src/Livewire/ConsentOptionFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,33 @@ protected function getFormSchema(): array
}
$formFields = [Forms\Components\Placeholder::make('welcome')->label('')->content(new HtmlString("Hi {$this->user->firstname},<br>Please read these terms and conditions carefully, we will email a copy to {$this->user->email}"))];
foreach($this->user->collections as $consentOption){


$fields = [
Forms\Components\Placeholder::make('text')->label('')->content(new HtmlString($consentOption->text)),
Forms\Components\Checkbox::make("consents.$consentOption->id")
->label($consentOption->label)
->required($consentOption->is_mandatory)
->required($consentOption->is_survey && $consentOption->is_mandatory)
];

if((int)$consentOption->additional_info === 1) {

$additionInfo = [];
foreach ($consentOption->fields as $field) {
$fieldName = "consents_info.$consentOption->id.{$field['name']}";
$options = array_combine(explode(',',$field['options']), explode(',',$field['options']));
$additionInfo[] = match ($field['type']) {
'text' => Forms\Components\TextInput::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'email' => Forms\Components\TextInput::make($fieldName)->label($field['label'])->email()->required((bool)$field['required']),
'select' => Forms\Components\Select::make($fieldName)->label($field['label'])->options($options)->required((bool)$field['required']),
'textarea' => Forms\Components\Textarea::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'number' => Forms\Components\TextInput::make($fieldName)->label($field['label'])->numeric()->required((bool)$field['required']),
'check' => Forms\Components\Checkbox::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'radio' => Forms\Components\Radio::make($fieldName)->label($field['label'])->options($options)->required((bool)$field['required']),
'date' => Forms\Components\DatePicker::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'datetime' => Forms\Components\DateTimePicker::make($fieldName)->label($field['label'])->required((bool)$field['required']),
$fieldName = "consents_info.$consentOption->id.{$field['name']}";
$options = $field['options'];
$additionInfo[] = match ($field['component']) {
'placeholder' => Forms\Components\Placeholder::make($fieldName)->label('')->content(new HtmlString($field['content']))->columnSpanFull(),
'likert' => Forms\Components\Radio::make($fieldName)->label($field['label'] ?? '')->options($options)->inline(true)->inlineLabel(false)->required((bool)$field['required']),
'text' => Forms\Components\TextInput::make($fieldName)->label($field['label'] ?? '')->required((bool)$field['required']),
'email' => Forms\Components\TextInput::make($fieldName)->label($field['label'] ?? '')->email()->required((bool)$field['required']),
'select' => Forms\Components\Select::make($fieldName)->label($field['label'] ?? '')->options($options)->required((bool)$field['required']),
'textarea' => Forms\Components\Textarea::make($fieldName)->label($field['label'] ?? '')->required((bool)$field['required']),
'number' => Forms\Components\TextInput::make($fieldName)->label($field['label'] ?? '')->numeric()->required((bool)$field['required']),
'check' => Forms\Components\Checkbox::make($fieldName)->label($field['label'] ?? '')->required((bool)$field['required']),
'radio' => Forms\Components\Radio::make($fieldName)->label($field['label'] ?? '')->options($options)->required((bool)$field['required']),
'date' => Forms\Components\DatePicker::make($fieldName)->label($field['label'] ?? '')->required((bool)$field['required']),
'datetime' => Forms\Components\DateTimePicker::make($fieldName)->label($field['label'] ?? '')->required((bool)$field['required']),
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/Models/ConsentOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Visualbuilder\FilamentUserConsent\Database\Factories\ConsentOptionFactory;
Expand Down Expand Up @@ -48,6 +49,7 @@ class ConsentOption extends Model
'title',
'label',
'text',
'is_survey',
'fields',
'is_mandatory',
'force_user_update',
Expand All @@ -73,7 +75,7 @@ class ConsentOption extends Model
// 'is_current' => 'boolean',
// 'force_user_update' => 'boolean',
// 'is_mandatory' => 'boolean',
];
];

/**
* @return string
Expand Down
41 changes: 25 additions & 16 deletions src/Resources/ConsentOptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public static function form(Form $form): Form
Forms\Components\Toggle::make('enabled')
->label('Enable this contract')
->required(),
Forms\Components\Toggle::make('is_survey')
->label('Is this survey consent?')
->required(),
Forms\Components\Toggle::make('is_mandatory')
->required(),

Expand Down Expand Up @@ -102,25 +105,31 @@ public static function form(Form $form): Form
Forms\Components\TextInput::make('name')
->regex('/^[a-z_]+$/')
->required(),
Forms\Components\Select::make('type')
->options([
'text' => 'Text Input',
'email' => 'Email Input',
'number' => 'Number Input',
'date' => 'Date Picker',
'datetime' => 'Date & Time Picker',
'textarea' => 'Text area',
'select' => 'Select dropdown',
'radio' => 'Radio dropdown',
'check' => 'Checkbox',
])
Forms\Components\Select::make('component')
->options(config('filament-user-consent.components'))
->searchable()
->live()
->required(),
Forms\Components\TextInput::make('label')->required(),
Forms\Components\TagsInput::make('options')->separator(',')->splitKeys(['Tab', ' ']),
Forms\Components\Toggle::make('required')->inline(false)->required(),
Forms\Components\TextInput::make('label')
->visible(fn(Get $get) => $get('component') !== 'placeholder'),
Forms\Components\Toggle::make('required')
->inline(false)
->required(fn(Get $get) => $get('component') !== 'placeholder')
->visible(fn(Get $get) => $get('component') !== 'placeholder'),
Forms\Components\RichEditor::make('content')
->required(fn(Get $get) => $get('component') === 'placeholder')
->visible(fn(Get $get) => $get('component') === 'placeholder')
->columnSpanFull(),
Forms\Components\KeyValue::make('options')
->addActionLabel('Add Option')
->keyLabel('Value')
->valueLabel('Label')
->columnSpanFull()
->required(fn(Get $get) => in_array($get('component'), ['select', 'radio', 'likert']))
->visible(fn(Get $get) => in_array($get('component'), ['select', 'radio', 'likert'])),
])
->defaultItems(1)
->columns(3)
->columns(2)
->addActionLabel('Add Field')
->collapsed()
])->visible(fn(Get $get) => (bool)$get('additional_info'))
Expand Down

0 comments on commit 1fa66ff

Please sign in to comment.