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/Gideons/GideonService.php
<?php

/**
 * Short description for the file.
 *
 * @author      Komma <support@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Gideons;

use Carbon\Carbon;
use Komma\Kms\Languages\Language;
use Komma\Gideons\Models\Gideon;

class GideonService
{

    private $today;

    public function __construct()
    {
        $this->today = Carbon::now()->addHour();
        //Detect daylight saving time
        if(Carbon::now()->format('I') == 0) $this->today->addHour();
        $this->today = $this->today->format('Y-m-d H:i:s');
    }

    public function getAllGideons($pagination = false, $itemsPerPage = 9, $ids = null, $onlyHighlighted = false)
    {

//        $gideons = Gideon::where('lft', '!=', 1)
//            ->with('translation')
//            ->with('translation.route')
//            ->with('images')
//            ->where('active', '=', 1)
//            ->where('date', '<=', $this->today);
//
//        $gideons->orderBy('date', 'desc');

        // Get pages with translations and routes

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

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

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

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

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

        return $gideons;

    }

    public function getLatestGideons($items = 5)
    {

        $gideons = Gideon::with('translation')
            ->with('translation.route')
            ->where('active', '=', 1)
            ->where('order_date', '>=', $this->today);

        $gideons->orderBy('order_date', 'asc');
        $gideons->limit($items);
        $gideons = $gideons->get();

        return $gideons;

    }


    public function getGideon($id, $nextAndPrev = true)
    {
        if( ! $gideon = Gideon::where('id', '=', $id)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->where('active', '=', 1)
            ->first()
        ) return \App::abort(404, 'gideon not found');

        $this->fillGideon($gideon);

//        if($nextAndPrev) $nextAndPrev = $this->getNextAndPrevious($gideon);

        return $gideon;
    }

    public function fillGideon(Gideon &$gideon)
    {
//        $gideon->translation->meta_description = str_limit(strip_tags($gideon->translation->description, 155));

        //Comment the decode when there aren't dynamic blocks
        $gideon->translation->description = json_decode($gideon->translation->description);

    }


    public function getNextAndPrevious(&$gideon)
    {
        $previous = \DB::table('gideons')
            ->leftJoin('gideon_translations', function ($join)
            {
                $join->on('gideon_translations.gideon_id', '=', 'gideons.id')
                    ->where('language_id', '=', Language::where('languages.iso_2', '=', \App::getLocale())->first()->id);

            })
            ->where('languages.iso_2', '=', \App::getLocale())
            ->join('languages', 'languages.id', '=', 'gideon_translations.language_id')
            ->leftJoin('routes', function ($join)
            {
                $join->on('gideon_translations.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', 'Komma\Kms\Gideons\Models\GideonTranslation');
            })
            ->leftJoin('images', function ($join)
            {
                $join->on('images.imageble_id', '=', 'gideons.id')
                    ->where('images.imageble_type', '=', 'Komma\Kms\Gideons\Models\Gideon')
                    ->where('sort_order', '=', 1);
            })
            ->select('gideons.*', 'gideon_translations.*', 'routes.route')
            ->where('gideons.active', '=', 1)
            ->where('gideons.id', '!=', $gideon->id)
            ->orderBy('gideons.order_date', 'desc')
            ->first();

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

            })
            ->where('languages.iso_2', '=', \App::getLocale())
            ->join('languages', 'languages.id', '=', 'gideon_translations.language_id')
            ->leftJoin('routes', function ($join)
            {
                $join->on('gideon_translations.id', '=', 'routes.routable_id')
                    ->where('routes.routable_type', '=', 'Komma\Kms\Gideons\Models\GideonTranslation');
            })
            ->leftJoin('images', function ($join)
            {
                $join->on('images.imageble_id', '=', 'gideons.id')
                    ->where('images.imageble_type', '=', 'Komma\Kms\Gideons\Models\Gideon')
                    ->where('sort_order', '=', 1);
            })
            ->select('gideons.*', 'gideon_translations.*', 'routes.route')
            ->where('gideons.active', '=', 1)
            ->where('gideons.order_date', '>=', $this->today)
            ->where('gideons.order_date', '<', $gideon->date)
            ->where('gideons.id', '!=', $gideon->id)
            ->orderBy('gideons.order_date', 'asc')
            ->first();

//        $previous = Gideon::where('date', '>', $activitie->date)->orderBy('date', 'asc')
//            ->with('translation')
//            ->with('translation.route')
//            ->with('images')
//            ->where('active', '=', 1)
//            ->where('date', '<=', $this->today)
//            ->where('id', '!=', $activitie->id)
//            ->first();
//        $next = Gideon::where('date', '<', $activitie->date)->orderBy('date', 'desc')
//            ->with('translation')
//            ->with('translation.route')
//            ->with('images')
//            ->where('active', '=', 1)
//            ->where('date', '<=', $this->today)
//            ->where('id', '!=', $activitie->id)
//            ->first();

        $gideon->next = $next;
        $gideon->previous = $previous;
    }

    /**
     * Convert activitie date to carbon
     *
     * @param $gideons
     */
    public function makeCarbonDate(&$gideons)
    {

        foreach ($gideons as $gideon)
        {
            $date = $gideon->order_date;
            $gideon->date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
        }
    }

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

        $gideonsArray = [];

        foreach ($gideons as $gideon)
        {

            $gideonDates = [];
            $dates = json_decode($gideon->date);

            foreach ($dates as $date){
                if(empty($date)) continue;
                $gideonDates[] = Carbon::createFromFormat('d-m-Y', $date);
            }

            $gideon->dates = $gideonDates;

            foreach ($gideon->dates as $date){

                $cloneGideon = clone $gideon;
                $cloneGideon->date = $date;
                $gideonsArray[$date->timestamp.$cloneGideon->name] = $cloneGideon;
            }
        }

        krsort($gideonsArray);

        return $gideonsArray;
    }

}