davide-casiraghi /
laravel-events-calendar
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers; |
||
| 4 | |||
| 5 | use DavideCasiraghi\LaravelEventsCalendar\Models\EventCategoryTranslation; |
||
| 6 | use Illuminate\Http\Request; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | use Validator; |
||
| 9 | |||
| 10 | class EventCategoryTranslationController extends Controller |
||
| 11 | { |
||
| 12 | /* Restrict the access to this resource just to logged in users */ |
||
| 13 | 5 | public function __construct() |
|
| 14 | { |
||
| 15 | 5 | $this->middleware('admin'); |
|
| 16 | 5 | } |
|
| 17 | |||
| 18 | /** |
||
| 19 | * Display a listing of the resource. |
||
| 20 | * |
||
| 21 | * @return \Illuminate\Http\Response |
||
| 22 | */ |
||
| 23 | /*public function index() |
||
| 24 | { |
||
| 25 | // |
||
| 26 | }*/ |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Show the form for creating a new resource. |
||
| 30 | * @param int $eventCategoryId |
||
| 31 | * @param string $languageCode |
||
| 32 | * @return \Illuminate\View\View |
||
| 33 | */ |
||
| 34 | 1 | public function create(int $eventCategoryId, string $languageCode) |
|
| 35 | { |
||
| 36 | 1 | $selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
|
| 37 | |||
| 38 | 1 | return view('laravel-events-calendar::eventCategoryTranslations.create') |
|
| 39 | 1 | ->with('eventCategoryId', $eventCategoryId) |
|
| 40 | 1 | ->with('languageCode', $languageCode) |
|
| 41 | 1 | ->with('selectedLocaleName', $selectedLocaleName); |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Store a newly created resource in storage. |
||
| 46 | * |
||
| 47 | * @param \Illuminate\Http\Request $request |
||
| 48 | * @return \Illuminate\Http\RedirectResponse |
||
| 49 | */ |
||
| 50 | 4 | public function store(Request $request) |
|
| 51 | { |
||
| 52 | // Validate form datas |
||
| 53 | 4 | $validator = Validator::make($request->all(), [ |
|
| 54 | 4 | 'name' => 'required', |
|
| 55 | ]); |
||
| 56 | 4 | if ($validator->fails()) { |
|
| 57 | return back()->withErrors($validator)->withInput(); |
||
| 58 | } |
||
| 59 | |||
| 60 | 4 | $eventCategoryTranslation = new EventCategoryTranslation(); |
|
| 61 | 4 | $eventCategoryTranslation->event_category_id = $request->get('event_category_id'); |
|
| 62 | 4 | $eventCategoryTranslation->locale = $request->get('language_code'); |
|
| 63 | |||
| 64 | 4 | $eventCategoryTranslation->name = $request->get('name'); |
|
| 65 | 4 | $eventCategoryTranslation->slug = Str::slug($eventCategoryTranslation->name, '-'); |
|
| 66 | |||
| 67 | 4 | $eventCategoryTranslation->save(); |
|
| 68 | |||
| 69 | 4 | return redirect()->route('eventCategories.index') |
|
| 70 | 4 | ->with('success', 'Translation created successfully.'); |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Display the specified resource. |
||
| 75 | * |
||
| 76 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategoryTranslation $eventCategoryTranslation |
||
| 77 | * @return \Illuminate\Http\Response |
||
| 78 | */ |
||
| 79 | /*public function show(EventCategoryTranslation $eventCategoryTranslation) |
||
| 80 | { |
||
| 81 | // |
||
| 82 | }*/ |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Show the form for editing the specified resource. |
||
| 86 | * @param int $eventCategoryId |
||
| 87 | * @param string $languageCode |
||
| 88 | * @return \Illuminate\View\View |
||
| 89 | */ |
||
| 90 | 1 | public function edit(int $eventCategoryId, string $languageCode) |
|
| 91 | { |
||
| 92 | 1 | $eventCategoryTranslation = EventCategoryTranslation::where('event_category_id', $eventCategoryId) |
|
| 93 | 1 | ->where('locale', $languageCode) |
|
| 94 | 1 | ->first(); |
|
| 95 | |||
| 96 | 1 | $selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
|
| 97 | |||
| 98 | 1 | return view('laravel-events-calendar::eventCategoryTranslations.edit', compact('eventCategoryTranslation')) |
|
| 99 | 1 | ->with('eventCategoryId', $eventCategoryId) |
|
| 100 | 1 | ->with('languageCode', $languageCode) |
|
| 101 | 1 | ->with('selectedLocaleName', $selectedLocaleName); |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Update the specified resource in storage. |
||
| 106 | * |
||
| 107 | * @param \Illuminate\Http\Request $request |
||
| 108 | * @return \Illuminate\Http\RedirectResponse |
||
| 109 | */ |
||
| 110 | 1 | public function update(Request $request) |
|
| 111 | { |
||
| 112 | |||
| 113 | // Validate form datas |
||
| 114 | 1 | $validator = Validator::make($request->all(), [ |
|
| 115 | 1 | 'name' => 'required', |
|
| 116 | ]); |
||
| 117 | 1 | if ($validator->fails()) { |
|
| 118 | return back()->withErrors($validator)->withInput(); |
||
| 119 | } |
||
| 120 | |||
| 121 | 1 | $eventCategoryTranslation = EventCategoryTranslation::where('id', $request->get('event_category_translation_id')); |
|
| 122 | |||
| 123 | 1 | $event_category_t['name'] = $request->get('name'); |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 124 | 1 | $event_category_t['slug'] = Str::slug($request->get('name'), '-'); |
|
| 125 | |||
| 126 | 1 | $eventCategoryTranslation->update($event_category_t); |
|
| 127 | |||
| 128 | 1 | return redirect()->route('eventCategories.index') |
|
| 129 | 1 | ->with('success', 'Translation updated successfully'); |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Remove the specified resource from storage. |
||
| 134 | * |
||
| 135 | * @param int $eventCategoryTranslationId |
||
| 136 | * @return \Illuminate\Http\RedirectResponse |
||
| 137 | */ |
||
| 138 | 1 | public function destroy(int $eventCategoryTranslationId) |
|
| 139 | { |
||
| 140 | 1 | $eventCategoryTranslation = EventCategoryTranslation::find($eventCategoryTranslationId); |
|
| 141 | 1 | $eventCategoryTranslation->delete(); |
|
| 142 | |||
| 143 | 1 | return redirect()->route('eventCategories.index') |
|
| 144 | 1 | ->with('success', __('laravel-events-calendar::messages.event_category_translation_deleted_successfully')); |
|
| 145 | } |
||
| 146 | } |
||
| 147 |