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