1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers; |
4
|
|
|
|
5
|
|
|
//use DavideCasiraghi\LaravelEventsCalendar\Models\User; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
9
|
|
|
use Intervention\Image\ImageManagerStatic as Image; |
10
|
|
|
use Illuminate\Routing\Controller as BaseController; |
11
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
12
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
13
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
14
|
|
|
|
15
|
|
|
class Controller extends BaseController |
16
|
|
|
{ |
17
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
18
|
|
|
|
19
|
|
|
// https://stackoverflow.com/questions/51611015/authuser-return-null-5-6 |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->middleware(function ($request, $next) { |
23
|
|
|
$this->user = Auth::user(); |
|
|
|
|
24
|
|
|
|
25
|
|
|
return $next($request); |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// ********************************************************************** |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the current logged user ID. |
33
|
|
|
* If user is admin or super admin return 0. |
34
|
|
|
* |
35
|
|
|
* @return int $ret |
36
|
|
|
*/ |
37
|
|
|
public function getLoggedUser() |
38
|
|
|
{ |
39
|
|
|
$user = Auth::user(); |
40
|
|
|
|
41
|
|
|
// This is needed to not get error in the queries with: ->when($loggedUser->id, function ($query, $loggedUserId) { |
42
|
|
|
/*if (! $user) { |
43
|
|
|
$user = new User(); |
44
|
|
|
$user->name = null; |
45
|
|
|
$user->group = null; |
46
|
|
|
}*/ |
47
|
|
|
|
48
|
|
|
$ret = $user; |
49
|
|
|
|
50
|
|
|
return $ret; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// ********************************************************************** |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the current logged user id. |
57
|
|
|
* |
58
|
|
|
* @return bool $ret - the current logged user id, if admin or super admin 0 |
59
|
|
|
*/ |
60
|
17 |
|
public function getLoggedAuthorId() |
61
|
|
|
{ |
62
|
17 |
|
$user = Auth::user(); |
63
|
|
|
|
64
|
17 |
|
$ret = null; |
65
|
|
|
|
66
|
17 |
|
if ($user) { |
67
|
|
|
//disabled for the tests errors -- still to solve the isSuperAdmin() |
68
|
|
|
//$ret = (! $user->isSuperAdmin() && ! $user->isAdmin()) ? $user->id : 0; |
69
|
14 |
|
$ret = (! $user->group == 1 && ! $user->group == 2) ? $user->id : 0; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
/*if ($user) { |
72
|
|
|
$ret = $user->id; |
73
|
|
|
}*/ |
74
|
|
|
|
75
|
17 |
|
return $ret; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// ********************************************************************** |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Upload image on server. |
82
|
|
|
* $imageFile - the file to upload |
83
|
|
|
* $imageSubdir is the subdir in /storage/app/public/images/.. |
84
|
|
|
* |
85
|
|
|
* @param array $imageFile |
86
|
|
|
* @param string $imageName |
87
|
|
|
* @param string $imageSubdir |
88
|
|
|
* @param string $imageWidth |
89
|
|
|
* @param string $thumbWidth |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
1 |
|
public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth) |
93
|
|
|
{ |
94
|
|
|
|
95
|
|
|
// Create dir if not exist (in /storage/app/public/images/..) |
96
|
1 |
|
if (! Storage::disk('public')->has('images/'.$imageSubdir.'/')) { |
|
|
|
|
97
|
1 |
|
Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/'); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$destinationPath = 'app/public/images/'.$imageSubdir.'/'; |
101
|
|
|
|
102
|
|
|
// Resize the image with Intervention - http://image.intervention.io/api/resize |
103
|
|
|
// - resize and store the image to a width of 300 and constrain aspect ratio (auto height) |
104
|
|
|
// - save file as jpg with medium quality |
105
|
1 |
|
$image = Image::make($imageFile->getRealPath()) |
106
|
1 |
|
->resize($imageWidth, null, |
|
|
|
|
107
|
|
|
function ($constraint) { |
108
|
1 |
|
$constraint->aspectRatio(); |
109
|
1 |
|
}) |
110
|
1 |
|
->save(storage_path($destinationPath.$imageName), 75); |
111
|
|
|
|
112
|
|
|
// Create the thumb |
113
|
1 |
|
$image->resize($thumbWidth, null, |
114
|
|
|
function ($constraint) { |
115
|
1 |
|
$constraint->aspectRatio(); |
116
|
1 |
|
}) |
117
|
1 |
|
->save(storage_path($destinationPath.'thumb_'.$imageName), 75); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
// ********************************************************************** |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the language name from language code. |
124
|
|
|
* |
125
|
|
|
* @param string $languageCode |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
2 |
|
public function getSelectedLocaleName($languageCode) |
129
|
|
|
{ |
130
|
2 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
131
|
2 |
|
$ret = $countriesAvailableForTranslations[$languageCode]['name']; |
132
|
|
|
|
133
|
2 |
|
return $ret; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|