File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/KommaApp/Blocks/BlockService.php
<?php
namespace KommaApp\Blocks;
use KommaApp\Blocks\Models\Block;
use KommaApp\Blocks\Models\BlockTranslation;
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
class BlockService
{
public function getBlocksIn($field, $values, $trans = true)
{
$blocks = Block::where('active', '=', 1)
->whereIn($field, $values)
->with('translations')
->get()
->keyBy($field);
if (!$trans) return $blocks;
foreach ($blocks as $key => $block) {
$blocks[$key] = $this->prepareBlockTranslation($block);
}
return $blocks;
}
public function getBlocksByCodeName($code_names)
{
return $this->getBlocksIn('code_name', $code_names);
}
private function prepareBlockTranslation($block)
{
$trans = $block->translations->keyBy('language_id');
if(!isset($trans[\Shop::getLanguageService()->getCurrentLanguageId()])) return;
$trans = $trans[\Shop::getLanguageService()->getCurrentLanguageId()];
foreach ($trans->getAttributes() as $key => $value) {
$block->{'trans_'.$key} = $value;
}
return $block;
}
}