Passed
Push — master ( be3f36...b6d0d2 )
by Karel
16:34
created

ContentController::repeaterJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
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 repeaterIndex()
88
    {
89
        $repeaters = $this->content->where('type', 'repeater')->get();
90
91
        return view('chuckcms::backend.content.repeater.index', compact('repeaters'));
92
    }
93
94
    public function repeaterCreate()
95
    {
96
        $pageViews = $this->template->getPageViews();
97
        return view('chuckcms::backend.content.repeater.create', compact('pageViews'));
98
    }
99
100
    public function repeaterEdit($slug)
101
    {
102
        $pageViews = $this->template->getPageViews();
103
        $repeater = Content::where('slug', $slug)->first();
104
        return view('chuckcms::backend.content.repeater.edit', compact('pageViews', 'repeater'));
105
    }
106
107
    public function repeaterJson($slug)
108
    {
109
        $repeater = Content::where('slug', $slug)->first();
110
111
        $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...
112
        $handle = fopen($filename, 'w+');
113
        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

113
        fputs(/** @scrutinizer ignore-type */ $handle, $repeater->toJson(JSON_PRETTY_PRINT));
Loading history...
114
        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

114
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
115
        $headers = array('Content-type'=> 'application/json');
116
        return response()->download($filename,$filename,$headers)->deleteFileAfterSend();
117
118
        //return view('chuckcms::backend.content.repeater.edit', compact('pageViews', 'repeater'));
119
    }
120
121
    public function repeaterSave(Request $request)
122
    {
123
        //add validation / move to repository...
124
        $content = [];
125
        $content_slug = $request->get('content_slug');
126
        $fields_slug = $request->get('fields_slug');
127
        $count = count($fields_slug);
128
        for ($i=0; $i < $count; $i++) { 
129
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['label'] = $request->get('fields_label')[$i];
130
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['type'] = $request->get('fields_type')[$i];
131
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['class'] = $request->get('fields_class')[$i];
132
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['placeholder'] = $request->get('fields_placeholder')[$i];
133
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['validation'] = $request->get('fields_validation')[$i];
134
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['value'] = $request->get('fields_value')[$i];
135
            $fieldsCount = count(explode(';',$request->get('fields_attributes_name')[$i]));
136
            for ($k=0; $k < $fieldsCount; $k++) { 
137
                $content['fields'][$content_slug . '_' . $fields_slug[$i]]['attributes'][explode(';',$request->get('fields_attributes_name')[$i])[$k]] = explode(';',$request->get('fields_attributes_value')[$i])[$k];
138
            }
139
            $content['fields'][$content_slug . '_' . $fields_slug[$i]]['required'] = $request->get('fields_required')[$i];
140
            $content['fields'][$content_slug . '_' . $fields_slug[$i]]['table'] = $request->get('fields_table')[$i];
141
        }
142
143
        $content['actions']['store'] = $request->get('action_store');
144
        if ($request->get('action_detail') == 'true') {
145
            $content['actions']['detail']['url'] = $request->get('action_detail_url');
146
            $content['actions']['detail']['page'] = $request->get('action_detail_page');
147
        } else {
148
            $content['actions']['detail'] = 'false';
149
        }
150
151
        $content['files'] = $request->get('files_allowed');
152
153
        Content::updateOrCreate(
154
            ['id' => $request->get('content_id')],
155
            ['slug' => $request->get('content_slug'),
156
            'type' => $request->get('content_type'),
157
            'content' => $content]
158
        );
159
        
160
        return redirect()->route('dashboard.content.repeaters');
161
    }
162
163
    public function repeaterImport(Request $request)
164
    {
165
        $this->validate(request(), [//@todo create custom Request class for page validation
166
            'slug' => 'required',
167
            'file' => 'required|file|mimetypes:application/json,application/octet-stream,text/plain'
168
        ]);
169
170
        $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

170
        $file_contents = file_get_contents(/** @scrutinizer ignore-type */ $request->file('file'));
Loading history...
171
172
        $new_slug = $request->get('slug');
173
        $old_slug = json_decode($file_contents,true)['slug'];
174
175
        $json_string = str_replace($old_slug, $new_slug, $file_contents);
176
        $json_file_array = json_decode($json_string,true);
177
178
        if ( !array_key_exists('type', $json_file_array) ) {
179
            $notification = array('type' => 'error', 'message' => 'The "type" key was not present in the JSON file.');
180
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
181
        }
182
183
        if ( !array_key_exists('content', $json_file_array) ) {
184
            $notification = array('type' => 'error', 'message' => 'The "content" key was not present in the JSON file.');
185
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
186
        }
187
188
        if ( !array_key_exists('fields', $json_file_array['content']) ) {
189
            $notification = array('type' => 'error', 'message' => 'The "fields" key was not present in the JSON file.');
190
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
191
        }
192
193
        if ( !array_key_exists('actions', $json_file_array['content']) ) {
194
            $notification = array('type' => 'error', 'message' => 'The "actions" 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('files', $json_file_array['content']) ) {
199
            $notification = array('type' => 'error', 'message' => 'The "files" key was not present in the JSON file.');
200
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
201
        }
202
203
        Content::updateOrCreate(
204
            ['id' => null],
205
            ['slug' => $new_slug,
206
            'type' => $json_file_array['type'],
207
            'content' => $json_file_array['content']]
208
        );
209
210
        $notification = array('type' => 'success', 'message' => 'The JSON file was successfully imported.');
211
        return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
212
    }
213
214
    public function repeaterEntriesIndex($slug)
215
    {
216
        $content = Content::where('slug', $slug)->first();
217
        $repeaters = $this->repeater->where('slug', $slug)->get();
218
        return view('chuckcms::backend.content.repeater.entries.index', compact('content', 'repeaters'));
219
    }
220
221
    public function repeaterEntriesCreate($slug)
222
    {
223
        $content = Content::where('slug', $slug)->first();
224
        return view('chuckcms::backend.content.repeater.entries.create', compact('content'));
225
    }
226
227
    public function repeaterEntriesSave(Request $request)
228
    {
229
        $slug = $request->get('content_slug');
230
        $content = $this->content->getBySlug($slug);
231
        $rules = $content->getRules();
232
        $this->validate(request(), $rules);
233
        $store = $content->storeEntry($request);
234
        if ($store == 'success') {
235
            return redirect()->route('dashboard.content.repeaters.entries', ['slug' => $slug]);
236
        } else {
237
            // error catching ... ?
238
        }
239
    }
240
241
    public function repeaterEntriesEdit($slug, $id)
242
    {
243
        $content = Content::where('slug', $slug)->first();
244
        $repeater = Repeater::where('id', $id)->first();
245
        return view('chuckcms::backend.content.repeater.entries.edit', compact('content', 'repeater'));
246
    }
247
248
    /**
249
     * Delete the resource from the page.
250
     *
251
     * @param  Request $request
252
     * @return string $status
253
     */
254
    public function repeaterEntriesDelete(Request $request)
255
    {
256
        // AUTHORIZE ... COMES HERE
257
        $status = $this->content->deleteById($request->get('repeater_id'));
258
        return $status;
259
    }
260
}
261