diff --git a/app/Form/Actions/FormStoreAction.php b/app/Form/Actions/FormStoreAction.php index 7181ff59..32cece88 100644 --- a/app/Form/Actions/FormStoreAction.php +++ b/app/Form/Actions/FormStoreAction.php @@ -37,6 +37,7 @@ class FormStoreAction 'export' => 'nullable|array', 'needs_prevention' => 'present|boolean', 'prevention_text' => 'array', + 'prevention_conditions' => 'array', ]; } diff --git a/app/Form/Actions/FormUpdateAction.php b/app/Form/Actions/FormUpdateAction.php index 49defea4..65f73c56 100644 --- a/app/Form/Actions/FormUpdateAction.php +++ b/app/Form/Actions/FormUpdateAction.php @@ -3,6 +3,7 @@ namespace App\Form\Actions; use App\Form\Models\Form; +use App\Lib\Editor\Condition; use App\Lib\Events\Succeeded; use Illuminate\Http\JsonResponse; use Lorisleiva\Actions\Concerns\AsAction; @@ -36,6 +37,7 @@ class FormUpdateAction 'export' => 'nullable|array', 'needs_prevention' => 'present|boolean', 'prevention_text' => 'array', + 'prevention_conditions' => 'array', ]; } diff --git a/app/Form/Actions/PreventionRememberAction.php b/app/Form/Actions/PreventionRememberAction.php index 25903b8f..9385ded0 100644 --- a/app/Form/Actions/PreventionRememberAction.php +++ b/app/Form/Actions/PreventionRememberAction.php @@ -2,6 +2,7 @@ namespace App\Form\Actions; +use App\Form\Editor\FormConditionResolver; use App\Form\Models\Participant; use App\Prevention\Mails\PreventionRememberMail; use App\Prevention\PreventionSettings; @@ -23,6 +24,10 @@ class PreventionRememberAction ->orWhereNull('last_remembered_at') ); foreach ($query->get() as $participant) { + if (!app(FormConditionResolver::class)->forParticipant($participant)->filterCondition($participant->form->prevention_conditions)) { + continue; + } + if ($participant->getFields()->getMailRecipient() === null || count($participant->preventions()) === 0) { continue; } diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index bb581c38..5b3bb34b 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -5,6 +5,7 @@ namespace App\Form\Models; use App\Form\Data\ExportData; use App\Form\Data\FieldCollection; use App\Form\Data\FormConfigData; +use App\Lib\Editor\Condition; use App\Lib\Editor\EditorData; use Cviebrock\EloquentSluggable\Sluggable; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -39,6 +40,7 @@ class Form extends Model implements HasMedia 'export' => ExportData::class, 'needs_prevention' => 'boolean', 'prevention_text' => EditorData::class, + 'prevention_conditions' => Condition::class, ]; /** @var array */ diff --git a/app/Form/Resources/FormResource.php b/app/Form/Resources/FormResource.php index 69437e42..5dfcf1dd 100644 --- a/app/Form/Resources/FormResource.php +++ b/app/Form/Resources/FormResource.php @@ -52,6 +52,7 @@ class FormResource extends JsonResource 'export' => $this->export, 'needs_prevention' => $this->needs_prevention, 'prevention_text' => $this->prevention_text, + 'prevention_conditions' => $this->prevention_conditions, 'links' => [ 'participant_index' => route('form.participant.index', ['form' => $this->getModel(), 'parent' => null]), 'participant_root_index' => route('form.participant.index', ['form' => $this->getModel(), 'parent' => -1]), @@ -99,6 +100,7 @@ class FormResource extends JsonResource 'prevention_text' => EditorData::default(), 'id' => null, 'export' => ExportData::from([]), + 'prevention_conditions' => ['mode' => 'all', 'ifs' => []], ], 'section_default' => [ 'name' => '', diff --git a/app/Lib/Editor/Condition.php b/app/Lib/Editor/Condition.php index 731433fc..c11bb3ac 100644 --- a/app/Lib/Editor/Condition.php +++ b/app/Lib/Editor/Condition.php @@ -20,21 +20,12 @@ class Condition extends Data public static function fromMedia(Media $media): self { - return static::withoutMagicalCreationFrom($media->getCustomProperty('conditions') ?: [ - 'mode' => 'any', - 'ifs' => [], - ]); + return $media->getCustomProperty('conditions') ? static::withoutMagicalCreationFrom($media->getCustomProperty('conditions')) : static::default(); } - /** - * @param array $block - */ - public static function fromBlock(array $block): self + public static function defaults(): self { - return static::withoutMagicalCreationFrom([ - 'mode' => data_get($block, 'tunes.condition.mode', 'any'), - 'ifs' => data_get($block, 'tunes.condition.ifs', []), - ]); + return static::withoutMagicalCreationFrom(['mode' => 'any', 'ifs' => []]); } public function hasStatements(): bool diff --git a/app/Lib/Editor/ConditionResolver.php b/app/Lib/Editor/ConditionResolver.php index 218f2a54..776eb105 100644 --- a/app/Lib/Editor/ConditionResolver.php +++ b/app/Lib/Editor/ConditionResolver.php @@ -21,6 +21,9 @@ abstract class ConditionResolver */ public function filterBlock(array $block): bool { - return $this->filterCondition(Condition::fromBlock($block)); + return $this->filterCondition(Condition::withoutMagicalCreationFrom([ + 'mode' => data_get($block, 'tunes.condition.mode', 'any'), + 'ifs' => data_get($block, 'tunes.condition.ifs', []), + ])); } } diff --git a/database/factories/Form/Models/FormFactory.php b/database/factories/Form/Models/FormFactory.php index a9f3aa29..26db7218 100644 --- a/database/factories/Form/Models/FormFactory.php +++ b/database/factories/Form/Models/FormFactory.php @@ -4,6 +4,7 @@ namespace Database\Factories\Form\Models; use App\Form\Data\ExportData; use App\Form\Models\Form; +use App\Lib\Editor\Condition; use Database\Factories\Traits\FakesMedia; use Illuminate\Database\Eloquent\Factories\Factory; use Tests\Feature\Form\FormtemplateFieldRequest; @@ -54,6 +55,7 @@ class FormFactory extends Factory 'is_active' => true, 'is_private' => false, 'export' => ExportData::from([]), + 'prevention_conditions' => Condition::defaults(), ]; } diff --git a/database/migrations/2024_07_11_154303_create_forms_prevention_conditions_column.php b/database/migrations/2024_07_11_154303_create_forms_prevention_conditions_column.php new file mode 100644 index 00000000..8ed2b373 --- /dev/null +++ b/database/migrations/2024_07_11_154303_create_forms_prevention_conditions_column.php @@ -0,0 +1,32 @@ +json('prevention_conditions')->default(json_encode(['mode' => 'all', 'ifs' => []])); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('forms', function (Blueprint $table) { + $table->dropColumn('prevention_conditions'); + }); + } +}; diff --git a/resources/js/views/form/Index.vue b/resources/js/views/form/Index.vue index 7d039b6f..bdba101d 100644 --- a/resources/js/views/form/Index.vue +++ b/resources/js/views/form/Index.vue @@ -124,6 +124,9 @@ :rows="6" label="Präventions-Hinweis" > + + +