feedback forms updated

master
Константин 2024-01-31 19:15:37 +03:00
parent 78f97c32a1
commit 8fb69c7539
12 changed files with 118 additions and 95 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace App\Events\Feedback;
use App\Models\Objects\NirObject;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class FeedbackFormSubmitted {
use Dispatchable, InteractsWithSockets, SerializesModels;
public NirObject $object;
public string $mailto;
public function __construct(NirObject $object, string $mailto) {
$this->object = $object;
$this->mailto = $mailto;
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class FeedbackSender {
use Dispatchable, InteractsWithSockets, SerializesModels;
public string $email;
public array $data;
public function __construct(string $email, array $data) {
$this->email = $email;
$this->data = $data;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Listeners\Feedback;
use App\Events\Feedback\FeedbackFormSubmitted;
use App\Mail\Feedback\NotifyFeedbackFormSubmitted;
use Illuminate\Support\Facades\Mail;
class SendFeedbackFormNotifications {
public function __construct() {
}
public function handle(FeedbackFormSubmitted $event) {
try {
Mail::to($event->mailto)->send(new NotifyFeedbackFormSubmitted($event->object));
} catch (\Exception $exception) {
var_dump($exception);
}
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace App\Listeners;
use App\Mail\FeedbackSender;
use Illuminate\Support\Facades\Mail;
class SendFeedbackMessage {
public function __construct() {
}
public function handle(object $event) {
try {
Mail::to($event->email)->send(new FeedbackSender($event->data));
} catch (\Exception $exception) {
var_dump($exception);
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Mail\Feedback;
use App\Models\Objects\NirObject;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NotifyFeedbackFormSubmitted extends Mailable implements ShouldQueue {
use Queueable, SerializesModels;
public NirObject $object;
public function __construct(NirObject $object) {
$this->object = $object;
}
public function build(): NotifyFeedbackFormSubmitted {
$this->subject('Поступило новое обращение')->view('mail.feedback.submitted');
$this->object->attachedFiles->each(function($asset) {
$this->attachFromStorage($asset->path);
});
return $this;
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class FeedbackSender extends Mailable implements ShouldQueue {
use Queueable, SerializesModels;
public array $data;
public function __construct(array $data) {
$this->data = $data;
}
public function build() {
return $this->subject('Поступило новое обращение')->view('mail.feedback.support');
}
}

View File

@ -89,6 +89,10 @@ class Field extends Model {
return new $transformer; return new $transformer;
} }
public function getIsFileAttribute(): bool {
return in_array($this->type, [FieldType::DOCUMENT, FieldType::IMAGE]);
}
public function getRepresented($filters = [], ?FiltersService $service = null): ?Collection { public function getRepresented($filters = [], ?FiltersService $service = null): ?Collection {

View File

@ -148,6 +148,28 @@ class NirObject extends Model {
return $result; return $result;
} }
public function parsedValue($fieldName): ?string {
if ($field = $this->type->getField($fieldName)) return $field->getValue($this->id)->map(function($val) use($field) {
if ($field->type === FieldType::BOOLEAN) return $val ? 'Да' : 'Нет';
elseif ($field->type === FieldType::DATE) return $val ? $val->format('d.m.Y') : null;
elseif ($field->type === FieldType::TIME) return $val ? $val->format('H:i') : null;
elseif ($field->type === FieldType::DATETIME) return $val ? $val->format('d.m.Y H:i') : null;
else return $val->caption ?? $val->title ?? $val->name ?? $val;
})->implode('; ');
return null;
}
public function getAttachedFilesAttribute(): Collection {
$result = collect();
$this->properties->each(function($group) use(&$result) {
$group->fields->each(function($field) use(&$result) {
if ($field->isFile) $this->getValue($field->name)->each(function($asset) use(&$result) {$result->push($asset);});
});
});
return $result;
}
public function setValues(array $values): Collection { public function setValues(array $values): Collection {
return collect($values)->map(function($value, $fieldName) { return collect($values)->map(function($value, $fieldName) {
return $this->setValue($fieldName, $value); return $this->setValue($fieldName, $value);

View File

@ -4,12 +4,12 @@ namespace App\Providers;
use App\Events\Applications\ApplicationExpertChanged; use App\Events\Applications\ApplicationExpertChanged;
use App\Events\Applications\ApplicationStatusChanged; use App\Events\Applications\ApplicationStatusChanged;
use App\Events\FeedbackSender; use App\Events\Feedback\FeedbackFormSubmitted;
use App\Events\PasswordRecovered; use App\Events\PasswordRecovered;
use App\Events\UserRegistered; use App\Events\UserRegistered;
use App\Listeners\Applications\SendApplicationExpertChangedNotifications; use App\Listeners\Applications\SendApplicationExpertChangedNotifications;
use App\Listeners\Applications\SendApplicationStatusChangedNotifications; use App\Listeners\Applications\SendApplicationStatusChangedNotifications;
use App\Listeners\SendFeedbackMessage; use App\Listeners\Feedback\SendFeedbackFormNotifications;
use App\Listeners\SendPasswordRecoveredNotification; use App\Listeners\SendPasswordRecoveredNotification;
use App\Listeners\SendRegistrationNotification; use App\Listeners\SendRegistrationNotification;
use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Events\Registered;
@ -27,8 +27,8 @@ class EventServiceProvider extends ServiceProvider {
PasswordRecovered::class => [ PasswordRecovered::class => [
SendPasswordRecoveredNotification::class SendPasswordRecoveredNotification::class
], ],
FeedbackSender::class => [ FeedbackFormSubmitted::class => [
SendFeedbackMessage::class SendFeedbackFormNotifications::class
], ],
ApplicationStatusChanged::class => [ ApplicationStatusChanged::class => [

View File

@ -2,32 +2,20 @@
namespace App\Services\Forms\Feedback; namespace App\Services\Forms\Feedback;
use App\Events\FeedbackSender; use App\Events\Feedback\FeedbackFormSubmitted;
use App\Models\Objects\NirObject; use App\Models\Objects\ObjectType;
use App\Services\Forms\FormsService; use App\Services\Forms\FormsService;
use App\Transformers\Objects\ObjectTransformer;
use Illuminate\Http\JsonResponse;
class FeedbackForms extends FormsService { class FeedbackForms extends FormsService {
public array $formTitles = ['create' => 'Создание операции', 'update' => 'Редактирование операции']; public function store(array $data): ?JsonResponse {
if ($objectType = ObjectType::byUuid($data['type'] ?? null)->first()) {
public function form(?string $id = null, array $data = []): array { $object = $objectType->objects()->create();
$model = NirObject::byUuid($id)->first(); $object->setValues($data);
$groups = [ if (!empty($data['mailto'])) event(new FeedbackFormSubmitted($object, $data['mailto']));
['name' => 'common', 'fields' => $this->commonGroupFields($model)] return fractal($object, new ObjectTransformer())->respond();
];
return ['title' => $this->formTitle($model), 'data' => $groups];
} }
public function commonGroupFields(?NirObject $model): array {
return [];
}
public function store(array $data) {
event(new FeedbackSender($data['mailto'], $data));
return null;
}
public function update(string $id, array $data) {
return null; return null;
} }
} }

View File

@ -0,0 +1,10 @@
@extends('mail.layouts.layout')
@section('content')
<p>Поступило обращение с сайта {{env('APP_URL')}}.</p>
@foreach($object->properties as $group)
@foreach($group->fields as $field)
@if(!$field->isFile)<p>{{$field->title}}: {{$object->parsedValue($field->name)}}</p>@endif
@endforeach
@endforeach
@endsection

View File

@ -1,8 +0,0 @@
@extends('mail.layouts.layout')
@section('content')
<p>Поступило обращение с сайта {{env('APP_URL')}}.</p>
<p class="mb-10">Email отправителя: {{$data['feedback-email'] ?? '-'}}</p>
<p class="mb-10">Имя отправителя: {{$data['feedback-name'] ?? '-'}}</p>
<p class="mb-10">Сообщение: {!! nl2br($data['feedback-message'] ?? '-') !!}</p>
@endsection