File: D:/HostingSpaces/jacques-hein/jacques-hein.nl/app/Komma/Services/ServiceService.php
<?php
/**
* Short description for the file.
*
* @author Komma <support@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace Komma\Services;
use Komma\Images\ImageService;
use Komma\Pages\PageService;
use Komma\Projects\Models\Project;
use Komma\Services\Models\Service;
class ServiceService
{
protected $imageService;
protected $pageService;
public function __construct(ImageService $imageService, PageService $pageService)
{
$this->imageService = $imageService;
$this->pageService = $pageService;
}
/**
* Get Project by id
*
* @param $id
* @return mixed
*/
public function getService($id)
{
if(!$service = Service::where('id', '=', $id)
->with('translation')
->with('translation.route')
->with('images')
->where('active', '=', 1)
->first()) return \App::abort(404, 'project not found');
if($service->images->count() != 0) $service->images = $this->imageService->orderImagesOnLanguage($service->images);
$this->fillService($service);
return $service;
}
/**
* Fill additional content to project
*
* @param Project $project
*/
protected function fillService(Service &$service){
$service->translation->description = json_decode($service->translation->description);
$service->routeInOtherLanguages = $this->serviceInOtherLanguage($service->id)->allTranslations;
$metaParent = $this->pageService->getMetaData('projects');
$service->translation->meta_title = $service->translation->name.' | '.$metaParent->meta_title;
}
/**
* Get all projects
*
* @param bool $pagination
* @param int $itemsPerPage
* @return mixed
*/
public function getAllServices($pagination = false, $itemsPerPage = 10)
{
$services = Service::where('lft', '!=', 1)
->with('translation')
->with('translation.route')
->where('active', '=', 1)
->orderBy('lft');
if ($pagination) {
$services = $services->paginate($itemsPerPage);
} else {
$services = $services->get();
foreach ($services as $key =>$service){
if(!isset($service->translation)) $services->forget($key);
}
}
return $services;
}
/**
* This method gets the projects where
*
* @param $field | string, field for the where
* @param $values | array, value for the where
* @return mixed
*/
public function getServicesWhere($field, $values)
{
//Only get the active blocks
$services = Service::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 ($services as &$content){
if(!$content->images->count()) continue;
$content->images = $this->imageService->orderImagesOnLanguage($content->images);
}
foreach ($services as $key =>$service){
if(!isset($service->translation)) $services->forget($key);
}
return $services;
}
/**
* Get all translations of an page
* based upon this page id
*
* @param $page_id
* @return mixed
*/
public function serviceInOtherLanguage($id){
return Service::where('id', '=', $id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
}