1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
use Illuminate\Http\RedirectResponse; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\View\View; |
8
|
|
|
use Xetaravel\Models\Repositories\AccountRepository; |
9
|
|
|
use Xetaravel\Models\User; |
10
|
|
|
use Xetaravel\Models\Validators\AccountValidator; |
11
|
|
|
|
12
|
|
|
class AccountController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
|
21
|
|
|
$this->breadcrumbs->addCrumb('Account', route('users_account_index')); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Show the account update form. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\View\View |
28
|
|
|
*/ |
29
|
|
|
public function index(): View |
30
|
|
|
{ |
31
|
|
|
$user = User::find(Auth::user()->id); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->breadcrumbs->setCssClasses('breadcrumb'); |
34
|
|
|
|
35
|
|
|
return view('account.index', ['user' => $user, 'breadcrumbs' => $this->breadcrumbs]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handle a account update request for the application. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Http\Request $request |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\RedirectResponse |
44
|
|
|
*/ |
45
|
|
|
public function update(Request $request): RedirectResponse |
46
|
|
|
{ |
47
|
|
|
AccountValidator::update($request->all())->validate(); |
48
|
|
|
|
49
|
|
|
if (AccountRepository::update($request->all())) { |
50
|
|
|
$user = User::find(Auth::user()->id); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// Handle the avatar upload. |
53
|
|
|
if (!is_null($request->file('avatar'))) { |
54
|
|
|
$user->clearMediaCollection('avatar'); |
55
|
|
|
$user->addMedia($request->file('avatar')) |
56
|
|
|
->preservingOriginal() |
57
|
|
|
->setName(substr(md5($user->username), 0, 10)) |
58
|
|
|
->setFileName(substr(md5($user->username), 0, 10) . '.' . $request->file('avatar')->extension()) |
59
|
|
|
->toMediaCollection('avatar'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return redirect() |
63
|
|
|
->route('users_account_index') |
64
|
|
|
->with('success', 'Your account has been updated successfully !'); |
65
|
|
|
} else { |
66
|
|
|
return redirect() |
67
|
|
|
->route('users_account_index') |
68
|
|
|
->with('danger', 'An error occurred while saving your informations !'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: