Completed
Push — master ( 740285...fcbeea )
by Davide
06:35
created

BlogTranslationController::__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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use DavideCasiraghi\LaravelSmartBlog\Models\BlogTranslation;
9
10
class BlogTranslationController extends Controller
11
{
12
    /***************************************************************************/
13
    /* Restrict the access to this resource just to logged in users */
14 7
    public function __construct()
15
    {
16 7
        $this->middleware('admin');
17 7
    }
18
19
    /***************************************************************************/
20
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    /*public function index()
27
    {
28
        //
29
    }*/
30
31
    /***************************************************************************/
32
33
    /**
34
     * Show the form for creating a new resource.
35
     * @param int $blogId
36
     * @param string $languageCode
37
     * @return \Illuminate\Http\Response
38
     */
39 1
    public function create($blogId, $languageCode)
40
    {
41 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
42
43 1
        return view('laravel-smart-blog::blogTranslations.create')
44 1
                ->with('blogId', $blogId)
45 1
                ->with('languageCode', $languageCode)
46 1
                ->with('selectedLocaleName', $selectedLocaleName);
47
    }
48
49
    // **********************************************************************
50
51
    /**
52
     * Show the form for editing the specified resource.
53
     *
54
     * @param int $blogId
55
     * @param string $languageCode
56
     * @return \Illuminate\Http\Response
57
     */
58 1
    public function edit($blogId, $languageCode)
59
    {
60 1
        $blogTranslation = BlogTranslation::where('blog_id', $blogId)
61 1
                        ->where('locale', $languageCode)
62 1
                        ->first();
63
64 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
65
66 1
        return view('laravel-smart-blog::blogTranslations.edit', compact('blogTranslation'))
67 1
                    ->with('blogId', $blogId)
68 1
                    ->with('languageCode', $languageCode)
69 1
                    ->with('selectedLocaleName', $selectedLocaleName);
70
    }
71
72
    /***************************************************************************/
73
74
    /**
75
     * Store a newly created resource in storage.
76
     *
77
     * @param  \Illuminate\Http\Request  $request
78
     * @return \Illuminate\Http\Response
79
     */
80 6
    public function store(Request $request)
81
    {
82
        // Validate form datas
83 6
        $validator = Validator::make($request->all(), [
84 6
                'name' => 'required',
85
            ]);
86 6
        if ($validator->fails()) {
87 1
            return back()->withErrors($validator)->withInput();
88
        }
89
90 5
        $blogTranslation = new BlogTranslation();
91 5
        $blogTranslation->blog_id = $request->get('blog_id');
92 5
        $blogTranslation->locale = $request->get('language_code');
93
94 5
        $blogTranslation->name = $request->get('name');
95 5
        $blogTranslation->description = $request->get('description');
96 5
        $blogTranslation->slug = Str::slug($blogTranslation->name, '-');
97
98 5
        $blogTranslation->save();
99
100 5
        return redirect()->route('blogs.index')
101 5
                        ->with('success', 'Translation created successfully.');
102
    }
103
104
    /***************************************************************************/
105
106
    /**
107
     * Display the specified resource.
108
     *
109
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\BlogTranslation  $blogTranslation
110
     * @return \Illuminate\Http\Response
111
     */
112
    /*public function show(BlogTranslation $blogTranslation)
113
    {
114
        //
115
    }*/
116
117
    /***************************************************************************/
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param  \Illuminate\Http\Request  $request
123
     * @return \Illuminate\Http\Response
124
     */
125 2
    public function update(Request $request)
126
    {
127 2
        request()->validate([
128 2
            'name' => 'required',
129
        ]);
130
131 1
        $blogTranslation = BlogTranslation::where('id', $request->get('blog_translation_id'));
132
133 1
        $blog_t = [];
134 1
        $blog_t['name'] = $request->get('name');
135 1
        $blog_t['description'] = $request->get('description');
136 1
        $blog_t['slug'] = Str::slug($request->get('name'), '-');
137
138 1
        $blogTranslation->update($blog_t);
139
140 1
        return redirect()->route('blogs.index')
141 1
                        ->with('success', 'Translation updated successfully');
142
    }
143
144
    /***************************************************************************/
145
146
    /**
147
     * Remove the specified resource from storage.
148
     *
149
     * @param int $blogTranslationId
150
     * @return \Illuminate\Http\Response
151
     */
152 1
    public function destroy($blogTranslationId)
153
    {
154 1
        $blogTranslation = BlogTranslation::find($blogTranslationId);
155 1
        $blogTranslation->delete();
156
157 1
        return redirect()->route('blogs.index')
158 1
                        ->with('success', __('messages.blog_translation_deleted_successfully'));
159
    }
160
}
161