Revize 75508baf
Přidáno uživatelem Marek Lovčí před téměř 5 roky(ů)
app/Http/Controllers/ArtefactController.php | ||
---|---|---|
5 | 5 |
use App\Artefact; |
6 | 6 |
use App\ArtefactCategory; |
7 | 7 |
use App\Category; |
8 |
use Illuminate\Http\Request; |
|
9 |
use Illuminate\Support\Facades\DB; |
|
8 |
use App\User; |
|
9 |
use Illuminate\Contracts\View\Factory; |
|
10 |
use Illuminate\Http\RedirectResponse; |
|
11 |
use Illuminate\Support\Facades\Auth; |
|
12 |
use Illuminate\View\View; |
|
10 | 13 |
|
11 | 14 |
class ArtefactController extends Controller |
12 | 15 |
{ |
16 |
const ORDER_COLUMN = 'page'; |
|
17 |
|
|
13 | 18 |
public function __construct() |
14 | 19 |
{ |
15 | 20 |
$this->middleware('auth'); |
16 | 21 |
} |
17 | 22 |
|
23 |
/** |
|
24 |
* Prepare all necessary data for shown Artefact. |
|
25 |
* These are current count of likes, if it set to |
|
26 |
* a favorites by a logged user and all metadata |
|
27 |
* connected to the artefact. |
|
28 |
* |
|
29 |
* @param $artefacts all artefacts |
|
30 |
* @return mixed modified artefacts |
|
31 |
*/ |
|
32 |
public function prepData($artefacts) |
|
33 |
{ |
|
34 |
foreach($artefacts as $artefact) |
|
35 |
{ |
|
36 |
$artefact['likes'] = Artefact::find($artefact->id)->users()->count(); |
|
37 |
$artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($artefact->id)) ? false : true; |
|
38 |
$metadata = Artefact::find($artefact->id)->metadata()->orderBy(self::ORDER_COLUMN)->get(); |
|
39 |
|
|
40 |
foreach ($metadata as $item) |
|
41 |
{ |
|
42 |
$item['favourite'] = is_null(User::find(Auth::id())->likesMetadata()->find($item->id)) ? false : true; |
|
43 |
} |
|
44 |
|
|
45 |
$artefact['metadata'] = $metadata; |
|
46 |
} |
|
47 |
|
|
48 |
return $artefacts; |
|
49 |
} |
|
50 |
|
|
18 | 51 |
/** |
19 | 52 |
* Returns view of all artefacts. |
20 | 53 |
* |
21 |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
54 |
* @return Factory|View
|
|
22 | 55 |
*/ |
23 | 56 |
public function default() |
24 | 57 |
{ |
25 |
$artefacts = Artefact::all();
|
|
26 |
|
|
58 |
$artefacts = Artefact::simplePaginate(1);
|
|
59 |
$artefacts = $this->prepData($artefacts); |
|
27 | 60 |
return view('artefact.default', ['artefacts' => $artefacts]); |
28 | 61 |
} |
29 | 62 |
|
30 | 63 |
/** |
31 | 64 |
* Returns view of artefacts related to the chosen category |
32 | 65 |
* |
33 |
* @param $id id of the category
|
|
34 |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
66 |
* @param $id id of the category |
|
67 |
* @return Factory|View
|
|
35 | 68 |
*/ |
36 | 69 |
public function showCategory($id) |
37 | 70 |
{ |
38 |
$cateogryArtefacts = ArtefactCategory::where('category_id', $id)->get(); |
|
39 |
if(count($cateogryArtefacts) > 0) |
|
71 |
$artefacts = Category::find($id)->artefacts()->simplePaginate(1); |
|
72 |
$artefacts = $this->prepData($artefacts); |
|
73 |
return view('artefact.default', ['artefacts' => $artefacts]); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* Returns view of artefacts related to the chosen categories |
|
78 |
* |
|
79 |
* @param $id string with categories ids |
|
80 |
* @return Factory|View |
|
81 |
*/ |
|
82 |
public function showCategories($id) |
|
83 |
{ |
|
84 |
//Parsing text for individual ids of categories |
|
85 |
$textWithIds = $id; |
|
86 |
$pieces = explode(",", $textWithIds); |
|
87 |
$arrStrToInt = array(); |
|
88 |
for($i = 0;$i < count($pieces); $i++) |
|
40 | 89 |
{ |
41 |
$artefacts = array(); |
|
42 |
foreach($cateogryArtefacts as $ar) |
|
90 |
if($pieces[$i] == null || !strcmp($pieces[$i], "")) |
|
43 | 91 |
{ |
44 |
array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
|
|
92 |
continue;
|
|
45 | 93 |
} |
46 |
return view('artefact.category', ['artefacts' => $artefacts]); |
|
94 |
|
|
95 |
array_push($arrStrToInt, (int)$pieces[$i]); |
|
47 | 96 |
} |
48 |
else |
|
97 |
|
|
98 |
//Creating array of all artefacts ids |
|
99 |
$arrArtIds = array(); |
|
100 |
foreach($arrStrToInt as $cat) |
|
49 | 101 |
{ |
50 |
return view('artefact.category', ['artefacts' => array()]); |
|
102 |
$tmpArtefacts = Category::find($cat)->artefacts()->get(); |
|
103 |
foreach($tmpArtefacts as $art) |
|
104 |
{ |
|
105 |
array_push($arrArtIds, $art->id); |
|
106 |
} |
|
51 | 107 |
} |
108 |
|
|
109 |
$artefacts = Artefact::whereIn('id', $arrArtIds)->simplePaginate(1); |
|
110 |
$artefacts = $this->prepData($artefacts); |
|
111 |
return view('artefact.default', ['artefacts' => $artefacts]); |
|
52 | 112 |
} |
53 | 113 |
|
54 | 114 |
/** |
55 | 115 |
* Returns view of single artefact given by its id. |
56 | 116 |
* |
57 | 117 |
* @param $id int id of the artefact |
58 |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
118 |
* @return Factory|View
|
|
59 | 119 |
*/ |
60 | 120 |
public function view($id) |
61 | 121 |
{ |
122 |
$artefact = Artefact::where('id', $id)->simplePaginate(1); |
|
123 |
$artefact = $this->prepData($artefact); |
|
124 |
//return view('artefact.view', ['artefact' => $artefact]); |
|
125 |
return view('artefact.default', ['artefacts' => $artefact]); |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Likes artefact given by its id. |
|
130 |
* |
|
131 |
* @param $id int id of metadata |
|
132 |
* @return RedirectResponse |
|
133 |
*/ |
|
134 |
public function like($id) |
|
135 |
{ |
|
136 |
$user = User::find(Auth::id()); |
|
137 |
$artefact = Artefact::find($id); |
|
138 |
|
|
139 |
$user->likesArtefacts()->attach($artefact); |
|
140 |
|
|
141 |
return back()->withInput(); |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* Unlikes artefact given by its id. |
|
146 |
* |
|
147 |
* @param $id int id of metadata |
|
148 |
* @return RedirectResponse |
|
149 |
*/ |
|
150 |
public function unlike($id) |
|
151 |
{ |
|
152 |
$user = User::find(Auth::id()); |
|
62 | 153 |
$artefact = Artefact::find($id); |
63 |
$artefact['likes'] = Artefact::find($id)->users()->count(); |
|
64 | 154 |
|
65 |
return view('artefact.view', ['artefact' => $artefact]); |
|
155 |
$user->likesArtefacts()->detach($artefact); |
|
156 |
|
|
157 |
return back()->withInput(); |
|
66 | 158 |
} |
159 |
|
|
160 |
/** |
|
161 |
* Returns artefact given by its id. |
|
162 |
* |
|
163 |
* @param $id int id of artefact |
|
164 |
* @return RedirectResponse |
|
165 |
*/ |
|
166 |
/*public function view($id) |
|
167 |
{ |
|
168 |
//$user = User::find(Auth::id()); |
|
169 |
//$artefact = Artefact::find($id); |
|
170 |
|
|
171 |
$artefacts = Artefact::withCount('users')->orderByDesc('users_count')->get()->take(10); |
|
172 |
|
|
173 |
|
|
174 |
$currentPage = -1; // You can set this to any page you want to paginate to |
|
175 |
|
|
176 |
for($i=0;$i<10;$i++){ |
|
177 |
if($artefacts[$i]->id==$id) $currentPage = $i+1; |
|
178 |
} |
|
179 |
// Make sure that you call the static method currentPageResolver() |
|
180 |
// before querying users |
|
181 |
\Illuminate\Pagination\Paginator::currentPageResolver(function () use ($currentPage) { |
|
182 |
return $currentPage; |
|
183 |
}); |
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
$artefacts = Artefact::withCount('users')->orderByDesc('users_count')->limit(10)->paginate(1); |
|
188 |
|
|
189 |
$artefacts = ArtefactController::prepData($artefacts); |
|
190 |
return view('artefact.default', ['artefacts' => $artefacts]); |
|
191 |
/*foreach($artefacts as $item) |
|
192 |
{ |
|
193 |
$id = $item->id; |
|
194 |
$item['likes'] = Artefact::find($id)->users()->count(); |
|
195 |
$item['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true; |
|
196 |
}*/ |
|
197 |
|
|
198 |
|
|
199 |
//return back()->withInput(); |
|
200 |
/*}*/ |
|
67 | 201 |
} |
Také k dispozici: Unified diff
Merge origin/develop into master