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/SBogers33/bbec.nl/app/Komma/References/ReferenceService.php
<?php

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

namespace Komma\References;

use Komma\Images\ImageService;
use Komma\Pages\PageService;
use Komma\References\Models\Reference;

class ReferenceService
{
    protected $imageService;
    protected $pageService;

    public function __construct(ImageService $imageService, PageService $pageService)
    {
        $this->imageService = $imageService;
        $this->pageService = $pageService;
    }

    /**
     * Get Reference by id
     *
     * @param $id
     * @return mixed
     */
    public function getReference($id)
    {
        if(!$reference = Reference::where('id', '=', $id)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->where('active', '=', 1)
            ->first()) return \App::abort(404, 'reference not found');

//        if($reference->images->count() != 0) $reference->images = $this->imageService->orderImagesOnLanguage($reference->images);

        $this->fillReference($reference);

        return $reference;
    }

    /**
     * Fill additional content to reference
     *
     * @param Reference $reference
     */
    protected function fillReference(Reference &$reference){

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


        $reference->routeInOtherLanguages = $this->referenceInOtherLanguage($reference->id)->allTranslations;

        $reference->meta_description = strip_tags($reference->translation->meta_description);
    }


    /**
     * Get all references
     *
     * @param bool $pagination
     * @param int $itemsPerPage
     * @return mixed
     */
    public function getAllReferences($pagination = false, $itemsPerPage = 10)
    {

        $references = Reference::where('lft', '!=', 1)
            ->with('translation')
            ->with('translation.route')
            ->where('active', '=', 1)
            ->orderBy('lft');

            $references = $references->get();
            foreach ($references as $key =>$reference){
                if(!isset($reference->translation)) $references->forget($key);
            }

        return $references;

    }



    /**
     * This method gets the references where
     *
     * @param $field | string, field for the where
     * @param $values | array, value for the where
     * @return mixed
     */
    public function getReferencesWhere($field, $values)
    {
        //Only get the active blocks
        $references = Reference::where('active', '=', 1)
            //Where field is value
            ->where('lft', '!=', 1)
            ->where($field, '=', $values)
            //Also load the translation
            ->with('translation')
            ->with('images')
            ->orderBy('lft')
            ->get();

        foreach ($references as &$content){
            if(!$content->images->count()) continue;
            $content->images = $this->imageService->orderImagesOnLanguage($content->images);
        }

        foreach ($references as $key =>$reference){
            if(!isset($reference->translation)) $references->forget($key);
        }

        return $references;
    }

    /**
     * Get all translations of an page
     * based upon this page id
     *
     * @param $page_id
     * @return mixed
     */
    public function referenceInOtherLanguage($id){
        return Reference::where('id', '=', $id)
            ->with('allTranslations')
            ->with('allTranslations.route')
            ->first();
    }

}