Browse Source

Merge pull request #27 from nafiesl/person_age

Add user/person age attribute

Closes #26
pull/30/head
Nafies Luthfi 7 years ago
committed by GitHub
parent
commit
6349f8b215
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/Http/Requests/Users/UpdateRequest.php
  2. 56
      app/User.php
  3. 1
      database/migrations/2014_10_12_000000_create_users_table.php
  4. 3
      resources/lang/en/user.php
  5. 3
      resources/lang/id/user.php
  6. 7
      resources/views/users/edit.blade.php
  7. 8
      resources/views/users/partials/profile.blade.php
  8. 5
      resources/views/users/search.blade.php
  9. 2
      tests/Feature/UsersProfileTest.php
  10. 80
      tests/Unit/UserTest.php

7
app/Http/Requests/Users/UpdateRequest.php

@ -30,6 +30,7 @@ class UpdateRequest extends FormRequest
'name' => 'required|string|max:255', 'name' => 'required|string|max:255',
'gender_id' => 'required|numeric', 'gender_id' => 'required|numeric',
'dob' => 'nullable|date|date_format:Y-m-d', 'dob' => 'nullable|date|date_format:Y-m-d',
'yob' => 'nullable|date_format:Y',
'dod' => 'nullable|date|date_format:Y-m-d', 'dod' => 'nullable|date|date_format:Y-m-d',
'yod' => 'nullable|date_format:Y', 'yod' => 'nullable|date_format:Y',
'phone' => 'nullable|string|max:255', 'phone' => 'nullable|string|max:255',
@ -59,6 +60,12 @@ class UpdateRequest extends FormRequest
$formData['yod'] = $formData['yod']; $formData['yod'] = $formData['yod'];
} }
if ($formData['dob']) {
$formData['yob'] = substr($formData['dob'], 0, 4);
} else {
$formData['yob'] = $formData['yob'];
}
if ($formData['password']) { if ($formData['password']) {
$formData['password'] = bcrypt($formData['password']); $formData['password'] = bcrypt($formData['password']);
} }

56
app/User.php

@ -2,6 +2,7 @@
namespace App; namespace App;
use Carbon\Carbon;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
@ -27,7 +28,7 @@ class User extends Authenticatable
'nickname', 'gender_id', 'name', 'nickname', 'gender_id', 'name',
'email', 'password', 'email', 'password',
'address', 'phone', 'address', 'phone',
'dob', 'dod', 'yod', 'city',
'dob', 'yob', 'dod', 'yod', 'city',
'father_id', 'mother_id', 'parent_id', 'father_id', 'mother_id', 'parent_id',
]; ];
@ -230,4 +231,57 @@ class User extends Authenticatable
{ {
return $this->hasMany(Couple::class, 'manager_id'); return $this->hasMany(Couple::class, 'manager_id');
} }
public function getAgeAttribute()
{
$ageDetail = null;
$yearOnlySuffix = Carbon::now()->format('-m-d');
if ($this->dob && !$this->dod) {
$ageDetail = Carbon::parse($this->dob)->diffInYears();
}
if (!$this->dob && $this->yob) {
$ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->diffInYears();
}
if ($this->dob && $this->dod) {
$ageDetail = Carbon::parse($this->dob)->diffInYears($this->dod);
}
if (!$this->dob && $this->yob && !$this->dod && $this->yod) {
$ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->diffInYears($this->yod.$yearOnlySuffix);
}
if ($this->dob && $this->yob && $this->dod && $this->yod) {
$ageDetail = Carbon::parse($this->dob)->diffInYears($this->dod);
}
return $ageDetail;
}
public function getAgeDetailAttribute()
{
$ageDetail = null;
$yearOnlySuffix = Carbon::now()->format('-m-d');
if ($this->dob && !$this->dod) {
$ageDetail = Carbon::parse($this->dob)->timespan();
}
if (!$this->dob && $this->yob) {
$ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->timespan();
}
if ($this->dob && $this->dod) {
$ageDetail = Carbon::parse($this->dob)->timespan($this->dod);
}
if (!$this->dob && $this->yob && !$this->dod && $this->yod) {
$ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->timespan($this->yod.$yearOnlySuffix);
}
if ($this->dob && $this->yob && $this->dod && $this->yod) {
$ageDetail = Carbon::parse($this->dob)->timespan($this->dod);
}
return $ageDetail;
}
public function getAgeStringAttribute()
{
return '<div title="'.$this->age_detail.'">'.$this->age.' '.trans_choice('user.age_years', $this->age).'</div>';
}
} }

1
database/migrations/2014_10_12_000000_create_users_table.php

@ -22,6 +22,7 @@ class CreateUsersTable extends Migration
$table->uuid('mother_id')->nullable(); $table->uuid('mother_id')->nullable();
$table->uuid('parent_id')->nullable(); $table->uuid('parent_id')->nullable();
$table->date('dob')->nullable(); $table->date('dob')->nullable();
$table->year('yob')->nullable();
$table->unsignedTinyInteger('birth_order')->nullable(); $table->unsignedTinyInteger('birth_order')->nullable();
$table->date('dod')->nullable(); $table->date('dod')->nullable();
$table->year('yod')->nullable(); $table->year('yod')->nullable();

3
resources/lang/en/user.php

@ -18,6 +18,8 @@ return [
'nieces' => 'Nieces', 'nieces' => 'Nieces',
'marriages' => 'Marriages', 'marriages' => 'Marriages',
'birth_order' => 'Birth Order', 'birth_order' => 'Birth Order',
'age' => 'Age',
'age_years' => 'year|years',
// Actions // Actions
'edit' => 'Edit Profile', 'edit' => 'Edit Profile',
@ -38,6 +40,7 @@ return [
'mother' => 'Mother', 'mother' => 'Mother',
'parent' => 'Parent', 'parent' => 'Parent',
'dob' => 'Date of Birth', 'dob' => 'Date of Birth',
'yob' => 'Year of Birth',
'dod' => 'Date of Death', 'dod' => 'Date of Death',
'yod' => 'Year of Death', 'yod' => 'Year of Death',
'email' => 'Email', 'email' => 'Email',

3
resources/lang/id/user.php

@ -18,6 +18,8 @@ return [
'nieces' => 'Keponakan', 'nieces' => 'Keponakan',
'marriages' => 'Pernikahan', 'marriages' => 'Pernikahan',
'birth_order' => 'Anak ke', 'birth_order' => 'Anak ke',
'age' => 'Usia',
'age_years' => 'tahun',
// Actions // Actions
'edit' => 'Edit Profil', 'edit' => 'Edit Profil',
@ -38,6 +40,7 @@ return [
'mother' => 'Ibu', 'mother' => 'Ibu',
'parent' => 'Orang Tua', 'parent' => 'Orang Tua',
'dob' => 'Tanggal Lahir', 'dob' => 'Tanggal Lahir',
'yob' => 'Tahun Lahir',
'dod' => 'Tanggal Meninggal', 'dod' => 'Tanggal Meninggal',
'yod' => 'Tahun Meninggal', 'yod' => 'Tahun Meninggal',
'email' => 'Email', 'email' => 'Email',

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

@ -67,14 +67,15 @@
{!! FormField::text('nickname', ['label' => trans('user.nickname')]) !!} {!! FormField::text('nickname', ['label' => trans('user.nickname')]) !!}
<div class="row"> <div class="row">
<div class="col-md-6">{!! FormField::radios('gender_id', [1 => trans('app.male_code'), trans('app.female_code')], ['label' => trans('user.gender')]) !!}</div> <div class="col-md-6">{!! FormField::radios('gender_id', [1 => trans('app.male_code'), trans('app.female_code')], ['label' => trans('user.gender')]) !!}</div>
<div class="col-md-6">{!! FormField::text('dob', ['label' => trans('user.dob'), 'placeholder' => trans('app.example').' 1959-07-20']) !!}</div>
</div>
<div class="row">
<div class="col-md-4"> <div class="col-md-4">
{!! FormField::text('birth_order', ['label' => trans('user.birth_order'), 'type' => 'number', 'min' => 1]) !!} {!! FormField::text('birth_order', ['label' => trans('user.birth_order'), 'type' => 'number', 'min' => 1]) !!}
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-md-6">{!! FormField::text('yob', ['label' => trans('user.yob'), 'placeholder' => trans('app.example').' 1959']) !!}</div>
<div class="col-md-6">{!! FormField::text('dob', ['label' => trans('user.dob'), 'placeholder' => trans('app.example').' 1959-07-20']) !!}</div>
</div>
<div class="row">
<div class="col-md-6">{!! FormField::text('yod', ['label' => trans('user.yod'), 'placeholder' => trans('app.example').' 2003']) !!}</div> <div class="col-md-6">{!! FormField::text('yod', ['label' => trans('user.yod'), 'placeholder' => trans('app.example').' 2003']) !!}</div>
<div class="col-md-6">{!! FormField::text('dod', ['label' => trans('user.dod'), 'placeholder' => trans('app.example').' 2003-10-17']) !!}</div> <div class="col-md-6">{!! FormField::text('dod', ['label' => trans('user.dod'), 'placeholder' => trans('app.example').' 2003-10-17']) !!}</div>
</div> </div>

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

@ -31,6 +31,14 @@
<td>{{ $user->dod }}</td> <td>{{ $user->dod }}</td>
</tr> </tr>
@endif @endif
<tr>
<th>{{ trans('user.age') }}</th>
<td>
@if ($user->age)
{!! $user->age_string !!}
@endif
</td>
</tr>
@if ($user->email) @if ($user->email)
<tr> <tr>
<th>{{ trans('user.email') }}</th> <th>{{ trans('user.email') }}</th>

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

@ -27,8 +27,11 @@
@foreach ($chunkedUser as $user) @foreach ($chunkedUser as $user)
<div class="col-md-3"> <div class="col-md-3">
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">
<div class="panel-heading text-center">
{{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }} {{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }}
@if ($user->age)
{!! $user->age_string !!}
@endif
</div> </div>
<div class="panel-body"> <div class="panel-body">
<h3 class="panel-title">{{ $user->profileLink() }} ({{ $user->gender }})</h3> <h3 class="panel-title">{{ $user->profileLink() }} ({{ $user->gender }})</h3>

2
tests/Feature/UsersProfileTest.php

@ -31,6 +31,7 @@ class UsersProfileTest extends TestCase
'name' => 'Nama User', 'name' => 'Nama User',
'gender_id' => 1, 'gender_id' => 1,
'dob' => '1959-06-09', 'dob' => '1959-06-09',
'yob' => '',
'dod' => '2003-10-17', 'dod' => '2003-10-17',
'yod' => '', 'yod' => '',
'address' => 'Jln. Angkasa, No. 70', 'address' => 'Jln. Angkasa, No. 70',
@ -46,6 +47,7 @@ class UsersProfileTest extends TestCase
'name' => 'Nama User', 'name' => 'Nama User',
'gender_id' => 1, 'gender_id' => 1,
'dob' => '1959-06-09', 'dob' => '1959-06-09',
'yob' => '1959',
'dod' => '2003-10-17', 'dod' => '2003-10-17',
'yod' => '2003', 'yod' => '2003',
'address' => 'Jln. Angkasa, No. 70', 'address' => 'Jln. Angkasa, No. 70',

80
tests/Unit/UserTest.php

@ -4,6 +4,7 @@ namespace Tests\Unit;
use App\User; use App\User;
use App\Couple; use App\Couple;
use Carbon\Carbon;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
@ -145,4 +146,83 @@ class UserTest extends TestCase
$this->assertInstanceOf(Collection::class, $user->managedCouples); $this->assertInstanceOf(Collection::class, $user->managedCouples);
$this->assertInstanceOf(Couple::class, $user->managedCouples->first()); $this->assertInstanceOf(Couple::class, $user->managedCouples->first());
} }
/**
* @test
* @dataProvider userAgeDataProvider
*/
public function user_has_age_attribute($today, $dob, $yob, $dod, $yod, $age)
{
Carbon::setTestNow($today);
$user = factory(User::class)->make([
'dob' => $dob, 'yob' => $yob, 'dod' => $dod, 'yod' => $yod,
]);
$this->assertEquals($age, $user->age);
Carbon::setTestNow();
}
/**
* Provide data for calculating user age.
* Returning array of today, dob, yob, dod, yod, and age.
*
* @return array
*/
public function userAgeDataProvider()
{
return [
['2018-02-02', '1997-01-01', '1997', null, null, 21],
['2018-02-02', '1997-01-01', null, null, null, 21],
['2018-02-02', null, '1997', null, null, 21],
['2018-02-02', '1997-01-01', '1997', '2017-01-01', '2017', 20],
['2018-02-02', null, '1997', null, '2017', 20],
];
}
/**
* @test
* @dataProvider userAgeDetailDataProvider
*/
public function user_has_age_detail_attribute($today, $dob, $yob, $dod, $yod, $age)
{
Carbon::setTestNow($today);
$user = factory(User::class)->make([
'dob' => $dob, 'yob' => $yob, 'dod' => $dod, 'yod' => $yod,
]);
$this->assertEquals($age, $user->age_detail);
Carbon::setTestNow();
}
/**
* Provide data for calculating user age detail.
* Returning array of today, dob, yob, dod, yod, and age.
*
* @return array
*/
public function userAgeDetailDataProvider()
{
return [
['2018-02-02', '1997-01-01', '1997', null, null, '21 tahun, 1 bulan, 1 hari'],
['2018-02-02', '1997-01-01', null, null, null, '21 tahun, 1 bulan, 1 hari'],
['2018-02-02', null, '1997', null, null, '21 tahun'],
['2018-02-02', '1997-01-01', '1997', '2017-01-01', '2017', '20 tahun'],
['2018-02-02', null, '1997', null, '2017', '20 tahun'],
];
}
/** @test */
public function user_has_age_string_attribute()
{
$today = '2018-02-02';
Carbon::setTestNow($today);
$user = factory(User::class)->make(['dob' => '1997-01-01']);
$ageString = '<div title="21 tahun, 1 bulan, 1 hari">21 tahun</div>';
$this->assertEquals($ageString, $user->age_string);
Carbon::setTestNow();
}
} }
Loading…
Cancel
Save