Passed
Push — master ( 9bcfb0...988f4e )
by Alexander
06:53
created

TranslatorAdminController::changeLiveMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * Laravel Controller for handling request to the translator admin interface
5
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
6
namespace Hokan22\LaravelTranslator\Controllers;
7
8
use Illuminate\Routing\Controller;
9
use Hokan22\LaravelTranslator\Models\TranslationIdentifier;
10
use Hokan22\LaravelTranslator\TranslatorFacade;
11
use Carbon\Carbon;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\DB;
14
use Illuminate\Support\Facades\Input;
15
16
/**
17
 * Class TranslatorAdminController
18
 *
19
 * @category LaravelController
20
 * @package  Hokan22\LaravelTranslator\Controllers
21
 * @author   Alexander Viertel <[email protected]>
22
 * @license  http://opensource.org/licenses/MIT MIT
23
 * @link     https://github.com/Hokan22/laravel-translator
24
 */
25
class TranslatorAdminController extends Controller
26
{
27
    /**
28
     * Return an overview of translations
29
     *
30
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
31
     */
32
    public function index()
33
    {
34
        $search = Input::get('search', '');
35
        $locale = Input::get('locale', '');
36
37
        $query = TranslationIdentifier::with('translations');
38
39
        if ($locale != '') {
40
            $query = $query->whereDoesntHave('translations', function ($query) use ($locale)
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
41
                {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
42
                    $query->where('translations.locale', 'like', $locale);
43
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 12 spaces, found 16
Loading history...
44
            );
45
        }
46
47
        if ($search != '') {
48
            $query = $query->where(function ($query) use ($search) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
49
                $query ->where('identifier',     'LIKE', '%'.$search.'%')
50
                    ->orWhere('parameters', 'LIKE', '%'.$search.'%')
51
                    ->orWhere('group',      'LIKE', '%'.$search.'%')
52
                    ->orWhere('page_name',  'LIKE', '%'.$search.'%')
53
                    ->orWhere('description','LIKE', '%'.$search.'%');
54
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
55
        }
56
57
        $trans_identifier = $query->orderBy('id')->paginate(20)->appends(Input::except('page'));
58
59
        $available_locales = TranslatorFacade::getConfigValue('available_locales');
60
61
        return view('translator::index',
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
62
            [
63
                'identifier'        =>  $trans_identifier,
64
                'available_locales' =>  $available_locales,
65
                'page'              =>  Input::get('page'),
66
                'query_locale'      =>  Input::get('locale'),
67
                'search'            =>  Input::get('search'),
68
            ]
69
        );
70
    }
71
72
    /**
73
     * Return the edit view for a translation with $id
74
     *
75
     * @param integer $id ID of the translation Identifier to edit
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
76
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
77
     */
78
    public function edit($id)
79
    {
80
        $identifier = TranslationIdentifier::findOrFail($id);
81
82
        $available_locales = TranslatorFacade::getConfigValue('available_locales');
83
        
84
        return view('translator::edit',
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
85
            [
86
                'identifier'        =>  $identifier,
87
                'available_locales' =>  $available_locales,
88
                'page'              =>  Input::get('page'),
89
                'locale'            =>  Input::get('locale'),
90
                'search'            =>  Input::get('search'),
91
            ]
92
        );
93
94
    }
95
96
    /**
97
     * Update
98
     *
99
     * @param integer $id ID of the identifier to edit
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
100
     * @param Request $request Object with the values of the identifier
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
101
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
102
     */
103
    public function postEdit($id, Request $request)
104
    {
105
        TranslationIdentifier::findOrFail($id);
106
107
        foreach ($request->all() as $key => $value) {
108
109
            if ($value === null || in_array($key, ['_token', 'page', 'locale', 'search'])) {
110
                continue;
111
            }
112
113
            $value = str_replace("\r", "", $value);
114
            $value = str_replace("\n", "<br />", $value);
115
116
            $timestamp = Carbon::now();
117
118
            // Eloquent doesn't support composite keys, therefore a raw query is used
119
            // This query will create the translation or update the translation if it already exists in the database
120
            DB::statement("INSERT INTO `translations` (`translation_identifier_id`, `locale`, `translation`, `updated_at`, `created_at`)
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
121
                            VALUES ($id, '$key', '$value', '$timestamp', '$timestamp') 
122
                            ON DUPLICATE KEY 
123
                            UPDATE `translation` = '$value'");
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
124
125
            DB::statement("UPDATE `translation_identifiers` SET `updated_at` = '$timestamp' WHERE `id` LIKE $id");
126
        }
127
128
        return $this->edit($id);
129
    }
130
131
    /**
132
     * Post edit of multiple Identifiers from the index view
133
     *
134
     * @param Request $request Request with multiple values of identifiers to update
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
135
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
136
     */
137
    public function postIdentifier(Request $request)
138
    {
139
        /** @var TranslationIdentifier $translation_identifiers */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
140
        $translation_identifiers = TranslationIdentifier::all()->whereIn('id', array_keys($request->all()));
141
142
        foreach ($request->all() as $id => $identifier) {
143
144
            if (!is_array($identifier)) {
145
                continue;
146
            };
147
148
            $translation_identifier = $translation_identifiers->find($id);
149
150
            $translation_identifier->parameters     = isset($identifier['parameters']) ? explode($id, $identifier['parameters']) : [];
151
            $translation_identifier->group          = isset($identifier['group']) ? $identifier['group'] : 'default';
152
            $translation_identifier->page_name      = isset($identifier['page_name']) ? $identifier['page_name'] : null;
153
            $translation_identifier->description    = isset($identifier['description']) ? $identifier['description'] : null;
154
155
            $translation_identifier->save();
156
        }
157
        return $this->index();
158
    }
159
160
    /**
161
     * Test view with some test translations
162
     *
163
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
164
     */
165
    public function test()
166
    {
167
        return view('translator::test');
168
    }
169
170
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Doc comment for parameter "$state" missing
Loading history...
171
     * @param $state string 'enabled|disabled'
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
172
     * @return \Illuminate\Http\RedirectResponse
173
     */
174
    public function changeLiveMode ($state) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before opening parenthesis; 1 found
Loading history...
Coding Style introduced by
Opening brace should be on a new line
Loading history...
175
176
        if ($state == 'enable') {
177
            session(['translation_live_mode' => true]);
178
        } else {
179
            session(['translation_live_mode' => false]);
180
        }
181
182
        return redirect()->back();
183
    }
184
}