Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit cc2b3da

Browse files
committed
Add user update profile and password fonctionnalities
1 parent 023ad66 commit cc2b3da

File tree

9 files changed

+264
-13
lines changed

9 files changed

+264
-13
lines changed

app/Helpers/MediaHelper.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018. MckenzieArts
4+
* @author Mckenziearts <contact@mckenziearts.design>
5+
* @link https://www.camer-freelancer.com
6+
* @since 7.1
7+
* @version 1.0
8+
* @package CF/Helpers
9+
*/
10+
11+
namespace App\Helpers;
12+
13+
use Intervention\Image\Facades\Image;
14+
15+
class MediaHelper
16+
{
17+
/**
18+
* @protected
19+
*
20+
* @var string $dir, the file uploaded path
21+
*/
22+
protected static $dir = 'app/public';
23+
24+
/**
25+
* @return string
26+
*/
27+
public static function getUploadsFolder()
28+
{
29+
return self::$dir;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public static function getStoragePath()
36+
{
37+
return storage_path(self::$dir);
38+
}
39+
40+
/**
41+
* Return the size of an image
42+
*
43+
* @param string $file
44+
* @param string $folder
45+
* @return array $width and $height of the file give in parameter
46+
*/
47+
public static function getFileSizes(string $file, string $folder = null)
48+
{
49+
if ($folder) {
50+
list($width, $height, $type, $attr) = getimagesize(storage_path(self::$dir.'/'. $folder .'/'. date('FY') .'/'.$file));
51+
}
52+
list($width, $height, $type, $attr) = getimagesize(storage_path(self::$dir.'/'. date('FY') .'/'.$file));
53+
54+
return [
55+
'width' => $width,
56+
'height' => $height
57+
];
58+
}
59+
60+
/**
61+
* resize, To rezise and image
62+
*
63+
* @param string $file, l'image à redimmensionner
64+
* @param int $width, la largeur à laquelle on doit redimensionner l'image
65+
* @param int $height, la hateur à laquelle on doit redimensionner l'image
66+
* @param string $filepath, le chemin ou est garder le fichier redimensionner
67+
*/
68+
public static function resize($file, $width, $height, $filepath)
69+
{
70+
Image::make($file)->resize($width, $height)->save($filepath);
71+
}
72+
73+
/**
74+
* getImageWeight, permet de retourner le poids d'une image
75+
*
76+
* @param $octets
77+
* @return string
78+
*/
79+
public static function getImageWeight($octets) {
80+
$resultat = $octets;
81+
for ($i = 0; $i < 8 && $resultat >= 1024; $i++) {
82+
$resultat = $resultat / 1024;
83+
}
84+
if ($i > 0) {
85+
return preg_replace('/,00$/', '', number_format($resultat, 2, ',', ''))
86+
. ' ' . substr('KMGTPEZY', $i-1, 1) . 'o';
87+
} else {
88+
return $resultat . ' o';
89+
}
90+
}
91+
}

app/Http/Controllers/UserController.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Repositories\UserRepository;
56
use Illuminate\Http\Request;
67

78
class UserController extends Controller
89
{
9-
public function __construct()
10+
/**
11+
* @var UserRepository
12+
*/
13+
private $repository;
14+
15+
/**
16+
* UserController constructor.
17+
*
18+
* @param UserRepository $repository
19+
*/
20+
public function __construct(UserRepository $repository)
1021
{
22+
$this->repository = $repository;
1123
}
1224

1325
/**
@@ -20,13 +32,59 @@ public function account()
2032
return view('frontend.users.account');
2133
}
2234

35+
/**
36+
* Update User account
37+
*
38+
* @param Request $request
39+
* @param int $id
40+
* @return \Illuminate\Http\RedirectResponse
41+
*/
42+
public function updateAccount(Request $request, int $id)
43+
{
44+
$request->validate($this->rules());
45+
46+
$this->repository->update($request->except(['_token', '_method']), $id);
47+
48+
return redirect(route('users.account'))->with('success', __("Votre profil a été mis à jour avec succès !"));
49+
}
50+
2351
/**
2452
* User update password
2553
*
2654
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2755
*/
28-
public function updatePassword()
56+
public function password()
2957
{
3058
return view('frontend.users.password');
3159
}
60+
61+
/**
62+
* User password Update
63+
*
64+
* @param Request $request
65+
* @param int $id
66+
* @return \Illuminate\Http\RedirectResponse
67+
*/
68+
public function updatePassword(Request $request, int $id)
69+
{
70+
$request->validate(['password' => 'required|confirmed|min:6']);
71+
72+
$this->repository->passwordUpdate($request->except(['_token', '_method']), $id);
73+
74+
return redirect(route('users.password'))->with('success', __("Votre mot de passe a été mis à jour avec succès !"));
75+
}
76+
77+
/**
78+
* Validation Rules
79+
*
80+
* @return array
81+
*/
82+
public function rules()
83+
{
84+
return [
85+
'name' => 'required',
86+
'email' => 'required',
87+
'avatar' => 'image|mimes:jpeg,png,jpg',
88+
];
89+
}
3290
}

app/Repositories/UserRepository.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace App\Repositories;
99

10+
use App\Traits\UploadAction;
1011
use App\User;
1112

1213
class UserRepository
1314
{
15+
use UploadAction;
16+
1417
/**
1518
* @var User
1619
*/
@@ -34,4 +37,54 @@ public function newInstance()
3437
{
3538
return $this->model->newInstance();
3639
}
40+
41+
/**
42+
* Return a model with the id set in parameter
43+
*
44+
* @param int $id
45+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
46+
*/
47+
public function find(int $id)
48+
{
49+
return $this->model->newQuery()
50+
->findOrFail($id);
51+
}
52+
53+
/**
54+
* Update user profile
55+
*
56+
* @param array $data
57+
* @param int $id
58+
* @return bool
59+
*/
60+
public function update(array $data, $id)
61+
{
62+
$model = $this->find($id);
63+
64+
$model->name = $data['name'] ?? '';
65+
$model->email = $data['email'] ?? '';
66+
$model->phone_number = $data['phone_number'] ?? '';
67+
$model->twitter_profile = $data['twitter_profile'] ?? '';
68+
$model->github_profile = $data['github_profile'] ?? '';
69+
/* Media upload */
70+
if (array_key_exists('avatar', $data)) {
71+
$model->avatar = $this->upload($data['avatar'], 'users');
72+
}
73+
74+
return $model->save();
75+
}
76+
77+
/**
78+
* Update password
79+
*
80+
* @param array $data
81+
* @param int $id
82+
* @return bool
83+
*/
84+
public function passwordUpdate(array $data, int $id)
85+
{
86+
$model = $this->find($id);
87+
88+
return $model->update(['password' => bcrypt($data['password'])]);
89+
}
3790
}

app/Traits/UploadAction.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018. MckenzieArts
4+
* @author Mckenziearts <contact@mckenziearts.design>
5+
* @link https://www.camer-freelancer.com
6+
*/
7+
8+
namespace App\Traits;
9+
10+
use App\Helpers\MediaHelper;
11+
use Illuminate\Http\UploadedFile;
12+
13+
trait UploadAction
14+
{
15+
/**
16+
* Upload file to uploads folder
17+
*
18+
* @param UploadedFile $file
19+
* @param string|null $folder
20+
* @return string
21+
*/
22+
public function upload(UploadedFile $file, string $folder = null)
23+
{
24+
$destinationPath = is_null($folder) ?
25+
MediaHelper::getStoragePath() :
26+
MediaHelper::getStoragePath(). '/' .$folder. '/' .date('FY');
27+
$extension = $file->getClientOriginalExtension();
28+
29+
$fileName = sha1(explode('.', $file->getClientOriginalName())[0]). '.' .$extension;
30+
31+
$file->move($destinationPath, $fileName);
32+
$name = $folder. '/' .date('FY'). '/' .$fileName;
33+
34+
return $name;
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@if(Session::has('success'))
2+
<div class="alert alert-success alert-dismissible fade show" role="alert">
3+
<strong>Well Done !!</strong> {{ Session::get('success') }}
4+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
5+
<span aria-hidden="true">&times;</span>
6+
</button>
7+
</div>
8+
<div class="chatter-alert-spacer"></div>
9+
@endif

resources/views/frontend/users/account.blade.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121

2222
<section class="account">
2323
<div class="container">
24+
@include('frontend.partials.success')
2425
<div class="row">
2526
<div class="col-sm-12 col-md-3">
2627
@include('frontend.users.sidebar')
2728
</div>
2829
<div class="col-sm-12 col-md-9">
2930
<div class="card forum_account">
3031
<h2>{{ __('Profile') }}</h2>
31-
<form action="#" method="POST" enctype="multipart/form-data">
32+
<form action="{{ route('users.update-account', auth()->user()) }}" method="POST" enctype="multipart/form-data">
3233
@method('PUT')
3334
@csrf
3435
<div class="account__grid">
@@ -40,25 +41,25 @@
4041

4142
@if ($errors->has('name'))
4243
<span class="invalid-feedback" role="alert">
43-
<strong>{{ $errors->first('name') }}</strong>
44-
</span>
44+
<strong>{{ $errors->first('name') }}</strong>
45+
</span>
4546
@endif
4647
</div>
4748
<div class="form-group">
4849
<label for="email">{{ __('E-Mail Address') }} <small class="text-danger">*</small></label>
4950

50-
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ auth()->user()->email }}" disabled>
51+
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ auth()->user()->email }}" readonly>
5152

5253
@if ($errors->has('email'))
5354
<span class="invalid-feedback" role="alert">
54-
<strong>{{ $errors->first('email') }}</strong>
55-
</span>
55+
<strong>{{ $errors->first('email') }}</strong>
56+
</span>
5657
@endif
5758
</div>
5859

5960
<div class="form-group">
6061
<label for="phone_number">{{ __('Phone Number') }}</label>
61-
<input id="phone_number" type="email" class="form-control" name="phone_number" value="{{ auth()->user()->phone_number }}">
62+
<input id="phone_number" type="text" class="form-control" name="phone_number" value="{{ auth()->user()->phone_number }}">
6263
</div>
6364

6465
<div class="form-group">

resources/views/frontend/users/password.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121

2222
<section class="account">
2323
<div class="container">
24+
@include('frontend.partials.success')
2425
<div class="row">
2526
<div class="col-sm-12 col-md-3">
2627
@include('frontend.users.sidebar')
2728
</div>
2829
<div class="col-sm-12 col-md-9">
2930
<div class="card forum_account">
3031
<h2>{{ __('Update password') }}</h2>
31-
<form action="#" method="POST">
32+
<form action="{{ route('users.update-password', auth()->user()) }}" method="POST">
3233
@csrf
3334

3435
<div class="form-group">

resources/views/vendor/chatter/home.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@
7474
<div class="card-header"><i class="icon ion-person"></i> {{ __('Personnal Informations') }}</div>
7575
<div class="card-body">
7676
<ul class="profil_content">
77-
<li class="profil_item"><a href="javascript:;">{{ __('Follow Discussions') }}</a></li>
77+
{{--<li class="profil_item"><a href="javascript:;">{{ __('Follow Discussions') }}</a></li>
7878
<li class="profil_item"><a href="javascript:;">{{ __('My Discussions') }}</a></li>
79-
<li class="profil_item"><a href="javascript:;">{{ __('My Messages') }}</a></li>
79+
<li class="profil_item"><a href="javascript:;">{{ __('My Messages') }}</a></li>--}}
8080
<li class="profil_item"><a href="{{ route('users.account') }}">{{ __('My account') }}</a></li>
8181
<li class="profil_item">
8282
<a href="{{ route('logout') }}" onclick="event.preventDefault();

routes/web.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
/** Profile Route */
2121
Route::group(['middleware' => 'auth'], function () {
2222
Route::get('/account', 'UserController@account')->name('users.account');
23-
Route::get('/update-password', 'UserController@updatePassword')->name('users.password');
23+
Route::put('/update-account/{id}', 'UserController@updateAccount')->name('users.update-account');
24+
Route::get('/update-password', 'UserController@password')->name('users.password');
25+
Route::post('/update-password/{id}', 'UserController@updatePassword')->name('users.update-password');
2426
});
2527

2628
Route::get('/', 'SiteController@index')->name('home');

0 commit comments

Comments
 (0)