diff --git a/app/Http/Requests/Users/UpdateRequest.php b/app/Http/Requests/Users/UpdateRequest.php index b77795c..289f09e 100644 --- a/app/Http/Requests/Users/UpdateRequest.php +++ b/app/Http/Requests/Users/UpdateRequest.php @@ -30,6 +30,7 @@ class UpdateRequest extends FormRequest 'name' => 'required|string|max:255', 'gender_id' => 'required|numeric', 'dob' => 'nullable|date|date_format:Y-m-d', + 'yob' => 'nullable|date_format:Y', 'dod' => 'nullable|date|date_format:Y-m-d', 'yod' => 'nullable|date_format:Y', 'phone' => 'nullable|string|max:255', @@ -59,6 +60,12 @@ class UpdateRequest extends FormRequest $formData['yod'] = $formData['yod']; } + if ($formData['dob']) { + $formData['yob'] = substr($formData['dob'], 0, 4); + } else { + $formData['yob'] = $formData['yob']; + } + if ($formData['password']) { $formData['password'] = bcrypt($formData['password']); } diff --git a/app/User.php b/app/User.php index 11257d7..cd98314 100644 --- a/app/User.php +++ b/app/User.php @@ -2,6 +2,7 @@ namespace App; +use Carbon\Carbon; use Ramsey\Uuid\Uuid; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -27,7 +28,7 @@ class User extends Authenticatable 'nickname', 'gender_id', 'name', 'email', 'password', 'address', 'phone', - 'dob', 'dod', 'yod', 'city', + 'dob', 'yob', 'dod', 'yod', 'city', 'father_id', 'mother_id', 'parent_id', ]; @@ -230,4 +231,57 @@ class User extends Authenticatable { 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 '
'.$this->age.' '.trans_choice('user.age_years', $this->age).'
'; + } } diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index bf4c98a..d446047 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/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('parent_id')->nullable(); $table->date('dob')->nullable(); + $table->year('yob')->nullable(); $table->unsignedTinyInteger('birth_order')->nullable(); $table->date('dod')->nullable(); $table->year('yod')->nullable(); diff --git a/resources/lang/en/user.php b/resources/lang/en/user.php index 7b67031..ae87d63 100644 --- a/resources/lang/en/user.php +++ b/resources/lang/en/user.php @@ -18,6 +18,8 @@ return [ 'nieces' => 'Nieces', 'marriages' => 'Marriages', 'birth_order' => 'Birth Order', + 'age' => 'Age', + 'age_years' => 'year|years', // Actions 'edit' => 'Edit Profile', @@ -38,6 +40,7 @@ return [ 'mother' => 'Mother', 'parent' => 'Parent', 'dob' => 'Date of Birth', + 'yob' => 'Year of Birth', 'dod' => 'Date of Death', 'yod' => 'Year of Death', 'email' => 'Email', diff --git a/resources/lang/id/user.php b/resources/lang/id/user.php index e2425e0..ced0d3b 100644 --- a/resources/lang/id/user.php +++ b/resources/lang/id/user.php @@ -18,6 +18,8 @@ return [ 'nieces' => 'Keponakan', 'marriages' => 'Pernikahan', 'birth_order' => 'Anak ke', + 'age' => 'Usia', + 'age_years' => 'tahun', // Actions 'edit' => 'Edit Profil', @@ -38,6 +40,7 @@ return [ 'mother' => 'Ibu', 'parent' => 'Orang Tua', 'dob' => 'Tanggal Lahir', + 'yob' => 'Tahun Lahir', 'dod' => 'Tanggal Meninggal', 'yod' => 'Tahun Meninggal', 'email' => 'Email', diff --git a/resources/views/users/edit.blade.php b/resources/views/users/edit.blade.php index 158969d..1493cf0 100644 --- a/resources/views/users/edit.blade.php +++ b/resources/views/users/edit.blade.php @@ -67,14 +67,15 @@ {!! FormField::text('nickname', ['label' => trans('user.nickname')]) !!}
{!! FormField::radios('gender_id', [1 => trans('app.male_code'), trans('app.female_code')], ['label' => trans('user.gender')]) !!}
-
{!! FormField::text('dob', ['label' => trans('user.dob'), 'placeholder' => trans('app.example').' 1959-07-20']) !!}
-
-
{!! FormField::text('birth_order', ['label' => trans('user.birth_order'), 'type' => 'number', 'min' => 1]) !!}
+
{!! FormField::text('yob', ['label' => trans('user.yob'), 'placeholder' => trans('app.example').' 1959']) !!}
+
{!! FormField::text('dob', ['label' => trans('user.dob'), 'placeholder' => trans('app.example').' 1959-07-20']) !!}
+
+
{!! FormField::text('yod', ['label' => trans('user.yod'), 'placeholder' => trans('app.example').' 2003']) !!}
{!! FormField::text('dod', ['label' => trans('user.dod'), 'placeholder' => trans('app.example').' 2003-10-17']) !!}
diff --git a/resources/views/users/partials/profile.blade.php b/resources/views/users/partials/profile.blade.php index bc58061..3610084 100644 --- a/resources/views/users/partials/profile.blade.php +++ b/resources/views/users/partials/profile.blade.php @@ -31,6 +31,14 @@ {{ $user->dod }} @endif + + {{ trans('user.age') }} + + @if ($user->age) + {!! $user->age_string !!} + @endif + + @if ($user->email) {{ trans('user.email') }} diff --git a/resources/views/users/search.blade.php b/resources/views/users/search.blade.php index eb139e7..ffad122 100644 --- a/resources/views/users/search.blade.php +++ b/resources/views/users/search.blade.php @@ -27,8 +27,11 @@ @foreach ($chunkedUser as $user)
-
+
{{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }} + @if ($user->age) + {!! $user->age_string !!} + @endif

{{ $user->profileLink() }} ({{ $user->gender }})

diff --git a/tests/Feature/UsersProfileTest.php b/tests/Feature/UsersProfileTest.php index 2bdbf9b..76f43dc 100644 --- a/tests/Feature/UsersProfileTest.php +++ b/tests/Feature/UsersProfileTest.php @@ -31,6 +31,7 @@ class UsersProfileTest extends TestCase 'name' => 'Nama User', 'gender_id' => 1, 'dob' => '1959-06-09', + 'yob' => '', 'dod' => '2003-10-17', 'yod' => '', 'address' => 'Jln. Angkasa, No. 70', @@ -46,6 +47,7 @@ class UsersProfileTest extends TestCase 'name' => 'Nama User', 'gender_id' => 1, 'dob' => '1959-06-09', + 'yob' => '1959', 'dod' => '2003-10-17', 'yod' => '2003', 'address' => 'Jln. Angkasa, No. 70', diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 96c839d..ba231d5 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit; use App\User; use App\Couple; +use Carbon\Carbon; use Tests\TestCase; use Illuminate\Support\Collection; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -145,4 +146,83 @@ class UserTest extends TestCase $this->assertInstanceOf(Collection::class, $user->managedCouples); $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 = '
21 tahun
'; + $this->assertEquals($ageString, $user->age_string); + + Carbon::setTestNow(); + } }