1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Book; |
6
|
|
|
use App\Models\Student; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
|
11
|
|
|
class BookController extends Controller |
12
|
|
|
{ |
13
|
|
|
public function store(Request $request) |
14
|
|
|
{ |
15
|
|
|
$request->validate([ |
16
|
|
|
'book_id' => 'required', |
17
|
|
|
'student_id' => 'required', |
18
|
|
|
]); |
19
|
|
|
|
20
|
|
|
$student = Student::find($request->student_id); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$student->books()->attach(Book::find($request->book_id), [ |
|
|
|
|
23
|
|
|
'code' => $request->code, |
|
|
|
|
24
|
|
|
'status_id' => $request->status_id, |
|
|
|
|
25
|
|
|
'expiry_date' => $request->expiry_date, |
|
|
|
|
26
|
|
|
]); |
27
|
|
|
|
28
|
|
|
return redirect()->back(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function update(Request $request) |
32
|
|
|
{ |
33
|
|
|
$request->validate([ |
34
|
|
|
'book_student_id' => 'required', |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
DB::table('book_student')->where('id', $request->book_student_id)->update([ |
|
|
|
|
38
|
|
|
'status_id' => $request->status, |
|
|
|
|
39
|
|
|
'code' => $request->code, |
|
|
|
|
40
|
|
|
'expiry_date' => $request->expiry_date, |
|
|
|
|
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
return Student::find(DB::table('book_student')->where('id', $request->book_student_id)->first()->student_id)->books; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function destroy(Request $request) |
47
|
|
|
{ |
48
|
|
|
$student_id = DB::table('book_student')->where('id', $request->book_student_id)->first()->student_id; |
|
|
|
|
49
|
|
|
|
50
|
|
|
DB::table('book_student')->where('id', $request->book_student_id)->delete(); |
51
|
|
|
|
52
|
|
|
return Student::find($student_id)->books; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function exportCode(Request $request) |
56
|
|
|
{ |
57
|
|
|
$request->validate([ |
58
|
|
|
'book_student_id' => 'required', |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$book = DB::table('book_student')->find($request->book_student_id); |
|
|
|
|
62
|
|
|
$student = Student::find($book->student_id); |
63
|
|
|
|
64
|
|
|
$image = imagecreatefrompng(storage_path('afloja/vignette_livre.png')); |
65
|
|
|
$black = imagecolorallocate($image, 0, 0, 0); |
66
|
|
|
$font = storage_path('afloja/fonts/OpenSans-ExtraBold.ttf'); |
67
|
|
|
|
68
|
|
|
// First line of text |
69
|
|
|
$font_size = 42; |
70
|
|
|
$text = 'Alumno:'; |
71
|
|
|
$angle = 0; |
72
|
|
|
$width = imagesx($image); |
73
|
|
|
$centerX = $width / 2; |
74
|
|
|
[$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
75
|
|
|
$left_offset = ($right - $left) / 2; |
76
|
|
|
$x = $centerX - $left_offset; |
77
|
|
|
imagettftext($image, $font_size, $angle, $x, 450, $black, $font, $text); |
78
|
|
|
|
79
|
|
|
// Name of student |
80
|
|
|
$text = $student->lastname.' '.$student->firstname; |
|
|
|
|
81
|
|
|
$angle = 0; |
82
|
|
|
$width = imagesx($image); |
83
|
|
|
$centerX = $width / 2; |
84
|
|
|
[$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
85
|
|
|
$left_offset = ($right - $left) / 2; |
86
|
|
|
$x = $centerX - $left_offset; |
87
|
|
|
imagettftext($image, $font_size, $angle, $x, 525, $black, $font, $text); |
88
|
|
|
|
89
|
|
|
// First line of text |
90
|
|
|
$text = 'Código Premium:'; |
91
|
|
|
$angle = 0; |
92
|
|
|
$width = imagesx($image); |
93
|
|
|
$centerX = $width / 2; |
94
|
|
|
[$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
95
|
|
|
$left_offset = ($right - $left) / 2; |
96
|
|
|
$x = $centerX - $left_offset; |
97
|
|
|
imagettftext($image, $font_size, $angle, $x, 620, $black, $font, $text); |
98
|
|
|
|
99
|
|
|
// Code |
100
|
|
|
$text = $book->code; |
101
|
|
|
$angle = 0; |
102
|
|
|
$width = imagesx($image); |
103
|
|
|
$centerX = $width / 2; |
104
|
|
|
[$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
105
|
|
|
$left_offset = ($right - $left) / 2; |
106
|
|
|
$x = $centerX - $left_offset; |
107
|
|
|
imagettftext($image, $font_size, $angle, $x, 690, $black, $font, $text); |
108
|
|
|
|
109
|
|
|
// last line of text |
110
|
|
|
$font = storage_path('afloja/fonts/OpenSans-Light.ttf'); |
111
|
|
|
$font_size = 30; |
112
|
|
|
$expiry_date = Carbon::parse($book->expiry_date)->format('j/m/Y'); |
113
|
|
|
$text = "(Valido hasta el $expiry_date)"; |
114
|
|
|
$angle = 0; |
115
|
|
|
$width = imagesx($image); |
116
|
|
|
$centerX = $width / 2; |
117
|
|
|
[$left, , $right] = imageftbbox($font_size, $angle, $font, $text); |
118
|
|
|
$left_offset = ($right - $left) / 2; |
119
|
|
|
$x = $centerX - $left_offset; |
120
|
|
|
imagettftext($image, $font_size, $angle, $x, 755, $black, $font, $text); |
121
|
|
|
|
122
|
|
|
// Using imagepng() results in clearer text compared with imagejpeg() |
123
|
|
|
header('Content-Type: image/jpg'); |
124
|
|
|
header('Content-Description: File Transfer'); |
125
|
|
|
header('Content-Type: application/octet-stream'); |
126
|
|
|
header('Content-Transfer-Encoding: binary'); |
127
|
|
|
header('Expires: 0'); |
128
|
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
129
|
|
|
header('Pragma: public'); |
130
|
|
|
ob_start(); |
131
|
|
|
imagepng($image); |
132
|
|
|
header('Content-Disposition: attachment; filename=vignette.jpg'); |
133
|
|
|
imagedestroy($image); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|