/
PortfolioController.php
88 lines (71 loc) · 2.48 KB
/
PortfolioController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace Modules\Portfolio\Http;
use Modules\Portfolio\Project;
use Modules\Portfolio\ProjectTranslation;
use Modules\System\Http\FrontController;
use Modules\Tags\Tag;
/**
* Class PortfolioController
* @package Modules\Portfolio\Http
*/
class PortfolioController extends FrontController
{
/**
* @param Project $project
* @param Tag $tags
* @return \Illuminate\Contracts\View\View
* @throws \Exception
*/
public function index(Project $project, Tag $tags)
{
$projects = $project->with($this->relations())->orderBy('date', 'desc')->get();
$options = [
'columns' => $this->theme->setting('portfolioColumns'),
//grid or full width?
'grid' => $this->theme->setting('portfolioGrid'),
//space or no space?
'spaced' => $this->theme->setting('portfolioSpaced'),
];
$tags = $projects->getUniqueTags();
return $this->theme->render('portfolio.index', ['projects' => $projects, 'options' => $options, 'tags' => $tags]);
}
/**
* @param ProjectTranslation $portfolio
* @return \Illuminate\Contracts\View\View
*/
public function show(ProjectTranslation $portfolio)
{
$relations = $this->relations();
$project = $portfolio->project;
if (! $project) {
abort(404);
}
$project->load($relations);
$this->seo->setEntity($project);
$tags = $project->tags;
$relatedProjects = $project->with($relations)
->taggedWith($tags)
->where($project->getTable().'.'.$project->getKeyName(), '<>', $project->id)
->orderBy($project->getTable().'.date', 'desc')
->take(4)
->get();
if ($relatedProjects->count() < 4) {
$extra = $project->with($relations)
->where($project->getTable().'.'.$project->getKeyName(), '<>', $project->id)
->orderBy($project->getTable().'.date', 'desc')
->take(4)
->get();
while ($relatedProjects->count() < 4 && $extra->count() > 0) {
$relatedProjects->push($extra->pop());
}
}
return $this->theme->render('portfolio.show', ['project' => $project, 'relatedProjects' => $relatedProjects]);
}
/**
*
*/
protected function relations()
{
return ['translations', 'images', 'images.translations', 'images.sizes', 'tags', 'tags.translations'];
}
}