123 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace App\Http\Controllers\Dashboard;
 | |
| 
 | |
| use App\Http\Controllers\Controller;
 | |
| use App\Http\Requests\CVRequest;
 | |
| use App\Http\Resources\CVInfoCollection;
 | |
| use App\Http\Resources\FullCVCollection;
 | |
| use App\Http\Resources\FullCVResource;
 | |
| use App\Models\CV;
 | |
| use Illuminate\Http\RedirectResponse;
 | |
| use Illuminate\Http\Request;
 | |
| use Illuminate\Support\Str;
 | |
| use Inertia\Response as InertiaResponse;
 | |
| 
 | |
| class CVController extends Controller
 | |
| {
 | |
|     public function index(Request $request): InertiaResponse
 | |
|     {
 | |
|         return inertia('CV/Index', [
 | |
|             'cvs' => new FullCVCollection(CV::all()),
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     public function show(CV $cv): InertiaResponse
 | |
|     {
 | |
|         return inertia('CV/Show', [
 | |
|             'cv' => new FullCVResource($cv),
 | |
|             'cvInfo' => new CVInfoCollection($cv->info()->orderByDesc('id')->get()),
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     public function create(): InertiaResponse
 | |
|     {
 | |
|         return inertia('CV/Create');
 | |
|     }
 | |
| 
 | |
|     public function store(CVRequest $request): RedirectResponse
 | |
|     {
 | |
|         CV::query()
 | |
|             ->create([
 | |
|                 'token' => Str::random(50),
 | |
|                 'recipient' => $request->get('recipient'),
 | |
|                 'email' => $request->get('email'),
 | |
|                 'phone_number' => $request->get('phone_number'),
 | |
|                 'locations' => ($locations = $request->get('locations')) === [''] ? [] : $locations,
 | |
|                 'mission' => ($mission = $request->get('mission')) === [''] ? [] : $mission,
 | |
|                 'rodo' => ($rodo =$request->get('rodo')) === '' ? null : $rodo,
 | |
|                 'position' => $request->get('position'),
 | |
|                 'notes' => $request->get('notes'),
 | |
|             ]);
 | |
|         return redirect()
 | |
|             ->route('admin.cv.store')
 | |
|             ->with('success', 'Utworzono nowe CV dla firmy ' . $request->get('recipient'));
 | |
|     }
 | |
| 
 | |
|     public function updateSendStatus(CV $cv): RedirectResponse
 | |
|     {
 | |
|         $cv->update([
 | |
|             'sended' => true,
 | |
|             'sended_timestamp' => now()
 | |
|         ]);
 | |
|         return redirect()
 | |
|             ->route('admin.cv.show', ['cv' => $cv])
 | |
|             ->with('success', 'Status wysłania ustawiono jako "wysłano".');
 | |
|     }
 | |
| 
 | |
|     public function edit(CV $cv): InertiaResponse
 | |
|     {
 | |
|         return inertia('CV/Edit', [
 | |
|             'cv' => new FullCVResource($cv),
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     public function update(CVRequest $request, CV $cv): RedirectResponse
 | |
|     {
 | |
|         $toUpdate = [
 | |
|             'recipient' => $request->get('recipient'),
 | |
|             'email' => $request->get('email'),
 | |
|             'phone_number' => $request->get('phone_number'),
 | |
|             'locations' => ($locations = $request->get('locations')) === [''] ? [] : $locations,
 | |
|             'mission' => ($mission = $request->get('mission')) === [''] ? [] : $mission,
 | |
|             'rodo' => ($rodo =$request->get('rodo')) === '' ? null : $rodo,
 | |
|             'position' => $request->get('position'),
 | |
|             'notes' => $request->get('notes'),
 | |
|         ];
 | |
| 
 | |
|         if ($cv->sended && ! $request->boolean('sended')) {
 | |
|             $toUpdate = array_merge($toUpdate, [
 | |
|                 'sended' => false,
 | |
|                 'sended_timestamp' => null, 
 | |
|             ]);
 | |
|         } else if (! $cv->sended && $request->boolean('sended')) {
 | |
|             $toUpdate = array_merge($toUpdate, [
 | |
|                 'sended' => true,
 | |
|                 'sended_timestamp' => now(), 
 | |
|             ]);
 | |
|         }
 | |
| 
 | |
|         $cv->update($toUpdate);
 | |
|         return redirect()
 | |
|             ->back()
 | |
|             ->with('success', 'Zaktualizowano CV dla firmy ' . $request->get('recipient'));
 | |
|     }
 | |
| 
 | |
|     public function delete(CV $cv): InertiaResponse
 | |
|     {
 | |
|         return inertia('CV/ConfirmDelete', compact('cv'));
 | |
|     }
 | |
| 
 | |
|     public function destroy(CV $cv): RedirectResponse
 | |
|     {
 | |
|         $name = $cv->recipient;
 | |
|         $cv->delete();
 | |
| 
 | |
|         return redirect()
 | |
|             ->route('admin.cv.index')
 | |
|             ->with('info', 'Usunięto CV dla firmy "'. $name .'"');
 | |
|     }
 | |
| }
 |