siakad-itm/app/Services/FeedbackService.php
Yoga Pangestu 62f8c167e8
Some checks failed
tests / ci (pull_request) Has been cancelled
feat: implement feedback system with CRUD functionality
- Add FeedbackController to handle feedback submissions and management.
- Create Feedback model and migration for feedbacks table.
- Introduce FeedbackStatus and FeedbackType enums for better type handling.
- Implement FeedbackService for business logic related to feedback.
- Create FeedbackRequest for validation of feedback data.
- Add Tiptap rich text editor for feedback message input.
- Develop frontend components for displaying and managing feedback.
- Add routes for feedback management in web.php.
- Create utility types for feedback in TypeScript.
- Update app-sidebar to include feedback navigation.
2026-08-25 14:25:50 +07:00

77 lines
2.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\Feedback;
use App\Models\User;
use App\Support\TextAlignAttributeSanitizer;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
class FeedbackService
{
private readonly HtmlSanitizer $sanitizer;
public function __construct()
{
$config = (new HtmlSanitizerConfig)
->allowElement('p', ['style'])
->allowElement('h2', ['style'])
->allowElement('h3', ['style'])
->allowElement('strong')
->allowElement('b')
->allowElement('em')
->allowElement('i')
->allowElement('u')
->allowElement('s')
->allowElement('strike')
->allowElement('ul')
->allowElement('ol')
->allowElement('li')
->allowElement('blockquote')
->allowElement('br')
->allowElement('a', ['href'])
->allowElement('img', ['src', 'alt'])
->allowLinkSchemes(['http', 'https', 'mailto'])
->allowMediaSchemes(['https'])
->withAttributeSanitizer(new TextAlignAttributeSanitizer);
$this->sanitizer = new HtmlSanitizer($config);
}
public function paginated(User $user, int $perPage = 25, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator
{
return Feedback::query()
->where('user_id', $user->id)
->when($search, fn ($q) => $q->where('subject', 'like', "%{$search}%"))
->orderBy($sort, $direction)
->paginate($perPage);
}
public function create(User $user, array $data): Feedback
{
return Feedback::create([
'user_id' => $user->id,
'type' => $data['type'],
'subject' => $data['subject'],
'message' => $this->sanitizer->sanitize($data['message']),
]);
}
public function update(Feedback $feedback, array $data): Feedback
{
$feedback->type = $data['type'];
$feedback->subject = $data['subject'];
$feedback->message = $this->sanitizer->sanitize($data['message']);
$feedback->update();
return $feedback;
}
public function delete(Feedback $feedback): bool
{
return $feedback->delete();
}
}