Passed
Push — master ( 8372f4...8e3d45 )
by Davide
07:49 queued 11s
created

RegionController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 Illuminate\Support\Facades\DB;
9
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
10
use DavideCasiraghi\LaravelEventsCalendar\Models\Region;
11
use DavideCasiraghi\LaravelEventsCalendar\Models\Country;
12
13
class RegionController extends Controller
14
{
15
    /* Restrict the access to this resource just to logged in users except show view */
16 9
    public function __construct()
17
    {
18 9
        $this->middleware('admin', ['except' => ['show']]);
19 9
    }
20
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26 3
    public function index(Request $request)
27
    {
28 3
        $countries = Country::getCountries();
29
        
30 3
        $searchKeywords = $request->input('keywords');
31 3
        if ($searchKeywords) {
32
            $regions = Region::orderBy('name')
33
                                ->where('name', 'like', '%'.$request->input('keywords').'%')
34
                                ->paginate(20);
35
        } else {
36 3
            $regions = Region::orderBy('name')
37 3
                                ->paginate(20);
38
        }
39
40
        // Countries available for translations
41 3
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
42
43 3
        return view('laravel-events-calendar::regions.index', compact('regions'))
44 3
            ->with('i', (request()->input('page', 1) - 1) * 20)
45 3
            ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations)
46 3
            ->with('searchKeywords', $searchKeywords)
47 3
            ->with('countries', $countries);
48
    }
49
50
    /**
51
     * Show the form for creating a new resource.
52
     *
53
     * @return \Illuminate\Http\Response
54
     */
55 1
    public function create()
56
    {
57 1
        $countries = Country::getCountries();
58
        
59 1
        return view('laravel-events-calendar::regions.create')
60 1
                ->with('countries', $countries);
61
    }
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     * @return \Illuminate\Http\Response
68
     */
69 2
    public function store(Request $request)
70
    {
71
        // Validate form datas
72 2
        $validator = Validator::make($request->all(), [
73 2
                'name' => 'required',
74
                'country_id' => 'required',
75
                'timezone' => 'required',
76
            ]);
77 2
        if ($validator->fails()) {
78 1
            return back()->withErrors($validator)->withInput();
79
        }
80
81 1
        $region = new Region();
82
83 1
        $this->saveOnDb($request, $region);
84
85 1
        return redirect()->route('regions.index')
86 1
                        ->with('success', __('laravel-events-calendar::messages.region_added_successfully'));
87
    }
88
89
    /**
90
     * Display the specified resource.
91
     *
92
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Region  $region
93
     * @return \Illuminate\Http\Response
94
     */
95 1
    public function show(Region $region)
96
    {
97 1
        return view('laravel-events-calendar::regions.show', compact('region'));
98
    }
99
100
    /**
101
     * Show the form for editing the specified resource.
102
     *
103
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Region  $region
104
     * @return \Illuminate\Http\Response
105
     */
106 1
    public function edit(Region $region)
107
    {
108 1
        $countries = Country::getCountries();
109
        
110 1
        return view('laravel-events-calendar::regions.edit', compact('region'))
111 1
                    ->with('countries', $countries);
112
    }
113
114
    /**
115
     * Update the specified resource in storage.
116
     *
117
     * @param  \Illuminate\Http\Request  $request
118
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Region  $region
119
     * @return \Illuminate\Http\Response
120
     */
121 2
    public function update(Request $request, Region $region)
122
    {
123 2
        request()->validate([
124 2
            'name' => 'required',
125
            'country_id' => 'required',
126
            'timezone' => 'required',
127
        ]);
128
129 1
        $this->saveOnDb($request, $region);
130
131 1
        return redirect()->route('regions.index')
132 1
                        ->with('success', __('laravel-events-calendar::messages.region_updated_successfully'));
133
    }
134
135
    /**
136
     * Remove the specified resource from storage.
137
     *
138
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Region  $region
139
     * @return \Illuminate\Http\Response
140
     */
141 1
    public function destroy(Region $region)
142
    {
143 1
        $region->delete();
144
145 1
        return redirect()->route('regions.index')
146 1
                        ->with('success', __('laravel-events-calendar::messages.region_deleted_successfully'));
147
    }
148
149
    // **********************************************************************
150
151
    /**
152
     * Return the single event region datas by cat id.
153
     *
154
     * @param  int $cat_id
155
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Region
156
     */
157
    /*public function eventregiondata($cat_id)
158
    {
159
        $ret = DB::table('regions')->where('id', $cat_id)->first();
160
        //dump($ret);
161
162
        return $ret;
163
    }*/
164
165
    // **********************************************************************
166
167
    /**
168
     * Save/Update the record on DB.
169
     *
170
     * @param  \Illuminate\Http\Request  $request
171
     * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region
172
     * @return void
173
     */
174 2
    public function saveOnDb($request, $region)
175
    {
176 2
        $region->name = $request->get('name');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on DavideCasiraghi\LaravelE...sCalendar\Models\Region. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
177 2
        $region->country_id = $request->get('country_id');
178 2
        $region->timezone = $request->get('timezone');
179 2
        $region->slug = Str::slug($region->name, '-');
0 ignored issues
show
Bug introduced by
The property slug does not seem to exist on DavideCasiraghi\LaravelE...sCalendar\Models\Region. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
180
181 2
        $region->save();
182 2
    }
183
}
184