|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
//use App\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
|
|
|
// ********************************************************************** |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the current logged user ID. |
|
23
|
|
|
* If user is admin or super admin return 0. |
|
24
|
|
|
* |
|
25
|
|
|
* @return int $ret |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getLoggedUser() |
|
28
|
|
|
{ |
|
29
|
|
|
$ret = Auth::user(); |
|
30
|
|
|
|
|
31
|
|
|
return $ret; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// ********************************************************************** |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get the current logged user id. |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool $ret - the current logged user id, if admin($user->group == 2) or super admin()$user->group == 1) is stet to 0 |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getLoggedAuthorId() |
|
42
|
|
|
{ |
|
43
|
|
|
$user = Auth::user(); |
|
44
|
|
|
$ret = null; |
|
45
|
|
|
if ($user) { |
|
46
|
|
|
//$ret = (! $user->isSuperAdmin() && ! $user->isAdmin()) ? $user->id : 0; |
|
47
|
|
|
$ret = (! $user->group == 1 && ! $user->group == 2) ? $user->id : 0; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $ret; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// ********************************************************************** |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Upload image on server. |
|
57
|
|
|
* $imageFile - the file to upload |
|
58
|
|
|
* $imageSubdir is the subdir in /storage/app/public/images/.. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $imageFile |
|
61
|
|
|
* @param string $imageName |
|
62
|
|
|
* @param string $imageSubdir |
|
63
|
|
|
* @param string $imageWidth |
|
64
|
|
|
* @param string $thumbWidth |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth) |
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
|
|
// Create dir if not exist (in /storage/app/public/images/..) |
|
71
|
1 |
|
if (! Storage::disk('public')->has('images/'.$imageSubdir.'/')) { |
|
|
|
|
|
|
72
|
1 |
|
Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$destinationPath = 'app/public/images/'.$imageSubdir.'/'; |
|
76
|
|
|
|
|
77
|
|
|
// Resize the image with Intervention - http://image.intervention.io/api/resize |
|
78
|
|
|
// - resize and store the image to a width of 300 and constrain aspect ratio (auto height) |
|
79
|
|
|
// - save file as jpg with medium quality |
|
80
|
1 |
|
$image = Image::make($imageFile->getRealPath()) |
|
81
|
1 |
|
->resize((int) $imageWidth, null, |
|
82
|
|
|
function ($constraint) { |
|
83
|
1 |
|
$constraint->aspectRatio(); |
|
84
|
1 |
|
}) |
|
85
|
1 |
|
->save(storage_path($destinationPath.$imageName), 75); |
|
86
|
|
|
|
|
87
|
|
|
// Create the thumb |
|
88
|
1 |
|
$image->resize((int) $thumbWidth, null, |
|
89
|
|
|
function ($constraint) { |
|
90
|
1 |
|
$constraint->aspectRatio(); |
|
91
|
1 |
|
}) |
|
92
|
1 |
|
->save(storage_path($destinationPath.'thumb_'.$imageName), 75); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
// ********************************************************************** |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the language name from language code. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $languageCode |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
6 |
|
public function getSelectedLocaleName($languageCode) |
|
104
|
|
|
{ |
|
105
|
6 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|
106
|
6 |
|
$ret = $countriesAvailableForTranslations[$languageCode]['name']; |
|
107
|
|
|
|
|
108
|
6 |
|
return $ret; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|