Completed
Push — master ( 633925...7a4edf )
by Davide
08:59
created

EventCategoryTranslationController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
rs 9.8666
cc 2
nc 2
nop 1
crap 2.0017
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use DavideCasiraghi\LaravelEventsCalendar\Models\EventCategoryTranslation;
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 int $languageCode
32
     * @return \Illuminate\Http\Response
33
     */
34 1
    public function create($eventCategoryId, $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\Response
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 int $languageCode
88
     * @return \Illuminate\Http\Response
89
     */
90 1
    public function edit($eventCategoryId, $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\Response
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
$event_category_t was never initialized. Although not strictly required by PHP, it is generally a good practice to add $event_category_t = array(); before regardless.
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  \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategoryTranslation  $eventCategoryTranslation
136
     * @return \Illuminate\Http\Response
137
     */
138 1
    public function destroy($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