1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gitamin\Http\Controllers; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Gitamin\Facades\Setting; |
16
|
|
|
use Gitamin\Models\Issue; |
17
|
|
|
use Illuminate\Support\Facades\Auth; |
18
|
|
|
use Illuminate\Support\Facades\Request; |
19
|
|
|
use Illuminate\Support\Facades\View; |
20
|
|
|
use Jenssegers\Date\Date; |
21
|
|
|
|
22
|
|
|
class ExploreController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Array of sub-menu items. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $subMenu = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a new project controller instance. |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->subMenu = [ |
37
|
|
|
'yours' => [ |
38
|
|
|
'title' => trans('dashboard.projects.yours'), |
39
|
|
|
'url' => route('dashboard.projects.index'), |
40
|
|
|
'icon' => 'fa fa-edit', |
41
|
|
|
'active' => false, |
42
|
|
|
], |
43
|
|
|
'starred' => [ |
44
|
|
|
'title' => trans('dashboard.projects.starred'), |
45
|
|
|
'url' => route('dashboard.projects.starred'), |
46
|
|
|
'icon' => 'fa fa-umbrella', |
47
|
|
|
'active' => false, |
48
|
|
|
], |
49
|
|
|
'explore' => [ |
50
|
|
|
'title' => trans('dashboard.projects.explore'), |
51
|
|
|
'url' => route('explore.index'), |
52
|
|
|
'icon' => 'fa fa-eye', |
53
|
|
|
'active' => false, |
54
|
|
|
], |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
View::share([ |
58
|
|
|
'sub_menu' => $this->subMenu, |
59
|
|
|
'sub_title' => trans_choice('dashboard.projects.projects', 2), |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Displays the explore page. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\View\View |
67
|
|
|
*/ |
68
|
|
|
public function indexAction() |
69
|
|
|
{ |
70
|
|
|
$this->subMenu['explore']['active'] = true; |
71
|
|
|
|
72
|
|
|
$today = Date::now(); |
73
|
|
|
$startDate = Date::now(); |
74
|
|
|
|
75
|
|
|
// Check if we have another starting date |
76
|
|
|
if (Request::has('start_date')) { |
77
|
|
|
try { |
78
|
|
|
// If date provided is valid |
79
|
|
|
$oldDate = Date::createFromFormat('Y-m-d', Request::get('start_date')); |
80
|
|
|
|
81
|
|
|
// If trying to get a future date fallback to today |
82
|
|
|
if ($today->gt($oldDate)) { |
83
|
|
|
$startDate = $oldDate; |
84
|
|
|
} |
85
|
|
|
} catch (Exception $e) { |
86
|
|
|
// Fallback to today |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$daysToShow = Setting::get('app_issue_days', 0) - 1; |
91
|
|
|
if ($daysToShow < 0) { |
92
|
|
|
$daysToShow = 0; |
93
|
|
|
$issueDays = []; |
94
|
|
|
} else { |
95
|
|
|
$issueDays = range(0, $daysToShow); |
96
|
|
|
} |
97
|
|
|
$dateTimeZone = Setting::get('app_timezone'); |
98
|
|
|
|
99
|
|
|
$issueVisiblity = Auth::check() ? 0 : 1; |
|
|
|
|
100
|
|
|
|
101
|
|
|
$allIssues = Issue::whereBetween('created_at', [ |
102
|
|
|
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00', |
103
|
|
|
$startDate->format('Y-m-d').' 23:59:59', |
104
|
|
|
])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) use ($dateTimeZone) { |
105
|
|
|
return (new Date($issue->created_at)) |
|
|
|
|
106
|
|
|
->setTimezone($dateTimeZone)->toDateString(); |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
// Add in days that have no issues |
110
|
|
|
foreach ($issueDays as $i) { |
111
|
|
|
$date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i); |
112
|
|
|
|
113
|
|
|
if (! isset($allIssues[$date->toDateString()])) { |
114
|
|
|
$allIssues[$date->toDateString()] = []; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Sort the array so it takes into account the added days |
119
|
|
|
$allIssues = $allIssues->sortBy(function ($value, $key) { |
120
|
|
|
return strtotime($key); |
121
|
|
|
}, SORT_REGULAR, true)->all(); |
122
|
|
|
|
123
|
|
|
return View::make('explore.index') |
124
|
|
|
->withPageTitle(trans('dashboard.explore')) |
125
|
|
|
->withProjects([]) |
126
|
|
|
->withSubMenu($this->subMenu) |
127
|
|
|
->withDaysToShow($daysToShow) |
128
|
|
|
->withAllIssues($allIssues) |
129
|
|
|
->withCanPageForward((bool) $today->gt($startDate)) |
130
|
|
|
->withCanPageBackward(Issue::where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0) |
131
|
|
|
->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString()) |
132
|
|
|
->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function groupsAction() |
136
|
|
|
{ |
137
|
|
|
$this->subMenu = [ |
138
|
|
|
'yours' => [ |
139
|
|
|
'title' => trans('gitamin.groups.yours'), |
140
|
|
|
'url' => route('dashboard.groups.index'), |
141
|
|
|
'icon' => 'fa fa-edit', |
142
|
|
|
'active' => false, |
143
|
|
|
], |
144
|
|
|
'explore' => [ |
145
|
|
|
'title' => trans('gitamin.groups.explore'), |
146
|
|
|
'url' => route('explore.groups'), |
147
|
|
|
'icon' => 'fa fa-eye', |
148
|
|
|
'active' => false, |
149
|
|
|
], |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$this->subMenu['explore']['active'] = true; |
153
|
|
|
|
154
|
|
|
return View::make('explore.groups') |
155
|
|
|
->withPageTitle(trans('dashboard.explore')) |
156
|
|
|
->withSubMenu($this->subMenu) |
157
|
|
|
->withSubTitle(trans_choice('dashboard.groups.groups', 2)) |
158
|
|
|
->withProjects([]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Shows an issue in more detail. |
163
|
|
|
* |
164
|
|
|
* @param \Gitamin\Models\Issue $issue |
165
|
|
|
* |
166
|
|
|
* @return \Illuminate\View\View |
167
|
|
|
*/ |
168
|
|
|
public function showIssue(Issue $issue) |
169
|
|
|
{ |
170
|
|
|
return View::make('issue') |
171
|
|
|
->withIssue($issue); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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.