File: D:/HostingSpaces/SBogers33/bbec.nl/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);
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();
$objectBlocks = [];
foreach ($blocks as $block){
if(!isset($block->translation)) continue;
$objectBlocks[$block->code_name] = $block;
}
return (object)$objectBlocks;
}
public function getContent($code_name){
//Only get the active blocks
$content = Block::where('active', '=', 1)
//Where field is value
->where('code_name', $code_name)
//Also load the translation
->with('translation')
->first();
$contentObject = (object)[];
// Populate dynamic content to translation description
if(isset($content->translation->description) && $content->translation->description != '[]'){
$dynamic = json_decode($content->translation->description);
// Loop through content to key by code_name if isset else get the type
// If you don't do this, you can't access an specific attribute
$orderedContent = [];
$slider = [];
foreach ($dynamic as $key => $partial){
if(isset($partial->code_name) && $partial->code_name != ''){
$contentObject->{$partial->code_name} = $partial;
}
elseif($partial->view == 'slider'){
$slider[] = $partial;
}
else{
$orderedContent[$key] = $partial;
}
}
$contentObject->slider = $slider;
$contentObject->dynamic = $orderedContent;
}
return $contentObject;
}
/**
* 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);
}
}