File: D:/HostingSpaces/SBogers10/ridderstee.komma.pro/app/Komma/Houses/HouseService.php
<?php
/**
* Short description for the file.
*
* @author Komma <support@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace Komma\Houses;
use Illuminate\Support\Collection;
use Komma\Images\ImageService;
use Komma\Pages\PageService;
use Komma\Houses\Models\House;
class HouseService
{
protected $imageService;
protected $pageService;
public function __construct(ImageService $imageService, PageService $pageService)
{
$this->imageService = $imageService;
$this->pageService = $pageService;
}
/**
* Get House by id
*
* @param $id
* @return mixed
*/
public function getHouse($id)
{
if(!$house = House::where('id', '=', $id)
->with('translation')
->with('translation.route')
->with('images')
->with('house_images')
->where('active', '=', 1)
->first()) return \App::abort(404, 'house not found');
$this->fillHouse($house);
return $house;
}
/**
* Fill additional content to house
*
* @param House $house
*/
protected function fillHouse(House &$house){
//Comment the decode when there aren't dynamic blocks
// $house->translation->description = json_decode($house->translation->description);
$house->routeInOtherLanguages = $this->houseInOtherLanguage($house->id)->allTranslations;
$house->meta_description = strip_tags($house->meta_description);
$metaParent = $this->pageService->getMetaData('houses');
$house->translation->meta_title = $house->translation->name.' | '.$metaParent->meta_title;
}
public function getRelativeHouses(House &$house, Collection $houses){
$otherSizes = [];
foreach($houses as $houseFilter){
if($houseFilter->amount == $house->amount && $house->id != $houseFilter->id) $house->otherType = $houseFilter;
if($houseFilter->type == $house->type && $house->id != $houseFilter->id) $otherSizes[] = $houseFilter;
}
$house->otherSizes = $otherSizes;
}
/**
* Get all houses
*
* @param bool $pagination
* @param int $itemsPerPage
* @return mixed
*/
public function getAllHouses($pagination = false, $itemsPerPage = 10)
{
$houses = House::with('translation')
->with('translation.route')
->where('active', '=', 1)
->orderBy('amount')
->orderBy('type');
if ($pagination) {
$houses = $houses->paginate($itemsPerPage);
} else {
$houses = $houses->get();
foreach ($houses as $key =>$house){
if(!isset($house->translation)) $houses->forget($key);
}
}
return $houses;
}
/**
* This method gets the houses where
*
* @param $field | string, field for the where
* @param $values | array, value for the where
* @return mixed
*/
public function getHousesWhere($field, $values)
{
//Only get the active blocks
$houses = House::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 ($houses as &$content){
if(!$content->images->count()) continue;
$content->images = $this->imageService->orderImagesOnLanguage($content->images);
}
foreach ($houses as $key =>$house){
if(!isset($house->translation)) $houses->forget($key);
}
return $houses;
}
/**
* Get all translations of an page
* based upon this page id
*
* @param $page_id
* @return mixed
*/
public function houseInOtherLanguage($id){
return House::where('id', '=', $id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
}