InputController.php 5.59 KB
Newer Older
1
2
3
4
5
6
<?php

namespace App\Http\Controllers;

use App\Input;

7
8
use App\Logic\Decoder\Decoders;
use App\Logic\Encoder\Encoders;
9
10

use App\Http\Requests\StoreInput;
Maik Messerschmidt's avatar
Maik Messerschmidt committed
11
use App\Http\Requests\UpdateInput;
12
13
14
15
use App\Http\Requests\ImportInputs;

use Illuminate\Http\Request;
use Str;
16
17
18
19
20
21
22
23

class InputController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
24
25
26
27
    public function index() {
        return view('inputs.index', [
            "inputs" => Input::without('data')->paginate(10)
        ]);
28
29
30
31
32
33
34
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
35
    public function create() {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        return view('inputs.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreInput $request) {
        Input::create($request->all());
        return redirect()->route('inputs.index');
    }

    /**
51
     * Show form to import inputs.
52
     */
53
54
    public function importForm() {
        return view('inputs.import');
55
56
57
    }

    /**
58
     * Execute the import.
59
     */
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    public function import(ImportInputs $request) {
        $decoder = Decoders::get($request->decoder);
        $encoder = Encoders::get($request->encoder);

        $entries = collect($decoder::toObjects($request->data->get()));

        if ($request->id_column) {
            // Check that each entry has a non-empty id.
            foreach ($entries as $idx => $entry) {
                $id = $entry[$request->id_column];

                if (Str::contains($id, ' '))
                    throw ValidationException::withMessages([
                        'data' => ["{$request->id_column} '{$id}' contains whitespace for entry ${idx}."]
                    ]);
                else if (! trim($id))
                    throw ValidationException::withMessages([
                        'data' => ["{$request->id_column} is empty for entry ${idx}."]
                    ]);
            }

            $ids = $entries->map(function($entry) use ($request) {
                return $entry[$request->id_column];
            });

            $groups = collect($entries)
                ->groupBy($request->id_column)
                ->map(function($entries) { return $entries->toArray(); });
        }
        else {
            // No id column given, try 3 * $entries->count() times to
            // generate $entries->count() unique ids, then give up.
            $ids = collect();
            $tries = 0;
            $max_tries = $entries->count() * 3;

            while ($tries < $max_tries && $ids->count() < $entries->count()) {
                $id = Str::random($request->id_length);
                if ($ids->contains($id) || Input::find($id))
                    ; // pass
                else
                    $ids->push($id);

                $tries += 1;
            }

            if ($ids->count() < $entries->count())
                throw ValidationException::withMessages([
                    'id_column' => [
                        "Couldn't generate enough uids. " .
                        "Try again or increase uid length."
                    ]
                ]);

            $groups = $ids
                ->combine($entries)
                // We need an array of arrays, right now we only have an array.
                ->map(function($entry) {
                    return [$entry];
                });
        }

        $encoded = $groups->map(function($entries) use ($encoder) {
            return $encoder::fromObjects($entries);
        });

        // preview
        if ($request->preview) {
            return back()->withInput()->with('preview', $encoded->take(2));
        }
        // upload
        else {
            foreach ($encoded as $id => $data) {
                Input::create(array_merge($request->all(), [
                    "id" => $id,
                    "data" => $data
                ]));
            }
138

139
140
            return redirect()->route('inputs.index');
        }
141
142
143
144
145
146
147
148
149
150
151
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Input $input) {
        $json = json_decode($input->data);

152
153
154
155
156
157
158
        // Funny thing: 'null' decodes to null just as any invalid json...
        // Another funny thing: response()->json(null) will return '{}'...
        // (Because php can't see, whether the parameter was provided or not...)
        if ($input->data === 'null') {
            return response($input->data)->header('Content-Type', 'application/json');
        }
        else if ($json === null)
159
            return $input->data;
160
161
        else
            return response()->json($json);
162
163
164
165
166
167
168
169
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Maik Messerschmidt's avatar
Maik Messerschmidt committed
170
171
    public function edit(Input $input) {
        return view('inputs.edit', ['input' => $input]);
172
173
174
175
176
177
178
179
180
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Maik Messerschmidt's avatar
Maik Messerschmidt committed
181
182
183
    public function update(UpdateInput $request, Input $input) {
        $input->update($request->all());
        return redirect()->route('inputs.index');
184
185
186
187
188
189
190
191
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
192
193
194
    public function destroy(Input $input) {
        $input->delete();
        return back();
195
196
    }
}