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/umans.komma.pro/app/Komma/Blocks/BlockService.php
<?php

namespace Komma\Blocks;

use Illuminate\Support\Collection;
use Komma\Blocks\Models\Block;
use Komma\Images\ImageService;


/**
 *
 * @author      Komma <support@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */
class BlockService
{
    private $imageService;

    /**
     * Inject the dependencies.
     * Ex. the imageService.
     *
     * BlockService constructor.
     * @param ImageService $imageService
     */
    public function __construct(ImageService $imageService)
    {
        $this->imageService = $imageService;
    }

    /**
     * This method gets the blocks with an IN
     *
     * @param $field | string, field for the in
     * @param $values | array, values for the in
     * @return mixed
     */
    public function getBlocksIn($field, $values)
    {
        //Only get the active blocks
        $blocks = Block::where('active', '=', 1)
            //Where in with field and values
            ->whereIn($field, $values)
            //Also load the translation
            ->with('translation')
            ->with('images')
            ->get()
            ->keyBy($field);

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

        return $blocks;
    }


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

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

        $objectBlocks = [];

        foreach ($blocks as $block){
            if(!isset($block->translation)) continue;
            $objectBlocks[$block->code_name] = $block;
        }

        return (object)$objectBlocks;
    }

    /**
     * This method invokes the getBlocksWhere
     * With code_name as field
     *
     * @param $code_names
     * @return mixed
     */
    public function getBlocksByCodeName($code_name)
    {
        return $this->getBlocksWhere('code_name', $code_name);
    }

    /**
     * This method invokes the getBlocksIn
     * With id as field
     *
     * @param $ids
     * @return mixed
     */
    public function getBlocksById($ids)
    {
        return $this->getBlocksIn('id', $ids);
    }

}