|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Spot; |
|
6
|
|
|
use App\Country; |
|
7
|
|
|
use App\Mail\SpotSubmitted; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Facades\DB; |
|
10
|
|
|
use Illuminate\Support\Facades\Mail; |
|
11
|
|
|
use Illuminate\Support\Facades\Cache; |
|
12
|
|
|
use Intervention\Image\Facades\Image; |
|
13
|
|
|
use Illuminate\Support\Facades\Storage; |
|
14
|
|
|
|
|
15
|
|
|
class SpotsController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Show the form for creating a new Spot. |
|
19
|
|
|
* |
|
20
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
21
|
|
|
*/ |
|
22
|
|
|
public function create() |
|
23
|
|
|
{ |
|
24
|
|
|
$minutes = 525600; // One year |
|
25
|
|
|
|
|
26
|
|
|
$countries = Cache::remember('countries', $minutes, function () { |
|
27
|
|
|
return Country::whereIn('continent_id', [2, 3, 6, 7]) |
|
28
|
|
|
->select('id', 'name_pt') |
|
29
|
|
|
->orderBy('name_pt', 'asc') |
|
30
|
|
|
->get(); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
return view('spots.create', compact('countries')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Store a newly created Spot in storage. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
41
|
|
|
*/ |
|
42
|
|
|
public function store(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
$request->validate([ |
|
45
|
|
|
'name' => 'required|string|max:60', |
|
46
|
|
|
'city' => 'required|string|max:35', |
|
47
|
|
|
'country' => 'required|exists:countries,id', |
|
48
|
|
|
'address' => 'required|string|nullable|max:255', |
|
49
|
|
|
'phone-number' => 'string|nullable|max:20', |
|
50
|
|
|
'email' => 'email|nullable', |
|
51
|
|
|
'website' => 'url|nullable', |
|
52
|
|
|
'image' => 'image|nullable|max:2000', |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
$image = $request->file('image'); |
|
56
|
|
|
$imagePath = null; |
|
57
|
|
|
|
|
58
|
|
|
if ($image) { |
|
59
|
|
|
$imagePath = $image->hashName('spots'); |
|
60
|
|
|
|
|
61
|
|
|
$img = Image::make($image); |
|
62
|
|
|
$img->fit(675, 450); |
|
63
|
|
|
|
|
64
|
|
|
Storage::put($imagePath, (string) $img->encode()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$name = $request->get('name'); |
|
68
|
|
|
|
|
69
|
|
|
$slug = str_slug($name); |
|
70
|
|
|
$slugIndex = 0; |
|
71
|
|
|
|
|
72
|
|
|
// If slug already in use, add an index to the slug |
|
73
|
|
|
while (Spot::where('slug', $slug)->count()) { |
|
74
|
|
|
$slugIndex++; |
|
75
|
|
|
$slug = "$slug-$slugIndex"; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$spot = Spot::create([ |
|
79
|
|
|
'name' => $name, |
|
80
|
|
|
'slug' => $slug, |
|
81
|
|
|
'country_id' => $request->get('country'), |
|
82
|
|
|
'city' => $request->get('city'), |
|
83
|
|
|
'address' => $request->get('address'), |
|
84
|
|
|
'email' => $request->get('email'), |
|
85
|
|
|
'phone_number' => $request->get('phone-number'), |
|
86
|
|
|
'website' => $request->get('website'), |
|
87
|
|
|
'image' => $imagePath, |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
$recipientAddress = config('mail.to')['address']; |
|
91
|
|
|
|
|
92
|
|
|
if ($recipientAddress && (config('mail.status') || app()->environment(['production']))) { |
|
|
|
|
|
|
93
|
|
|
// Send e-mail to admin alerting about new spot submission |
|
94
|
|
|
Mail::to($recipientAddress)->send(new SpotSubmitted($spot)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return redirect()->route('home')->with('success', |
|
98
|
|
|
"<p>Spot <strong>$name</strong> submetido com sucesso!</p><p>Será publicado em breve, após revisão por |
|
99
|
|
|
parte da nossa equipa. Obrigado!</p>"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Display the specified Spot. |
|
104
|
|
|
* |
|
105
|
|
|
* @param String $countrySlug |
|
106
|
|
|
* @param String $spotSlug |
|
107
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
108
|
|
|
*/ |
|
109
|
|
|
public function show(String $countrySlug, String $spotSlug) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$spot = Spot::where('slug', $spotSlug) |
|
112
|
|
|
->where('is_approved', true) |
|
113
|
|
|
->firstOrFail(); |
|
114
|
|
|
|
|
115
|
|
|
return view('spots.show', compact('spot')); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|