1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Tag; |
6
|
|
|
use App\Models\TagRelation; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
class TaggingController extends Controller |
11
|
|
|
{ |
12
|
|
|
public function index($orderby = 'tag', $direction = 'asc'){ |
13
|
|
|
$tags = Tag::all()->sortBy('title'); |
14
|
|
|
|
15
|
|
|
return view('tags.index', [ |
16
|
|
|
'tags' => $tags, |
17
|
|
|
'orderby' => $orderby, |
18
|
|
|
'direction' => $direction, |
19
|
|
|
]); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function showGames($tagid) { |
|
|
|
|
23
|
|
|
$games = \DB::table('games') |
24
|
|
|
->leftJoin('games_developer', 'games.id', '=', 'games_developer.game_id') |
25
|
|
|
->leftJoin('developer', 'games_developer.developer_id', '=', 'developer.id') |
26
|
|
|
->leftJoin('makers', 'makers.id', '=', 'games.maker_id') |
27
|
|
|
->leftJoin('tag_relations', function($join) { |
28
|
|
|
$join->on('tag_relations.content_id', '=', 'games.id'); |
29
|
|
|
$join->on('tag_relations.content_type', '=', \DB::raw("'game'")); |
30
|
|
|
}) |
31
|
|
|
->leftJoin('tags', 'tag_relations.tag_id', '=', 'tags.id') |
32
|
|
|
->leftJoin('games_files', 'games_files.game_id', '=', 'games.id') |
33
|
|
|
->leftJoin('users', 'games_developer.user_id', '=', 'users.id') |
34
|
|
|
->select([ |
35
|
|
|
'games.id as gameid', |
36
|
|
|
'games.title as gametitle', |
37
|
|
|
'games.subtitle as gamesubtitle', |
38
|
|
|
'developer.name as developername', |
39
|
|
|
'developer.id as developerid', |
40
|
|
|
'developer.created_at as developerdate', |
41
|
|
|
'developer.user_id as developeruserid', |
42
|
|
|
'users.name as developerusername', |
43
|
|
|
'games.created_at as gamecreated_at', |
44
|
|
|
'makers.short as makershort', |
45
|
|
|
'makers.title as makertitle', |
46
|
|
|
'makers.id as makerid', |
47
|
|
|
'games.views as views', |
48
|
|
|
'tags.title as tag', |
49
|
|
|
]) |
50
|
|
|
->selectRaw('(SELECT COUNT(id) FROM comments WHERE content_id = games.id AND content_type = "game") as commentcount') |
51
|
|
|
->selectRaw('(SELECT SUM(vote_up) FROM comments WHERE content_id = games.id AND content_type = "game") as voteup') |
52
|
|
|
->selectRaw('(SELECT SUM(vote_down) FROM comments WHERE content_id = games.id AND content_type = "game") as votedown') |
53
|
|
|
->selectRaw('MAX(games_files.release_type) as gametype') |
54
|
|
|
->selectRaw("(SELECT STR_TO_DATE(CONCAT(release_year,'-',release_month,'-',release_day ), '%Y-%m-%d') FROM games_files WHERE game_id = games.id ORDER BY release_year DESC, release_month DESC, release_day DESC LIMIT 1) as releasedate") |
55
|
|
|
->selectRaw('(SELECT COUNT(id) FROM games_coupdecoeur WHERE game_id = games.id) as cdccount') |
56
|
|
|
->where('tags.id', '=', $tagid) |
57
|
|
|
->orderBy('games.title') |
58
|
|
|
->groupBy('games.id') |
59
|
|
|
->get(); |
60
|
|
|
|
61
|
|
|
$gametypes = \DB::table('games_files_types') |
62
|
|
|
->select('id', 'title', 'short') |
63
|
|
|
->get(); |
64
|
|
|
$gtypes = array(); |
|
|
|
|
65
|
|
|
foreach ($gametypes as $gt) { |
66
|
|
|
$t['title'] = $gt->title; |
|
|
|
|
67
|
|
|
$t['short'] = $gt->short; |
|
|
|
|
68
|
|
|
$gtypes[$gt->id] = $t; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return view('tags.show', [ |
72
|
|
|
'games' => $games, |
73
|
|
|
'gametypes' => $gtypes, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function store(Request $request) { |
78
|
|
|
$this->validate($request, [ |
79
|
|
|
'title' => 'required', |
80
|
|
|
'content_id' => 'required', |
81
|
|
|
'content_type' => 'required', |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$tag = Tag::firstOrCreate(['title' => $request->get('title')]); |
|
|
|
|
85
|
|
|
$tagid = $tag->id; |
86
|
|
|
|
87
|
|
|
$tagrel = TagRelation::firstOrCreate([ |
|
|
|
|
88
|
|
|
'tag_id' => $tagid, |
89
|
|
|
'user_id' => \Auth::id(), |
90
|
|
|
'content_id' => $request->get('content_id'), |
91
|
|
|
'content_type' => $request->get('content_type'), |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
if ($request->get('content_type') == 'news') { |
95
|
|
|
return \Redirect::action('NewsController@show', $request->get('content_id')); |
96
|
|
|
}elseif ($request->get('content_type') == 'game') { |
97
|
|
|
return \Redirect::action('GameController@show', $request->get('content_id')); |
98
|
|
|
}elseif ($request->get('content_type') == 'resource') { |
99
|
|
|
return \Redirect::action('ResourceController@show', $request->get('content_id')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return \Redirect::action('IndexController@index'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.