-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProjectController.php
More file actions
150 lines (133 loc) · 5.34 KB
/
Copy pathProjectController.php
File metadata and controls
150 lines (133 loc) · 5.34 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Enums\EuPlatescStatus;
use App\Http\Filters\AcceptsVolunteersFilter;
use App\Http\Filters\CountiesFilter;
use App\Http\Filters\ProjectCategoriesFilter;
use App\Http\Filters\ProjectDatesFilter;
use App\Http\Filters\ProjectNationalFilter;
use App\Http\Filters\ProjectStatusFilter;
use App\Http\Filters\SearchFilter;
use App\Http\Requests\Project\DonateRequest;
use App\Http\Resources\Collections\ProjectCardCollection;
use App\Http\Resources\Project\ShowProjectResource;
use App\Http\Sorts\ProjectDonationsCountSort;
use App\Http\Sorts\ProjectDonationsSumSort;
use App\Models\County;
use App\Models\Project;
use App\Models\Volunteer;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\QueryBuilder;
class ProjectController extends Controller
{
public function index(Request $request): Response
{
return $this->projectList($request, 'list');
}
public function map(Request $request): Response
{
return $this->projectList($request, 'map');
}
protected function projectList(Request $request, string $view): Response
{
$project = QueryBuilder::for(Project::class)
->without('donations')
->allowedFilters([
AllowedFilter::custom('county', new CountiesFilter),
AllowedFilter::custom('category', new ProjectCategoriesFilter),
AllowedFilter::custom('date', new ProjectDatesFilter),
AllowedFilter::custom('status', new ProjectStatusFilter),
AllowedFilter::custom('volunteers', new AcceptsVolunteersFilter),
AllowedFilter::custom('search', new SearchFilter),
AllowedFilter::custom('is_national', new ProjectNationalFilter),
])
->allowedSorts([
AllowedSort::field('publish_date', 'start'),
AllowedSort::field('end_date', 'end'),
AllowedSort::field('target', 'target_budget'),
AllowedSort::custom('donations_total', new ProjectDonationsSumSort),
AllowedSort::custom('donations_count', new ProjectDonationsCountSort),
])
->defaultSort('-id')
->whereIsPublished()
->whereHasValidDates('start', 'end');
return Inertia::render('Public/Projects/Index', [
'view' => $view,
'categories' => $this->getProjectCategories(),
'counties' => $this->getCounties(),
'google_maps_api_key' => config('services.google_maps_api_key'),
'pins' => $view === 'map'
? County::query()
->withWhereHasProjectsCount()
->get()
: [],
'collection' => new ProjectCardCollection(
$project->paginate()->withQueryString()
),
]);
}
public function show(Project $project)
{
if (! $project->isPublished()) {
$this->authorize('view', $project);
}
return Inertia::render('Public/Projects/Show', [
'project' => new ShowProjectResource($project),
]);
}
public function donate(Project $project, DonateRequest $request)
{
$attributes = $request->validated();
try {
[$lastName, $firstName] = explode(' ', $attributes['name']);
} catch (\Exception $e) {
throw ValidationException::withMessages(['name' => __('invalid_name')]);
}
$donation = $project->donations()->create([
'organization_id' => $project->organization_id,
'user_id' => auth()->user()->id ?? null,
'amount' => $attributes['amount'],
'uuid' => (string) Str::uuid(),
'charge_amount' => 0,
'first_name' => $firstName,
'last_name' => $lastName,
'email' => $attributes['email'],
'status' => EuPlatescStatus::INITIALIZE,
'card_status' => null,
'card_holder_status_message' => null,
'approval_date' => null,
'charge_date' => null,
'updated_without_correct_e_pid' => false,
]);
return redirect()->route('donation.make', $donation->uuid);
}
public function volunteer(Project $project, Request $request)
{
$attributes = $request->validate([
'terms' => ['required', 'accepted'],
'email' => ['required', 'email'],
'name' => 'required',
'phone' => 'required',
]);
$volunteer = Volunteer::create([
'user_id' => auth()->user()->id ?? null,
'name' => $attributes['name'],
'email' => $attributes['email'],
'phone' => $attributes['phone'],
]);
$volunteer->projects()->attach($project->id, ['status' => 'pending']);
/*
* TODO: Corner case user volunteers is redirect to VolunteerThankYou page
* with project but if refreshes the page some data in thank you page is lost
* Posibly implementation duplicate ThankYou page and and send parameter of project
*/
return redirect()->route('volunteer.thanks')->with(['data' => $project]);
}
}