Passed
Push — master ( a09b17...708388 )
by Martin
05:47
created

SettingsController::deleteLogo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Event;
6
use App\Http\Controllers\Controller;
7
use App\Location;
8
use App\PriceCategory;
9
use App\PriceList;
10
use App\Project;
11
use App\Purchase;
12
use App\SeatMap;
13
use App\Setting;
14
use App\Ticket;
15
use App\User;
16
use Illuminate\Http\Request;
17
use Illuminate\Support\Facades\App;
18
use Illuminate\Support\Facades\DB;
19
use Illuminate\Support\Str;
20
use Mews\Purifier\Facades\Purifier;
21
use Spipu\Html2Pdf\Exception\Html2PdfException;
22
use Spipu\Html2Pdf\Html2Pdf;
23
24
class SettingsController extends Controller
25
{
26
    // Display all available settings
27
    public function index()
28
    {
29
        $terms = Setting::where('name', 'terms')->where('lang', App::getLocale())->first();
30
        $termsHtml = $terms ? $terms->value : view('components.default-texts.terms')->render();
31
32
        $privacy = Setting::where('name', 'privacy')->where('lang', App::getLocale())->first();
33
        $privacyHtml = $privacy ? $privacy->value : view('components.default-texts.privacy')->render();
34
35
        return view('admin.settings', [
36
            'terms' => $termsHtml,
37
            'privacy' => $privacyHtml
38
        ]);
39
    }
40
41
    /**
42
     * Receives HTML-input
43
     * 
44
     * Attention: Function might be target for XSS attacks. Handle input carfully!!!
45
     */
46
    public function updateTerms(Request $request)
47
    {
48
        Setting::updateOrCreate(
49
            ['name' => 'terms', 'lang' => App::getLocale()],
50
            ['value' => Purifier::clean($request->input('terms'))]
51
        );
52
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Terms and Conditions updated.');
53
    }
54
55
    /**
56
     * Receives HTML-input
57
     * 
58
     * Attention: Function might be target for XSS attacks. Handle input carfully!!!
59
     */
60
    public function updatePrivacy(Request $request)
61
    {
62
        Setting::updateOrCreate(
63
            ['name' => 'privacy', 'lang' => App::getLocale()],
64
            ['value' => Purifier::clean($request->input('privacy'))]
65
        );
66
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Privacy statement updated.');
67
    }
68
69
    /**
70
     * File-Upload
71
     */
72
    public function updateLogo(Request $request)
73
    {
74
        $validatedFile = $request->validate([
75
            'file' => 'file|max:30000|mimes:jpeg,bmp,png,svg,jpg'
76
        ]);
77
78
        // Only extract file extension of the new logo picture
79
        $extension = $validatedFile['file']->extension();
80
        // set it to a generic name to overwrite any existing logo with the same extension
81
        $logoStoreName = 'logo.' . $extension;
82
83
        // Store file as new logo and update the corresponding setting
84
        $validatedFile['file']->storeAs('images', $logoStoreName);
85
        Setting::updateOrCreate(
86
            ['name' => 'logo', 'lang' => 'en'],
87
            ['value' => 'images/' . $logoStoreName]
88
        );
89
90
        // Redirect to source page with success message
91
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Logo updated.');
92
    }
93
94
    // Purge the current logo
95
    public function deleteLogo(Request $request)
96
    {
97
        Setting::where([
98
            ['name', 'logo'],
99
            ['lang', 'en']
100
        ])->delete();
101
        // Redirect to source page with success message
102
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Logo deleted.');
103
    }
104
105
    /**
106
     * Returns a ticket filled with dummy data to check the
107
     * correct processing of the logo in the layout
108
     */
109
    public function testTicket()
110
    {
111
        // Wrap dummy data creation in a transaction in order
112
        // to not actually store it in the production database.
113
        //
114
        // We have to use eloquent models and cannot use factories,
115
        // because factories are not available on prod installations.
116
        DB::beginTransaction();
117
        $vendor = User::create([
118
            'name'     => 'TestVendor FamilynameOfVendor',
119
            'email'    => '[email protected]',
120
            'password' => ''
121
        ]);
122
        $customer = User::create([
123
            'name'     => 'Avery LongName ButItShouldWork',
124
            'email'    => '[email protected]',
125
            'password' => ''
126
        ]);
127
128
        $priceList = PriceList::create([
129
            'name' => 'Testlist'
130
        ]);
131
        $priceCategory = PriceCategory::create([
132
            'name'        => 'TestCategory',
133
            'price'       => 450,
134
            'description' => 'Just for testing the ticket layout'
135
        ]);
136
        $priceList->categories()->save($priceCategory);
137
138
        $location = Location::create([
139
            'name'    => 'Some Test Location anywhere',
140
            'address' => 'Somewhere over the rainbox street 42, 424242 Kummerland, Wilde13'
141
        ]);
142
        $project = Project::create([
143
            'name'        => 'A Test project',
144
            'description' => 'Something something testing',
145
            'is_archived' => false
146
        ]);
147
        $seatMap = SeatMap::create([
148
            'name'        => 'TestSeatMap',
149
            'seats'       => 2000,
150
            'description' => 'Some test description that does not really matter',
151
            'layout'      => null
152
        ]);
153
        $now = now();
154
155
        $purchase = new Purchase();
156
        $purchase->state = 'paid';
157
        $purchase->state_updated = $now;
158
        $purchase->random_id = Str::random(20);
159
        $purchase->payment_secret = Str::random(20);
160
        $purchase->customer_id = $customer->id;
161
        $purchase->vendor_id = $vendor->id;
162
        $purchase->payment_id = 'dummy-reference';
163
        $purchase->save();
164
165
        $event = new Event();
166
        $event->second_name        = 'First event';
167
        $event->customer_sell_stop = $now->add(1, 'day');
168
        $event->retailer_sell_stop = $now->add(2, 'day');
169
        $event->start_date         = $now->add(1, 'day');
170
        $event->end_date           = $now->add(10, 'day');
171
        $event->project_id         = $project->id;
172
        $event->location_id        = $location->id;
173
        $event->seat_map_id        = $seatMap->id;
174
        $event->price_list_id      = $priceList->id;
175
        $event->state              = 'open';
176
        $event->save();
177
178
        for ($i = 0; $i < 8; $i++) {
179
            $ticket = new Ticket();
180
            $ticket->random_id         = Str::random(20);
181
            $ticket->seat_number       = $i + 1000;
182
            $ticket->event_id          = $event->id;
183
            $ticket->purchase_id       = $purchase->id;
184
            $ticket->price_category_id = $priceCategory->id;
185
            $ticket->state             = 'consumed';
186
            $ticket->save();
187
        }
188
        try {
189
            $html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', 0);
190
            $html2pdf->pdf->SetDisplayMode('fullpage');
191
            $html2pdf->pdf->SetAuthor(config('app.name'));
192
            $html2pdf->pdf->SetTitle('Purchase #' . $purchase->id);
193
194
            // Generate pdf-content by passing the tickets to the view
195
            $content = view('pdfs.ticket-v2', ['tickets' => $purchase->tickets])->render();
196
            $html2pdf->writeHTML($content);
197
198
            $html2pdf->output('tickets-' . $purchase->id . '.pdf');
199
        } catch (Html2PdfException $e) {
200
            $html2pdf->clean();
201
            DB::rollBack();
202
            return redirect()->route('ticket.purchase', ['purchase' => $purchase])->with('state', $e->getMessage());
203
        }
204
        DB::rollBack();
205
    }
206
}
207