File: D:/HostingSpaces/PDeckers/opelkapitan.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\Kms\Categories\Models\Category;
use Komma\Pages\Models\Page;
use Komma\Pages\Models\PageTranslation;
use Komma\Projects\Models\Project;
use Komma\Routes\Models\Route;
class ProjectService
{
private $paginationSize = 50;
public function getAllProjects($pagination = true, $itemsPerPage = null)
{
if($itemsPerPage == null) $itemsPerPage = $this->paginationSize;
$projects = Project::where('lft', '!=', 1)
->with('translation')
->with('translation.route')
->with('images')
->where('active', '=', 1);
if ($pagination) {
$projects = $projects->paginate($itemsPerPage);
} else {
$projects = $projects->get();
}
return $projects;
}
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');
return $project;
}
/**
* 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
$parts = 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();
return $parts;
}
public function getPartTypesByKapitan($kapitan){
$parts = Project::distinct()
->where('active', '=', 1)
->where('lft', '!=', 1)
->whereHas('categories', function ($q) use ($kapitan){
$q->where('code_name', $kapitan);
})
->select('partType')
->orderBy('partType')
->groupBy('partType')
->get();
return $parts->lists('partType');
}
public function getPartsByPartTypeAndKapitan($kapitan, $partType)
{
$parts = Project::where('active', '=', 1)
->where('lft', '!=', 1)
->whereHas('categories', function ($q) use ($kapitan){
$q->where('code_name', $kapitan);
})
->where('partType', '=', $partType)
->orderBy('uniqueNr')
->paginate($this->paginationSize);
foreach ($parts as &$part){
$route = Route::where('rest_route', 'projects/'.$part->id)
->where('route', 'like', \Request::path().'%')
->first();
$part->translation->route = $route;
}
return $parts;
}
public function getOtherParts($category, $slug){
$parts = Project::where('active', '=', 1)
->where('lft', '!=', 1)
->whereHas('categories', function ($q) use($category){
$q->where('code_name', $category);
})
->orderBy('uniqueNr')
->paginate($this->paginationSize);
foreach ($parts as &$part){
$route = Route::where('rest_route', 'projects/'.$part->id)
->where('route', 'like', 'overig/'.$slug.'%')
->first();
$part->translation->route = $route;
}
return $parts;
}
/**
* Get all translations of an project
* based upon this project id
*
* @param $project_id
* @return mixed
*/
public function getOtherLanguageRoutes($project_id){
return Project::where('id', '=', $project_id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
public function getNextPreviousParts($part_id, $kapitan, $partType){
$previous = true;
$next = true;
$parts = Project::where('active', '=', 1)
->where('lft', '!=', 1)
->whereHas('categories', function ($q) use ($kapitan){
$q->where('code_name', $kapitan);
});
//if category is other don't filter on partType
if( ! in_array($kapitan, ['other_3','other_4',])){
$parts = $parts->where('partType', '=', $partType);
}
$parts = $parts->orderBy('uniqueNr')
->get();
//get key of current part
$partKey = 0;
foreach ($parts as $key => $part){
if($part->id == $part_id) $partKey = $key;
}
//check if part is first or last in collection
if($partKey == 0) $previous = false;
if( (count($parts->keys()) - 1) == $partKey) $next = false;
if($previous){
$previous = $parts[$partKey - 1];
$previous->route = $this->getRoute($previous->id, $kapitan);
}
if($next){
$next = $parts[$partKey + 1];
$next->route = $this->getRoute($next->id, $kapitan);
}
$previousNext = ['previous' => $previous, 'next' => $next];
//var_dump($kapitan);
//mooi($previousNext);
return $previousNext;
}
public function getRoute($id, $type){
if(in_array($type, ['other','other_1','other_2','other_3',])){
$searchRoute = 'overig/%';
if($type == 'other_1' || $type == 'other_2')
{
$searchRoute = '';
$segments = \Request::segments();
foreach ($segments as $key => $segment){
if($key < sizeof($segments)-1) $searchRoute.= $segment.'/';
}
$searchRoute .= '%';
}
return Route::where('rest_route', 'projects/'.$id)
->where('route', 'like', $searchRoute)
->first();
}
return Route::where('rest_route', 'projects/'.$id)
->where('route', 'like', '%opel-kapitan-'.str_replace('_', '-', $type).'%')
->first();
}
public function getCategoryCodeName($routeName){
$pageId = Route::where('route', $routeName)->first()->routable_id;
$pageTrans = PageTranslation::where('id', $pageId)
->with('page')
->first();
return $pageTrans->page->code_name;
}
}