add stickers logic
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Reports;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Requests\Reports\Stickers\StickerFormDataRequest;
|
||||
use App\Http\Requests\Reports\Stickers\StickerPrintRequest;
|
||||
use App\Http\Requests\Reports\Stickers\StickerStudentsByClassRequest;
|
||||
use App\Http\Resources\Reports\Stickers\StickerClassSectionResource;
|
||||
use App\Http\Resources\Reports\Stickers\StickerPresetResource;
|
||||
use App\Http\Resources\Reports\Stickers\StickerStudentResource;
|
||||
use App\Services\Reports\Stickers\StickerPresetService;
|
||||
use App\Services\Reports\Stickers\StickerPrintService;
|
||||
use App\Services\Reports\Stickers\StickerQueryService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class StickersController extends BaseApiController
|
||||
{
|
||||
public function __construct(
|
||||
private StickerQueryService $queryService,
|
||||
private StickerPresetService $presetService,
|
||||
private StickerPrintService $printService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function formData(StickerFormDataRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
$schoolYear = $this->queryService->resolveSchoolYear($payload['school_year'] ?? null);
|
||||
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
|
||||
|
||||
$classes = $this->queryService->listClassesWithStudents($schoolYear);
|
||||
$students = $classSectionId > 0
|
||||
? $this->queryService->listStudentsByClass($classSectionId, $schoolYear)
|
||||
: $this->queryService->listStudentsForYear($schoolYear);
|
||||
|
||||
$presets = $this->presetService->presets();
|
||||
|
||||
return $this->success([
|
||||
'classes' => StickerClassSectionResource::collection($classes),
|
||||
'students' => StickerStudentResource::collection($students),
|
||||
'presets' => StickerPresetResource::collection($presets),
|
||||
]);
|
||||
}
|
||||
|
||||
public function studentsByClass(StickerStudentsByClassRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
$schoolYear = $this->queryService->resolveSchoolYear($payload['school_year'] ?? null);
|
||||
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
|
||||
|
||||
$students = $this->queryService->listStudentsByClass($classSectionId, $schoolYear);
|
||||
|
||||
return $this->success([
|
||||
'students' => StickerStudentResource::collection($students),
|
||||
]);
|
||||
}
|
||||
|
||||
public function print(StickerPrintRequest $request)
|
||||
{
|
||||
try {
|
||||
$result = $this->printService->generate($request->validated());
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Sticker print failed: ' . $e->getMessage());
|
||||
return $this->error('Failed to generate stickers.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $this->error($result['message'] ?? 'Unable to generate stickers.', Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
return response($result['pdf'], 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . ($result['filename'] ?? 'Student_Stickers.pdf') . '"',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\Stickers;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class StickerFormDataRequest extends ApiFormRequest
|
||||
{
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
parent::prepareForValidation();
|
||||
|
||||
if (!$this->has('class_section_id') && $this->has('class_id')) {
|
||||
$this->merge(['class_section_id' => $this->input('class_id')]);
|
||||
}
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'class_section_id' => ['nullable', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\Stickers;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
class StickerPrintRequest extends ApiFormRequest
|
||||
{
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
parent::prepareForValidation();
|
||||
|
||||
if (!$this->has('class_section_id') && $this->has('class_id')) {
|
||||
$this->merge(['class_section_id' => $this->input('class_id')]);
|
||||
}
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'student_id' => ['nullable', 'integer', 'min:1'],
|
||||
'class_section_id' => ['nullable', 'integer', 'min:1'],
|
||||
'print_all' => ['nullable', 'boolean'],
|
||||
'sticker_size' => ['nullable', 'string', 'max:20'],
|
||||
'sticker_width' => ['nullable', 'numeric', 'min:1'],
|
||||
'sticker_height' => ['nullable', 'numeric', 'min:1'],
|
||||
'stickers_per_page' => ['nullable', 'integer', 'min:1'],
|
||||
'page_size' => ['nullable', 'string', 'max:20'],
|
||||
'orientation' => ['nullable', 'string', 'max:2'],
|
||||
'margin_left' => ['nullable', 'numeric', 'min:0'],
|
||||
'margin_top' => ['nullable', 'numeric', 'min:0'],
|
||||
'margin_right' => ['nullable', 'numeric', 'min:0'],
|
||||
'margin_bottom' => ['nullable', 'numeric', 'min:0'],
|
||||
'gap_x' => ['nullable', 'numeric', 'min:0'],
|
||||
'gap_y' => ['nullable', 'numeric', 'min:0'],
|
||||
'copies' => ['nullable', 'array'],
|
||||
'copies.*' => ['integer', 'min:0'],
|
||||
'single_copies' => ['nullable', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator(Validator $validator): void
|
||||
{
|
||||
$validator->after(function (Validator $validator) {
|
||||
$hasStudent = $this->filled('student_id');
|
||||
$hasClass = $this->filled('class_section_id');
|
||||
$printAll = (bool) $this->input('print_all', false);
|
||||
|
||||
if (!$hasStudent && !$hasClass && !$printAll) {
|
||||
$validator->errors()->add('selection', 'Please select a student or class, or set print_all.');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\Stickers;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class StickerStudentsByClassRequest extends ApiFormRequest
|
||||
{
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
parent::prepareForValidation();
|
||||
|
||||
if (!$this->has('class_section_id') && $this->has('class_id')) {
|
||||
$this->merge(['class_section_id' => $this->input('class_id')]);
|
||||
}
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => ['required', 'integer', 'min:1'],
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\Stickers;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class StickerClassSectionResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => (int) ($this['class_section_id'] ?? 0),
|
||||
'class_section_name' => (string) ($this['class_section_name'] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\Stickers;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class StickerPresetResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'value' => (string) ($this['value'] ?? ''),
|
||||
'label' => (string) ($this['label'] ?? ''),
|
||||
'title' => (string) ($this['title'] ?? ''),
|
||||
'text' => (string) ($this['text'] ?? ''),
|
||||
'per_page' => $this['per_page'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\Stickers;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class StickerStudentResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) ($this['id'] ?? 0),
|
||||
'firstname' => (string) ($this['firstname'] ?? ''),
|
||||
'lastname' => (string) ($this['lastname'] ?? ''),
|
||||
'registration_grade' => $this['registration_grade'] ?? null,
|
||||
'gender' => $this['gender'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user