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/SBogers110/franciscaansebeweging.nl/app/Komma/Travels/TravelService.php
<?php


namespace Komma\Travels;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Komma\Images\ImageService;
use Komma\Kms\Languages\Language;
use Komma\Travels\Models\Travel;

class TravelService
{

    public function getAllTravels($pagination = false, $itemsPerPage = 3, $ids = null, $onlyHighlighted = false)
    {

        // Get pages with translations and routes

        $travels = \DB::table('travels')
            ->leftJoin('travel_translations', function ($join) {
                $join->on('travel_translations.travel_id', '=', 'travels.id')
                    ->where('language_id', '=', Language::where('languages.iso_2', '=', \App::getLocale())->first()->id);

            })
            ->where('languages.iso_2', '=', \App::getLocale())
            ->join('languages', 'languages.id', '=', 'travel_translations.language_id')
            ->leftJoin('images', function ($join) {
                $join->on('images.imageble_id', '=', 'travels.id')
                    ->where('images.imageble_type', '=', 'Komma\Kms\Travels\Models\Travel')
                    ->where('sort_order', '=', 1)
                    ->where('sort_order', '=', 1);
            })
            ->leftJoin('routes', function ($join) {
                $join->on('travel_translations.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', 'Komma\Kms\Travels\Models\TravelTranslation');
            })
            ->select('travels.*', 'travel_translations.*', 'routes.route', 'images.medium_image_url')
            ->where('travels.active', '=', 1);

        if ($ids !== null) {
            $travels = $travels->whereIn('travels.id', $ids);
        }

        if ($onlyHighlighted === true) {
            $travels = $travels->where('travels.highlighted', 1);
        }

        if ($pagination) {
            $travels = $travels->paginate($itemsPerPage);
        } else {
            $travels = $travels->get();
        }

        return $travels;

    }

    /**
     * Get specific travel
     * with this id and current language
     *
     * @param $id
     * @return mixed
     */
    public function getTravel($id)
    {
        if (!$travel = Travel::where('id', '=', $id)
            ->where('active', 1)
            ->with([
                'images',
                'translation',
                'translation.route',
            ])
            ->first()) \App::abort(404, 'Page not found');

        $this->fillTravel($travel);

        return $travel;
    }


    /**
     * Get all travels by travel_type (page id)
     *
     * @param $typeId
     * @return Collection
     */
    public function getTravelByType($type)
    {

        $travels = Travel::where('travel_type', $type)
            ->where('active', 1)
            ->with([
                'images',
                'translation',
                'translation.route',
            ])->get();

        return $travels;

    }


    /**
     * Fill / populate Travel
     *
     * @param Travel $travel
     */
    public function fillTravel(Travel &$travel)
    {


        // Populate dynamic content to translation description
        if (isset($travel->translation->description) && $travel->translation->description != '[]') {
            $travel->translation->description = json_decode($travel->translation->description);
        }
    }

    /**
     * Convert for sorting
     *
     * @param $activities
     */
    public function convertForSorting($activities)
    {

        $activitiesArray = [];

        foreach ($activities as $activity) {
            $createDate = new \DateTime( $activity->start_date );
            $strippedDate = $createDate->format('d-m-Y');

            $newDate = Carbon::createFromFormat('d-m-Y', $strippedDate);

            $cloneActivity = clone $activity;
            $cloneActivity->date = $newDate;
            if ($newDate >= Carbon::today()) $activitiesArray[$newDate . $cloneActivity->name] = $cloneActivity;
        }

        ksort($activitiesArray);

        return $activitiesArray;
    }

}