HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/app/Trainings/TrainingApiController.php
<?php


namespace App\Trainings;


use App\Types\TrainingFilter;
use Illuminate\Pagination\LengthAwarePaginator;

final class TrainingApiController
{
    const AMOUNT_EACH_PAGE = 30;


    private TrainingService $trainingService;

    public function __construct()
    {
        $this->trainingService = app()->make(TrainingService::class);
    }

    /**
     * Get the filters for the sidebar
     *
     * @param  bool  $asJson
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Support\Collection
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function filters($asJson = true)
    {
        $filters = collect();

        // Get the filters out of the training model attributes
        foreach ($this->trainingService->getFilters() as $filter) {
            if(in_array($filter->type, [TrainingFilter::FILTER_TYPE_CHECKBOXES, TrainingFilter::FILTER_TYPE_LEVEL])) $filter->selectedOptions = [];
            $filters->push($filter);
        }

        if(!$asJson) return $filters;
        return response()->json($filters);
    }

    public function list()
    {
        $filters = request()->all();

        if(sizeof($filters) == 0) $results = $this->trainingService->getUpcomingTrainings(self::AMOUNT_EACH_PAGE);
        else $results = $this->trainingService->getTrainingForFilters($filters);

        foreach ($results as $result) $result->formatStartDate();

        $page = request()->get('page', 1);
        $paginator = new LengthAwarePaginator($results, $results->count(), self::AMOUNT_EACH_PAGE, $page);

        $offset = 0;
        if($page > 1) $offset = ($page - 1) * self::AMOUNT_EACH_PAGE;

        debug($paginator);
        debug($page);
        debug($paginator->slice($offset, self::AMOUNT_EACH_PAGE));
        debug($paginator->lastPage());

        return response()->json([
            'items' => $paginator->slice($offset, self::AMOUNT_EACH_PAGE),
            'current_page' => $page,
            'more_page' => $page < $paginator->lastPage()
        ])->withHeaders([
            'Access-Control-Allow-Origin' => '*',
        ]);

    }

//    public function mockC4Response()
//    {
//
//        $response = [];
//        $steLanguages = SteLanguage::whereNotNull('iso_2')->get();
//        $locations = Location::where('active', 1)->where('lft', '!=', '1')->get();
//
//        for($i = 0; $i < 20; $i++) {
//
//            $randomDate = Carbon::create(rand(2020, 2021), rand(today()->format('n'), 12), rand(1, 28));
//            $amountOfWeeks = [15, 24, 30];
//            $response[] = (object) [
//                'id' => 'training-' . rand(244, 1200),
//                'language' => $steLanguages->random()->iso_2,
//                'level' => Training::$levels[rand(0, (sizeof(Training::$levels) - 1))],
//                'training_type' => rand(0, 4),
//                'location' => $locations->random()->id,
//                'price' => rand(40, 1000),
//                'duration' => rand(1,3),
//                'amount_of_weeks' => $amountOfWeeks[rand(0,2)],
//                'start_date' => $randomDate->format('d-m-Y'),
//                'start_time' => rand(9,10) . ':' . rand(10, 59)
//            ];
//        }
//
//        $response[] = (object) [
//            'id' => 'training-123',
//            'language' => 'nl',
//            'level' => 'A1',
//            'training_type' => rand(0, 4),
//            'location' => $locations->random()->id,
//            'price' => rand(40, 1000),
//            'duration' => rand(1,3),
//            'amount_of_weeks' => 15,
//            'start_date' => today()->addDay()->format('d-m-Y'),
//            'start_time' => rand(9,10) . ':' . rand(10, 59)
//        ];
//
//
//        return response()->json($response);
//    }

}