File: D:/HostingSpaces/SBogers60/vandeurzenheftrucks.nl/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 false;
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){
//Comment the decode when there aren't dynamic blocks
$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;
}
public function getAllProjectsById($pagination = false, $itemsPerPage = 10, $projectIds)
{
$projects = Project::whereIn('id', $projectIds)
->with('translation')
->with('translation.route')
->where('active', '=', 1)
->orderBy('lifting_capacity','asc');
if ($pagination) {
$projects = $projects->paginate($itemsPerPage);
} else {
$projects = $projects->get();
foreach ($projects as $key =>$project){
if(!isset($project->translation)) $projects->forget($key);
}
}
return $projects;
}
public function getAllProjectsByIdExceptCurrent($pagination = false, $itemsPerPage = 10, $projectIds, $currentProjectId)
{
$currentProject = $this->getProject($currentProjectId);
$currentLiftingCapacity = $currentProject->lifting_capacity;
$projects = Project::whereIn('id', $projectIds)
->where('id', '!=', $currentProjectId)
->with('translation')
->with('translation.route')
->where('active', '=', 1)
->orderBy(\DB::raw('ABS(lifting_capacity - '.(float)$currentLiftingCapacity.')'),'asc' );
if ($pagination) {
$projects = $projects->paginate($itemsPerPage);
} else {
$projects = $projects->get();
foreach ($projects as $key =>$project){
if(!isset($project->translation)) $projects->forget($key);
}
}
return $projects;
}
public function getAllProjectsInCategory($pagination = false, $itemsPerPage = 10, $categoryId)
{
// $projects = \DB::table('project_categories')->where('id', 1)->get();
$projectsIds = [];
$projects = \DB::table('project_categories')->where('category_id', $categoryId)->get();
foreach ($projects as $project){
$projectsIds[] += $project->project_id;
}
return $projectsIds;
}
/**
* 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();
}
}