Passed
Push — master ( 02143c...138cf8 )
by Karel
01:31 queued 10s
created

ContentController::repeaterDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Models\Content;
6
use Chuckbe\Chuckcms\Models\Resource;
7
use Chuckbe\Chuckcms\Models\Repeater;
8
use Chuckbe\Chuckcms\Models\Template;
9
use Chuckbe\Chuckcms\Models\User;
10
11
use ChuckSite;
0 ignored issues
show
Bug introduced by
The type ChuckSite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
use Illuminate\Http\Request;
14
use Illuminate\Support\Facades\View;
15
16
use Illuminate\Foundation\Bus\DispatchesJobs;
17
use Illuminate\Routing\Controller as BaseController;
18
use Illuminate\Foundation\Validation\ValidatesRequests;
19
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
20
21
class ContentController extends BaseController
22
{
23
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
24
25
    private $content;
26
    private $resource;
27
    private $repeater;
28
    private $template;
29
    private $user;
30
    
31
    /**
32
     * Create a new controller instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct(Content $content, Resource $resource, Repeater $repeater, Template $template, User $user)
37
    {
38
        $this->content = $content;
39
        $this->resource = $resource;
40
        $this->repeater = $repeater;
41
        $this->template = $template;
42
        $this->user = $user;
43
    }
44
45
    public function resourceIndex()
46
    {
47
        $resources = $this->resource->get();
48
        return view('chuckcms::backend.content.resource.index', compact('resources'));
49
    }
50
51
    public function resourceCreate()
52
    {
53
        return view('chuckcms::backend.content.resource.create');
54
    }
55
56
    public function resourceEdit($slug)
57
    {
58
        $resource = Resource::where('slug', $slug)->first();
59
        return view('chuckcms::backend.content.resource.edit', compact('resource'));
60
    }
61
62
    public function resourceSave(Request $request)
63
    {
64
        //validate the request
65
        $this->validate(request(), [//@todo create custom Request class for site validation
66
            'slug' => 'required',
67
            'resource_key.*' => 'required',
68
            'resource_value.*' => 'required'
69
        ]);
70
71
        $resource = Resource::firstOrNew(['slug' => $request->get('slug')[0]]);
72
        $resource->slug = $request->get('slug')[0];
73
        $json = [];
74
        foreach(ChuckSite::getSupportedLocales() as $langKey => $langValue){
75
            $count = count($request->get('resource_key')[$langKey]);
76
            for ($i=0; $i < $count; $i++) { 
77
                $json[$langKey][$request->get('resource_key')[$langKey][$i]] = $request->get('resource_value')[$langKey][$i];
78
79
            }
80
        }
81
        $resource->json = $json;
82
        $resource->save();
83
84
        return redirect()->route('dashboard.content.resources');
85
    }
86
87
    public function resourceDelete(Request $request)
88
    {
89
        $this->validate(request(), [//@todo create custom Request class for site validation
90
            'resource_id' => 'required'
91
        ]);
92
93
        $resource = Resource::where('id', $request->get('resource_id'))->first();
94
95
        if ($resource->delete()) {
96
            return 'success';
97
        } else {
98
            return 'error';
99
        }
100
    }
101
102
    public function repeaterIndex()
103
    {
104
        $repeaters = $this->content->where('type', 'repeater')->get();
105
106
        return view('chuckcms::backend.content.repeater.index', compact('repeaters'));
107
    }
108
109
    public function repeaterCreate()
110
    {
111
        $pageViews = $this->template->getPageViews();
112
        return view('chuckcms::backend.content.repeater.create', compact('pageViews'));
113
    }
114
115
    public function repeaterEdit($slug)
116
    {
117
        $pageViews = $this->template->getPageViews();
118
        $repeater = Content::where('slug', $slug)->first();
119
        return view('chuckcms::backend.content.repeater.edit', compact('pageViews', 'repeater'));
120
    }
121
122
    public function repeaterJson($slug)
123
    {
124
        $repeater = Content::where('slug', $slug)->first();
125
126
        $filename = $repeater->slug.".json";
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Chuckbe\Chuckcms\Models\Content. Since you implemented __get, consider adding a @property annotation.
Loading history...
127
        $handle = fopen($filename, 'w+');
128
        fputs($handle, $repeater->toJson(JSON_PRETTY_PRINT));
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputs() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        fputs(/** @scrutinizer ignore-type */ $handle, $repeater->toJson(JSON_PRETTY_PRINT));
Loading history...
129
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
130
        $headers = array('Content-type'=> 'application/json');
131
        return response()->download($filename,$filename,$headers)->deleteFileAfterSend();
132
133
        //return view('chuckcms::backend.content.repeater.edit', compact('pageViews', 'repeater'));
134
    }
135
136
    public function repeaterSave(Request $request)
137
    {
138
        //add validation / move to repository...
139
        $content = [];
140
        $content_slug = $request->get('content_slug');
141
        $fields_slug = $request->get('fields_slug');
142
        $count = count($fields_slug);
143
        for ($i=0; $i < $count; $i++) { 
144
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['label'] = $request->get('fields_label')[$i];
145
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['type'] = $request->get('fields_type')[$i];
146
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['class'] = $request->get('fields_class')[$i];
147
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['placeholder'] = $request->get('fields_placeholder')[$i];
148
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['validation'] = $request->get('fields_validation')[$i];
149
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['value'] = $request->get('fields_value')[$i];
150
            $fieldsCount = count(explode(';',$request->get('fields_attributes_name')[$i]));
151
            for ($k=0; $k < $fieldsCount; $k++) { 
152
                $content['fields'][$content_slug . '_' . $fields_slug[$i]]['attributes'][explode(';',$request->get('fields_attributes_name')[$i])[$k]] = explode(';',$request->get('fields_attributes_value')[$i])[$k];
153
            }
154
            $content['fields'][$content_slug . '_' . $fields_slug[$i]]['required'] = $request->get('fields_required')[$i];
155
            $content['fields'][$content_slug . '_' . $fields_slug[$i]]['table'] = $request->get('fields_table')[$i];
156
        }
157
158
        $content['actions']['store'] = $request->get('action_store');
159
        if ($request->get('action_detail') == 'true') {
160
            $content['actions']['detail']['url'] = $request->get('action_detail_url');
161
            $content['actions']['detail']['page'] = $request->get('action_detail_page');
162
        } else {
163
            $content['actions']['detail'] = 'false';
164
        }
165
166
        $content['files'] = $request->get('files_allowed');
167
168
        Content::updateOrCreate(
169
            ['id' => $request->get('content_id')],
170
            ['slug' => $request->get('content_slug'),
171
            'type' => $request->get('content_type'),
172
            'content' => $content]
173
        );
174
        
175
        return redirect()->route('dashboard.content.repeaters');
176
    }
177
178
    public function repeaterImport(Request $request)
179
    {
180
        $this->validate(request(), [//@todo create custom Request class for page validation
181
            'slug' => 'required',
182
            'file' => 'required|file|mimetypes:application/json,application/octet-stream,text/plain'
183
        ]);
184
185
        $file_contents = file_get_contents($request->file('file'));
0 ignored issues
show
Bug introduced by
It seems like $request->file('file') can also be of type Illuminate\Http\UploadedFile[] and array; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
        $file_contents = file_get_contents(/** @scrutinizer ignore-type */ $request->file('file'));
Loading history...
186
187
        $new_slug = $request->get('slug');
188
        $old_slug = json_decode($file_contents,true)['slug'];
189
190
        $json_string = str_replace($old_slug, $new_slug, $file_contents);
191
        $json_file_array = json_decode($json_string,true);
192
193
        if ( !array_key_exists('type', $json_file_array) ) {
194
            $notification = array('type' => 'error', 'message' => 'The "type" key was not present in the JSON file.');
195
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
196
        }
197
198
        if ( !array_key_exists('content', $json_file_array) ) {
199
            $notification = array('type' => 'error', 'message' => 'The "content" key was not present in the JSON file.');
200
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
201
        }
202
203
        if ( !array_key_exists('fields', $json_file_array['content']) ) {
204
            $notification = array('type' => 'error', 'message' => 'The "fields" key was not present in the JSON file.');
205
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
206
        }
207
208
        if ( !array_key_exists('actions', $json_file_array['content']) ) {
209
            $notification = array('type' => 'error', 'message' => 'The "actions" key was not present in the JSON file.');
210
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
211
        }
212
213
        if ( !array_key_exists('files', $json_file_array['content']) ) {
214
            $notification = array('type' => 'error', 'message' => 'The "files" key was not present in the JSON file.');
215
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
216
        }
217
218
        Content::updateOrCreate(
219
            ['id' => null],
220
            ['slug' => $new_slug,
221
            'type' => $json_file_array['type'],
222
            'content' => $json_file_array['content']]
223
        );
224
225
        $notification = array('type' => 'success', 'message' => 'The JSON file was successfully imported.');
226
        return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
227
    }
228
229
    public function repeaterDelete(Request $request)
230
    {
231
        $this->validate(request(), [//@todo create custom Request class for site validation
232
            'content_id' => 'required'
233
        ]);
234
235
        $content = Content::where('id', $request->get('content_id'))->first();
236
        $repeaters = Repeater::where('slug', $content->slug)->delete();
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Chuckbe\Chuckcms\Models\Content. Since you implemented __get, consider adding a @property annotation.
Loading history...
Unused Code introduced by
The assignment to $repeaters is dead and can be removed.
Loading history...
237
238
        if ($content->delete()) {
239
            return 'success';
240
        } else {
241
            return 'error';
242
        }
243
    }
244
245
    public function repeaterEntriesIndex($slug)
246
    {
247
        $content = Content::where('slug', $slug)->first();
248
        $repeaters = $this->repeater->where('slug', $slug)->get();
249
        return view('chuckcms::backend.content.repeater.entries.index', compact('content', 'repeaters'));
250
    }
251
252
    public function repeaterEntriesCreate($slug)
253
    {
254
        $content = Content::where('slug', $slug)->first();
255
        return view('chuckcms::backend.content.repeater.entries.create', compact('content'));
256
    }
257
258
    public function repeaterEntriesSave(Request $request)
259
    {
260
        $slug = $request->get('content_slug');
261
        $content = $this->content->getBySlug($slug);
262
        $rules = $content->getRules();
263
        $this->validate(request(), $rules);
264
        $store = $content->storeEntry($request);
265
        if ($store == 'success') {
266
            return redirect()->route('dashboard.content.repeaters.entries', ['slug' => $slug]);
267
        } else {
268
            // error catching ... ?
269
        }
270
    }
271
272
    public function repeaterEntriesEdit($slug, $id)
273
    {
274
        $content = Content::where('slug', $slug)->first();
275
        $repeater = Repeater::where('id', $id)->first();
276
        return view('chuckcms::backend.content.repeater.entries.edit', compact('content', 'repeater'));
277
    }
278
279
    /**
280
     * Delete the resource from the page.
281
     *
282
     * @param  Request $request
283
     * @return string $status
284
     */
285
    public function repeaterEntriesDelete(Request $request)
286
    {
287
        // AUTHORIZE ... COMES HERE
288
        $status = $this->content->deleteById($request->get('repeater_id'));
289
        return $status;
290
    }
291
}
292