Skip to content

Commit

Permalink
Merge pull request #395 from RezaAb/master
Browse files Browse the repository at this point in the history
extradata column
  • Loading branch information
AlirezaAlgo authored Jun 11, 2019
2 parents 5682573 + 5738743 commit f780155
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 49 deletions.
50 changes: 41 additions & 9 deletions src/controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;

class ProfileController extends Controller {

public function getEdit() {

$admin = Admin::find(\Auth::guard('panel')->user()->id);
$admin = Admin::find(\Auth::guard('panel')->user()->id);

$demo = false;
if (\Config::get('panel.demo') == true) {
$demo = true;
}
$demo = false;
if (\Config::get('panel.demo') == true) {
$demo = true;
}

if (!$demo && request()->has('del_picture')){
$file = $admin->getAdminPicture();
//dd(public_path($file));
if (!empty($file) && File::exists(public_path($file))){
File::delete(public_path($file));
}
$admin->updateAdminPicture('');
return \Redirect::to(request()->path());
}

return \View('panelViews::editProfile')->with('admin', $admin)->with('demo_status', $demo);
}
Expand All @@ -22,18 +33,39 @@ public function postEdit() {

$demo = false;
if (\Config::get('panel.demo') == true) {
$demo = true;
$demo = true;
}

$admin = Admin::find(\Auth::guard('panel')->user()->id);
$inputs = Input::all();
request()->validate([
'picture' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
// Check if a profile image has been uploaded
if (request()->has('picture')) {
// Get image file
$image = request()->file('picture');
// Make a image name based on user name and current timestamp
$name = str_slug(request()->input('first_name')).'_'.str_slug(request()->input('last_name')).'_'.time();
// Define folder path
$folder = '/uploads/panel_avatars/';
// Make a file path where image will be stored [ folder path + file name + file extension]
$filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
// Upload image
$name = !empty($name) ? $name : str_random(25);
$file = $image->move(public_path($folder), $name.'.'.$image->getClientOriginalExtension());

// Set user profile image path in database to filePath
$admin->updateAdminPicture($filePath);
}
//dd($inputs['picture']);
$admin->update($inputs);
$admin->save();
return \View('panelViews::editProfile')->with(
array(
'admin' => $admin,
'message' => \Lang::get('panel::fields.successfullEditProfile'),
'demo_status' => $demo)
'admin' => $admin,
'message' => \Lang::get('panel::fields.successfullEditProfile'),
'demo_status' => $demo)
);
}
}
25 changes: 21 additions & 4 deletions src/models/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,26 @@ public function getSearchInExtraData($key, $query){

/**
* add or update admin's picture.
* @param $pic_base64_encoded
* @param $path_or_pic_base64_encoded
*/
public function updateAdminPicture($pic_base64_encoded){
public function updateAdminPicture($path_or_pic_base64_encoded){
//use forceFill() which will bypass the mass assignment check to perform update on any JSON path,
// if path is not there, it will be created and if it’s present it will be updated accordingly.
$this->forceFill(['extradata->picture' => $pic_base64_encoded]);
$this->forceFill(['extradata->picture' => $path_or_pic_base64_encoded]);

# Save the changes
$this->update();
}

/**
* get admin picture from extradata column
* @return mixed
*/
public function getAdminPicture(){
$extdata = $this->getExtraDataObj();
return $extdata->picture;
}

/**
* find admin by primary key id
* @param $admin_id
Expand Down Expand Up @@ -136,6 +145,14 @@ public function scopeGetExtraData($query, $key, $value){
//return $query->where('extradata->' + $key, $value);
}

/**
* get extradata as json object
* @return mixed
*/
public function getExtraDataObj(){
return json_decode($this->extradata);
}

/**
* Scope a query to get admin by a $key and search in $value.
* @param $query
Expand All @@ -151,7 +168,7 @@ public function scopeSearchInExtraData($query, $key, $query_value){
}


protected $fillable = array('first_name', 'last_name', 'email', 'password');
protected $fillable = array('first_name', 'last_name', 'email', 'password', 'extradata');
/**
* The attributes excluded from the model's JSON form.
*
Expand Down
80 changes: 45 additions & 35 deletions src/views/editProfile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@
@extends('panelViews::mainTemplate')
@section('page-wrapper')

@if (!empty($message))
<div class="alert-box success">
<h2>{{ $message }}</h2>
</div>
@endif

@if ($demo_status == true)
<h4>You are not allowed to edit the profile in demo version of panel.</h4>
@else

<div class="row">
<div class="col-xs-4" >
<div class="well well-lg">
{!!
Form::model($admin, array( $admin->id))
!!}

{!! Form::label('first_name', \Lang::get('panel::fields.FirstName')) !!}
{!! Form::text('first_name', $admin->first_name, array('class' => 'form-control')) !!}
<br />
{!! Form::label('last_name', \Lang::get('panel::fields.LastName')) !!}
{!! Form::text('last_name', $admin->last_name, array('class' => 'form-control')) !!}
<br />
<!-- email -->
{!! Form::label('email', \Lang::get('panel::fields.email')) !!}
{!! Form::email('email', $admin->email, array('class' => 'form-control')) !!}
<br />
{!! Form::submit(\Lang::get('panel::fields.updateProfile'), array('class' => 'btn btn-primary')) !!}

{!! Form::close() !!}
</div>
@endif

</div>
</div>
@if (!empty($message))
<div class="alert-box success">
<h2>{{ $message }}</h2>
</div>
@endif

@if ($demo_status == true)
<h4>You are not allowed to edit the profile in demo version of panel.</h4>
@else

<div class="row">
<div class="col-xs-4" >
<div class="well well-lg">
{!!
Form::model($admin, array( $admin->id, 'files' => true))
!!}

{!! Form::label('first_name', \Lang::get('panel::fields.FirstName')) !!}
{!! Form::text('first_name', $admin->first_name, array('class' => 'form-control')) !!}
<br />
{!! Form::label('last_name', \Lang::get('panel::fields.LastName')) !!}
{!! Form::text('last_name', $admin->last_name, array('class' => 'form-control')) !!}
<br />
<!-- email -->
{!! Form::label('email', \Lang::get('panel::fields.email')) !!}
{!! Form::email('email', $admin->email, array('class' => 'form-control')) !!}
<br />
@if (!empty($admin->getAdminPicture()))
<img src="{{ asset($admin->getAdminPicture()) }}" alt="{{$admin->first_name}} {{$admin->last_name}}" style="width: 60px; height: 60px; border-radius: 50%;" />
<a href="?del_picture=1">delete</a>
@else
<!-- file of picture -->
{!! Form::label('picture', \Lang::get('panel::fields.picture')) !!}
{!! Form::file('picture') !!}
@endif
<br />
<br />
{!! Form::submit(\Lang::get('panel::fields.updateProfile'), array('class' => 'btn btn-primary')) !!}

{!! Form::close() !!}
</div>
@endif

</div>
</div>

@stop
9 changes: 8 additions & 1 deletion src/views/mainTemplate.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@

<div class="navbar-default sidebar " role="navigation">
<div class="sidebar-nav navbar-collapse collapse " id="bs-example-navbar-collapse-1">
<div class="grav center"><img src="//www.gravatar.com/avatar/{{ md5( strtolower( trim( Auth::guard('panel')->user()->email ) ) )}}?d=mm&s=128" ><a href="{{url('panel/edit')}}"><span> {{ \Lang::get('panel::fields.change') }}</span></a></div>
<div class="grav center">
@if (!empty(Auth::guard('panel')->user()->getAdminPicture()))
<img src="{{ asset(Auth::guard('panel')->user()->getAdminPicture()) }}" />
@else
<img src="//www.gravatar.com/avatar/{{ md5( strtolower( trim( Auth::guard('panel')->user()->email ) ) )}}?d=mm&s=128" />
@endif
<a href="{{url('panel/edit')}}"><span> {{ \Lang::get('panel::fields.change') }}</span></a>
</div>
<div class="user-info">{{Auth::guard('panel')->user()->first_name.' '.Auth::guard('panel')->user()->last_name}}</div>
<a class="visit-site" href="{{$app['url']->to('/')}}">{{ \Lang::get('panel::fields.visiteSite') }} </a>
<ul class="nav" id="side-menu">
Expand Down

0 comments on commit f780155

Please sign in to comment.