File: D:/HostingSpaces/EUmans/umansradepo.be/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 Carbon\Carbon;
use Komma\Images\ImageService;
use Komma\Pages\PageService;
use Komma\Projects\Models\Project;
use Komma\Projects\Models\ProjectTranslation;
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);
$this->getPreviousAndNextProjects($project);
$this->getLatestProjects($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('references');
$project->translation->meta_title = $project->translation->name.' | '.$metaParent->meta_title;
$date = $project->date;
$project->date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
}
protected function getPreviousAndNextProjects(Project &$project){
$previous = Project::where('id', '!=', $project->id)
->with('translation')
->with('translation.route')
->where('active', '!=', 0)
->where('lft', '!=', 1)
->where('lft', '>', $project->lft)
->orderBy('lft', 'ASC');
$next = Project::where('id', '!=', $project->id)
->with('translation')
->with('translation.route')
->where('active', '!=', 0)
->where('lft', '!=', 1)
->where('lft', '<', $project->lft)
->orderBy('lft', 'DESC');
if(\Session::has('materials')){
$materials = explode(',', \Session::get('materials'));
$next->whereIn('material', $materials);
$previous->whereIn('material', $materials);
}
if(\Session::has('types')){
$types = explode(',', \Session::get('types'));
$next->whereIn('type', $types);
$previous->whereIn('type', $types);
}
$project->next = $next->first();
$project->previous = $previous->first();
}
protected function getLatestProjects(Project &$project){
$projects = Project::where('id', '!=', $project->id)
->with('translation')
->with('translation.route')
->where('active', '!=', 0)
->where('lft', '!=', 1)
->orderBy('lft', 'ASC')
->take(4)
->get();
$project->projects= $projects;
}
/**
* 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', '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;
}
/**
* 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, $amount = null)
{
//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('translation.route')
->with('images')
->orderBy('lft', 'ASC');
if($amount) $projects = $projects->take($amount);
$projects = $projects->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;
}
/**
* This method gets the projects where
*
* @param $field | string, field for the where
* @param $values | array, value for the where
* @return mixed
*/
public function getProjectsFiltered($filterArray, $amount = null)
{
//Only get the active blocks
$projects = Project::where('active', '=', 1)
//Where field is value
->where('lft', '!=', 1)
//Also load the translation
->with('translation')
->with('translation.route')
->with('images')
->orderBy('lft', 'ASC');
if($amount) $projects = $projects->take($amount);
$projects = $projects->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();
}
public function getFilters()
{
$filter1 = \DB::table('projects')
->select('material')
->groupBy('material')
->where('material', '!=', 'null')
->where('material', '!=', 0)
->get();
$filter2 = \DB::table('projects')
->select('type')
->groupBy('type')
->where('type', '!=', 'null')
->where('type', '!=', 0)
->get();
return ['material' => $filter1, 'type' => $filter2];
}
}