Completed
Push — master ( aec46e...b9d3b7 )
by Martin
11:50
created

SettingsController::testTicket()   A

Complexity

Conditions 2
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
nc 8
nop 0
dl 0
loc 28
rs 9.6333
c 1
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\Purchase;
8
use App\Setting;
9
use App\Ticket;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\App;
12
use Illuminate\Support\Facades\DB;
13
use Mews\Purifier\Facades\Purifier;
14
use Spipu\Html2Pdf\Exception\Html2PdfException;
15
use Spipu\Html2Pdf\Html2Pdf;
16
17
class SettingsController extends Controller
18
{
19
    // Display all available settings
20
    public function index()
21
    {
22
        $terms = Setting::where('name', 'terms')->where('lang', App::getLocale())->first();
23
        $termsHtml = $terms ? $terms->value : view('components.default-texts.terms')->render();
24
25
        $privacy = Setting::where('name', 'privacy')->where('lang', App::getLocale())->first();
26
        $privacyHtml = $privacy ? $privacy->value : view('components.default-texts.privacy')->render();
27
28
        return view('admin.settings', [
29
            'terms' => $termsHtml,
30
            'privacy' => $privacyHtml
31
        ]);
32
    }
33
34
    /**
35
     * Receives HTML-input
36
     * 
37
     * Attention: Function might be target for XSS attacks. Handle input carfully!!!
38
     */
39
    public function updateTerms(Request $request)
40
    {
41
        Setting::updateOrCreate(
42
            ['name' => 'terms', 'lang' => App::getLocale()],
43
            ['value' => Purifier::clean($request->input('terms'))]
44
        );
45
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Terms and Conditions updated.');
46
    }
47
48
    /**
49
     * Receives HTML-input
50
     * 
51
     * Attention: Function might be target for XSS attacks. Handle input carfully!!!
52
     */
53
    public function updatePrivacy(Request $request)
54
    {
55
        Setting::updateOrCreate(
56
            ['name' => 'privacy', 'lang' => App::getLocale()],
57
            ['value' => Purifier::clean($request->input('privacy'))]
58
        );
59
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Privacy statement updated.');
60
    }
61
62
    /**
63
     * File-Upload
64
     */
65
    public function updateLogo(Request $request)
66
    {
67
        $validatedFile = $request->validate([
68
            'file' => 'file|max:30000|mimes:jpeg,bmp,png,svg,jpg'
69
        ]);
70
        
71
        // Only extract file extension of the new logo picture
72
        $extension = $validatedFile['file']->extension();
73
        // set it to a generic name to overwrite any existing logo with the same extension
74
        $logoStoreName = 'logo.' . $extension;
75
76
        // Store file as new logo and update the corresponding setting
77
        $validatedFile['file']->storeAs('images', $logoStoreName);
78
        Setting::updateOrCreate(
79
            ['name' => 'logo', 'lang' => 'en'],
80
            ['value' => 'images/' . $logoStoreName]
81
        );
82
83
        // Redirect to source page with success message
84
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Logo updated.');
85
    }
86
87
    /**
88
     * Returns a ticket filled with dummy data to check the
89
     * correct processing of the logo in the layout
90
     */
91
    public function testTicket()
92
    {
93
        // Wrap dummy data creation in a transaction in order
94
        // to not actually store it in the production database
95
        DB::beginTransaction();
96
        $purchase = factory(Purchase::class)->create();
97
        $event = factory(Event::class)->create();
98
        factory(Ticket::class, 7)->create([
99
            'event_id' => $event->id,
100
            'purchase_id' => $purchase->id
101
        ]);
102
        try {
103
            $html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', 0);
104
            $html2pdf->pdf->SetDisplayMode('fullpage');
105
            $html2pdf->pdf->SetAuthor(config('app.name'));
106
            $html2pdf->pdf->SetTitle('Purchase #' . $purchase->id);
107
108
            // Generate pdf-content by passing the tickets to the view
109
            $content = view('pdfs.ticket-v2', ['tickets' => $purchase->tickets])->render();
110
            $html2pdf->writeHTML($content);
111
112
            $html2pdf->output('tickets-' . $purchase->id . '.pdf');
113
        } catch (Html2PdfException $e) {
114
            $html2pdf->clean();
115
            DB::rollBack();
116
            return redirect()->route('ticket.purchase', ['purchase' => $purchase])->with('state', $e->getMessage());
117
        }
118
        DB::rollBack();
119
    }
120
}
121