File: D:/HostingSpaces/SBogers87/basephotography.nl/app/Komma/Projects/ProjectRepository.php
<?php
namespace Komma\Projects;
use Komma\Kms\Categories\Models\CategoryTranslation;
use Komma\Kms\Images\Models\Image;
use Komma\Kms\Projects\Models\Project;
class ProjectRepository
{
/**
* Return all projects
*
* @param array $in
* @param array $with
* @return mixed
*/
public function getProjects($id = null)
{
if($id == null) {
return Project::where('lft', '!=', 1)
->where('active', '=', 1)
->orderBy('lft')
->get()
->keyBy('lft');
}
$currentProject = Project::where('id', $id)->where('active', 1)->first();
$previousProject = Project::where('lft', '<', $currentProject->lft)->where('active', 1)->where('lft', '!=', 1)->first();
$nextProject = Project::where('lft', '>', $currentProject->lft)->where('active', 1)->where('lft', '!=', 1)->first();
if(!isset($previousProject)) $previousProject = Project::where('lft', '>', $nextProject->lft)->where('active', 1)->where('lft', '!=', 1)->first();
if(!isset($nextProject)) $nextProject = Project::where('lft', '<', $previousProject->lft)->where('active', 1)->where('lft', '!=', 1)->first();
return [
'project' => $currentProject,
'next' => $nextProject,
'previous' => $previousProject
];
}
public function getProjectInformation($projects){
$projectArray = [];
foreach ($projects as $key => $project){
//dd($project->id);
$newProject = Project::leftJoin('project_translations',
'project_translations.project_id', '=', 'projects.id')
->leftJoin('routes', function($join){
$join->on('project_translations.id', '=', 'routes.routable_id')
->where('routes.routable_type', '=', 'Komma\Kms\Projects\Models\ProjectTranslation');
})
->where('projects.id', '=', ($project->id))
->select('project_translations.*', 'routes.route', 'projects.id', 'projects.double_block')
->first();
$projectImages = \DB::table('images')
->where('imageble_id', '=', $project->id)
->where('attribute_key', '=', 'images');
$projectImages = $projectImages->orderBy('sort_order', 'asc');
$newProject->images = $projectImages->get();
$newProject->header_image = \DB::table('images')
->where('imageble_id', '=', $project->id)
->where('attribute_key', '=', 'project_image')
->first();
//$newProject->category = $this->getCategories($newProject->category);
$projectArray[$key] = $newProject;
}
return $projectArray;
}
public function getCategories($selected = null)
{
if(isset($selected))
{
$categories = \DB::table('category_translations')
->whereNotNull('category_translations.name')
->select('category_translations.name')
->whereIn('category_translations.category_id', json_decode($selected))
->get();
}
else
{
$categories = \DB::table('category_translations')
->whereNotNull('category_translations.name')
->select('category_translations.name', 'category_translations.category_id as id')
->get();
}
return $categories;
}
/**
* Get category Id form category name
* @param $category
* @return mixed
*/
public function getCategoryId($category){
return CategoryTranslation::where('name', '=', $category)->first()->category_id;
}
/**
* Get all image ids with $categoryId
*
* @param $categoryId
* @return mixed
*/
public function getCategoryImageIds($categoryId){
$ids = \DB::table('category_image')
->where('category_id', '=', $categoryId)
->select('image_id')
->get();
$idArray = [];
foreach ($ids as $id){
$idArray[] = $id->image_id;
}
return $idArray;
}
public function getCategoryImages($images = []){
$images = Image::whereIn('images.id', $images)
->select('images.*', 'project_translations.name', 'routes.route', 'projects.active')
->where('images.attribute_key', '=', 'images')
->where('images.imageble_type', '=', 'Komma\Kms\Projects\Models\Project')
->where('routes.routable_type', '=', 'Komma\Kms\Projects\Models\ProjectTranslation')
->join('project_translations', 'images.imageble_id', '=', 'project_translations.project_id')
->join('routes', 'project_translations.id', '=', 'routes.routable_id')
->join('projects', 'project_translations.project_id', '=', 'projects.id')
->where('projects.active', '=', 1)
->get();
return $images;
}
}