Browse Source

Show user image on family member search

Add userImage helper function
pull/8/merge
Nafies Luthfi 8 years ago
parent
commit
bf6b55bb94
  1. 53
      app/Helpers/functions.php
  2. 27
      resources/views/users/edit.blade.php
  3. 6
      resources/views/users/partials/profile.blade.php
  4. 5
      resources/views/users/search.blade.php
  5. 115
      tests/Unit/Helpers/UserPhotoHelperTest.php

53
app/Helpers/functions.php

@ -1,31 +1,40 @@
<?php
use App\User;
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2).' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2).' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2).' KB';
} elseif ($bytes > 1) {
$bytes = $bytes.' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes.' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
function userPhoto(User $user, $attributes = [])
{
return Html::image(
userPhotoPath($user->photo_path, $user->gender_id),
null,
$attributes
);
}
function userPhotoPath($photoPath, $genderId)
{
if (is_file(public_path('storage/'.$photoPath))) {
return asset('storage/'.$photoPath);
}
return asset('images/icon_user_'.$genderId.'.png');
}

27
resources/views/users/edit.blade.php

@ -13,13 +13,6 @@
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ trans('user.edit') }}</h3></div>
<div class="panel-body">
<div class="text-center">
@if ($user->photo_path && is_file(public_path('storage/'.$user->photo_path)))
{{ Html::image('storage/'.$user->photo_path, $user->name, ['style' => 'max-width:100%']) }}
@else
{{ Html::image('images/icon_user_'.$user->gender_id.'.png', $user->name, ['style' => 'max-width:100%']) }}
@endif
</div>
{!! FormField::text('name', ['label' => trans('user.name')]) !!}
{!! FormField::text('nickname', ['label' => trans('user.nickname')]) !!}
<div class="row">
@ -56,11 +49,21 @@
</div>
{{ Form::close() }}
<div class="col-md-4">
{{ Form::open(['route' => ['users.photo-upload', $user], 'method' => 'patch', 'files' => true]) }}
{!! FormField::file('photo', ['required' => true, 'label' => __('user.reupload_photo'), 'info' => ['text' => 'Format jpg, maks: 200 Kb.', 'class' => 'warning']]) !!}
{!! Form::submit(__('user.update_photo'), ['class' => 'btn btn-success']) !!}
{{ link_to_route('users.show', trans('app.cancel'), [$user], ['class' => 'btn btn-default']) }}
{{ Form::close() }}
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ __('user.update_photo') }}</h3></div>
{{ Form::open(['route' => ['users.photo-upload', $user], 'method' => 'patch', 'files' => true]) }}
<div class="panel-body text-center">
{{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }}
</div>
<div class="panel-body">
{!! FormField::file('photo', ['required' => true, 'label' => __('user.reupload_photo'), 'info' => ['text' => 'Format jpg, maks: 200 Kb.', 'class' => 'warning']]) !!}
</div>
<div class="panel-footer">
{!! Form::submit(__('user.update_photo'), ['class' => 'btn btn-success']) !!}
{{ link_to_route('users.show', trans('app.cancel'), [$user], ['class' => 'btn btn-default']) }}
</div>
{{ Form::close() }}
</div>
</div>
</div>
@endsection

6
resources/views/users/partials/profile.blade.php

@ -1,11 +1,7 @@
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ trans('user.profile') }}</h3></div>
<div class="panel-body text-center">
@if ($user->photo_path && is_file(public_path('storage/'.$user->photo_path)))
{{ Html::image('storage/'.$user->photo_path, $user->name, ['style' => 'max-width:100%']) }}
@else
{{ Html::image('images/icon_user_'.$user->gender_id.'.png', $user->name, ['style' => 'max-width:100%']) }}
@endif
{{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }}
</div>
<table class="table">
<tbody>

5
resources/views/users/search.blade.php

@ -27,8 +27,11 @@
@foreach ($chunkedUser as $user)
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ $user->profileLink() }} ({{ $user->gender }})</h3></div>
<div class="panel-heading">
{{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }}
</div>
<div class="panel-body">
<h3 class="panel-title">{{ $user->profileLink() }} ({{ $user->gender }})</h3>
<div>{{ trans('user.nickname') }} : {{ $user->nickname }}</div>
<hr style="margin: 5px 0;">
<div>{{ trans('user.father') }} : {{ $user->father_id ? $user->father->name : '' }}</div>

115
tests/Unit/Helpers/UserPhotoHelperTest.php

@ -0,0 +1,115 @@
<?php
namespace Tests\Unit\Helpers;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class UserPhotoHelperTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_photo_path_function_exists()
{
$this->assertTrue(function_exists('userPhotoPath'));
}
/** @test */
public function user_photo_path_function_returns_default_photo_path_based_on_gender_if_photo_path_is_null()
{
$genderId = 1; // Male
$this->assertEquals(
asset('images/icon_user_1.png'), userPhotoPath(null, $genderId)
);
$genderId = 2; // Female
$this->assertEquals(
asset('images/icon_user_2.png'), userPhotoPath(null, $genderId)
);
}
/** @test */
public function user_photo_function_exists()
{
$this->assertTrue(function_exists('userPhoto'));
}
/** @test */
public function user_photo_function_returns_default_image_photo_element_if_no_agency_image_path_setting()
{
$user = factory(User::class)->create(['gender_id' => 1]);
$photoFile = 'images/icon_user_1.png';
$imageString = '<img';
$imageString .= ' src="'.asset($photoFile).'"';
$imageString .= '>';
$this->assertEquals($imageString, userPhoto($user));
}
/** @test */
public function user_photo_function_returns_correct_photo_element_based_on_user_photo_path()
{
$photoPath = 'images/user_photo_path.jpg';
copy(public_path('images/icon_user_1.png'), storage_path('app/public/images/user_photo_path.jpg'));
$this->assertFileExists(storage_path('app/public/images/user_photo_path.jpg'));
$user = factory(User::class)->create([
'gender_id' => 2,
'photo_path' => $photoPath,
]);
$imageString = '<img';
$imageString .= ' src="'.asset('storage/'.$photoPath).'"';
$imageString .= '>';
$this->assertEquals($imageString, userPhoto($user));
$this->assertFileExists(storage_path('app/public/images/user_photo_path.jpg'));
unlink(storage_path('app/public/images/user_photo_path.jpg'));
$this->assertFileNotExists(storage_path('app/public/images/user_photo_path.jpg'));
}
/** @test */
public function user_photo_function_has_overrideable_attributes()
{
$user = factory(User::class)->create([
'gender_id' => 1,
]);
$photoFile = 'images/icon_user_1.png';
$imageString = '<img';
$imageString .= ' src="'.asset($photoFile).'"';
$imageString .= ' class="123"';
$imageString .= ' style="display: inline"';
$imageString .= '>';
$overrides = [
'class' => '123',
'style' => 'display: inline',
];
$this->assertEquals($imageString, userPhoto($user, $overrides));
}
/** @test */
public function user_photo_function_returns_default_gender_logo_image_if_user_photo_file_doesnt_exists()
{
$user = factory(User::class)->create([
'gender_id' => 2,
'photo_path' => 'images/non_exists_photo_path.jpg',
]);
$photoFile = 'images/icon_user_2.png';
$imageString = '<img';
$imageString .= ' src="'.asset($photoFile).'"';
$imageString .= '>';
$this->assertEquals($imageString, userPhoto($user));
}
}
Loading…
Cancel
Save