diff --git a/app/Couple.php b/app/Couple.php index a606be5..f2db974 100644 --- a/app/Couple.php +++ b/app/Couple.php @@ -6,5 +6,13 @@ use Illuminate\Database\Eloquent\Model; class Couple extends Model { - // + public function husband() + { + return $this->belongsTo(User::class); + } + + public function wife() + { + return $this->belongsTo(User::class); + } } diff --git a/app/Http/Controllers/FamilyActionsController.php b/app/Http/Controllers/FamilyActionsController.php index 6d6ed54..c4ce316 100644 --- a/app/Http/Controllers/FamilyActionsController.php +++ b/app/Http/Controllers/FamilyActionsController.php @@ -127,4 +127,12 @@ class FamilyActionsController extends Controller return back(); } + + public function setParent(Request $request, User $user) + { + $user->parent_id = $request->get('set_parent_id'); + $user->save(); + + return redirect()->route('users.show', $user->id); + } } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 91c7794..ebaeef1 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Couple; use App\User; use Illuminate\Http\Request; @@ -51,6 +52,11 @@ class UsersController extends Controller $usersMariageList[$spouse->pivot->id] = $user->name.' & '.$spouse->name; } + $allMariageList = []; + foreach (Couple::with('husband','wife')->get() as $couple) { + $allMariageList[$couple->id] = $couple->husband->name.' & '.$couple->wife->name; + } + $malePersonList = User::where('gender_id', 1)->pluck('nickname', 'id'); $femalePersonList = User::where('gender_id', 2)->pluck('nickname', 'id'); @@ -58,11 +64,37 @@ class UsersController extends Controller 'currentUser' => $user, 'usersMariageList' => $usersMariageList, 'malePersonList' => $malePersonList, - 'femalePersonList' => $femalePersonList + 'femalePersonList' => $femalePersonList, + 'allMariageList' => $allMariageList ]); } /** + * Display the user's family chart. + * + * @param \App\User $user + * @return \Illuminate\Http\Response + */ + public function chart(User $user) + { + $father = $user->father_id ? $user->father : null; + $mother = $user->mother_id ? $user->mother : null; + + $fatherGrandpa = $father && $father->father_id ? $father->father : null; + $fatherGrandma = $father && $father->mother_id ? $father->mother : null; + + $motherGrandpa = $mother && $mother->father_id ? $mother->father : null; + $motherGrandma = $mother && $mother->mother_id ? $mother->mother : null; + + $childs = $user->childs; + $colspan = $childs->count(); + $colspan = $colspan < 4 ? 4 : $colspan; + + $siblings = $user->siblings(); + return view('users.chart', compact('user', 'childs', 'father', 'mother', 'fatherGrandpa', 'fatherGrandma', 'motherGrandpa', 'motherGrandma', 'siblings', 'colspan')); + } + + /** * Show the form for editing the specified resource. * * @param \App\User $user diff --git a/app/User.php b/app/User.php index ae22fe7..7d66629 100644 --- a/app/User.php +++ b/app/User.php @@ -90,9 +90,10 @@ class User extends Authenticatable return $this->hasMany(User::class, 'father_id'); } - public function profileLink() + public function profileLink($type = 'profile') { - return link_to_route('users.show', $this->name, [$this->id]); + $type = ($type == 'chart') ? 'chart' : 'show'; + return link_to_route('users.'.$type, $this->name, [$this->id]); } public function wifes() @@ -132,4 +133,23 @@ class User extends Authenticatable return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps(); } + + public function siblings() + { + // TODO: research for fit siblings query + return User::where(function ($query) { + $query->where('id', '!=', $this->id); + if ($this->father_id != null) + $query->where('father_id', $this->father_id); + // if ($this->mother_id != null) + // $query->where('mother_id', $this->mother_id); + // if ($this->parent_id != null) + // $query->where('parent_id', $this->parent_id); + })->get(); + } + + public function parent() + { + return $this->belongsTo(Couple::class); + } } diff --git a/bootstrap/app.php b/bootstrap/app.php old mode 100644 new mode 100755 diff --git a/bootstrap/autoload.php b/bootstrap/autoload.php old mode 100644 new mode 100755 diff --git a/bootstrap/cache/.gitignore b/bootstrap/cache/.gitignore old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json index d23b2d6..06275cc 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "luthfi/formfield": "^0.1.6" }, "require-dev": { + "barryvdh/laravel-debugbar": "^2.4", "fzaninotto/faker": "~1.4", "laravel/browser-kit-testing": "^1", "mockery/mockery": "0.9.*", diff --git a/composer.lock b/composer.lock index 0ea33cb..5addda9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "27fbe283b6f1d75a727cf51cee225967", + "content-hash": "07477f8810af9f131f85a76202d51325", "packages": [ { "name": "dnoegel/php-xdg-base-dir", @@ -1982,6 +1982,68 @@ ], "packages-dev": [ { + "name": "barryvdh/laravel-debugbar", + "version": "v2.4.0", + "source": { + "type": "git", + "url": "https://github.com/barryvdh/laravel-debugbar.git", + "reference": "de15d00a74696db62e1b4782474c27ed0c4fc763" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barryvdh/laravel-debugbar/zipball/de15d00a74696db62e1b4782474c27ed0c4fc763", + "reference": "de15d00a74696db62e1b4782474c27ed0c4fc763", + "shasum": "" + }, + "require": { + "illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*", + "maximebf/debugbar": "~1.13.0", + "php": ">=5.5.9", + "symfony/finder": "~2.7|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.4-dev" + }, + "laravel": { + "providers": [ + "Barryvdh\\Debugbar\\ServiceProvider" + ], + "aliases": { + "Debugbar": "Barryvdh\\Debugbar\\Facade" + } + } + }, + "autoload": { + "psr-4": { + "Barryvdh\\Debugbar\\": "src/" + }, + "files": [ + "src/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "PHP Debugbar integration for Laravel", + "keywords": [ + "debug", + "debugbar", + "laravel", + "profiler", + "webprofiler" + ], + "time": "2017-06-01T17:46:08+00:00" + }, + { "name": "doctrine/instantiator", "version": "1.0.5", "source": { @@ -2176,6 +2238,67 @@ "time": "2017-02-08T22:32:37+00:00" }, { + "name": "maximebf/debugbar", + "version": "1.13.1", + "source": { + "type": "git", + "url": "https://github.com/maximebf/php-debugbar.git", + "reference": "afee79a236348e39a44cb837106b7c5b4897ac2a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/afee79a236348e39a44cb837106b7c5b4897ac2a", + "reference": "afee79a236348e39a44cb837106b7c5b4897ac2a", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "psr/log": "^1.0", + "symfony/var-dumper": "^2.6|^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0|^5.0" + }, + "suggest": { + "kriswallsmith/assetic": "The best way to manage assets", + "monolog/monolog": "Log using Monolog", + "predis/predis": "Redis storage" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.13-dev" + } + }, + "autoload": { + "psr-4": { + "DebugBar\\": "src/DebugBar/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Maxime Bouroumeau-Fuseau", + "email": "maxime.bouroumeau@gmail.com", + "homepage": "http://maximebf.com" + }, + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "Debug bar in the browser for php application", + "homepage": "https://github.com/maximebf/php-debugbar", + "keywords": [ + "debug", + "debugbar" + ], + "time": "2017-01-05T08:46:19+00:00" + }, + { "name": "mockery/mockery", "version": "0.9.9", "source": { diff --git a/config/app.php b/config/app.php index 91a5552..653c78b 100644 --- a/config/app.php +++ b/config/app.php @@ -168,6 +168,7 @@ return [ */ Laravel\Tinker\TinkerServiceProvider::class, Luthfi\FormField\FormFieldServiceProvider::class, + Barryvdh\Debugbar\ServiceProvider::class, /* * Application Service Providers... diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 260606c..fcbd464 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -71,7 +71,9 @@ +
@yield('content') +
diff --git a/resources/views/users/chart.blade.php b/resources/views/users/chart.blade.php new file mode 100644 index 0000000..e21c7b6 --- /dev/null +++ b/resources/views/users/chart.blade.php @@ -0,0 +1,86 @@ +@extends('layouts.app') + +@section('content') +
+ {{ link_to_route('users.show', 'Lihat Profil '.$user->name, [$user->id], ['class' => 'btn btn-default']) }} +
+ + +
+
+ + + + + + + + + + + + + + + + + @foreach($childs as $child) + + @endforeach + + +
Kakek & Nenek + {{ $fatherGrandpa ? $fatherGrandpa->profileLink('chart') : '?' }} + {{ $fatherGrandma ? $fatherGrandma->profileLink('chart') : '?' }} + + {{ $motherGrandpa ? $motherGrandpa->profileLink('chart') : '?' }} + + {{ $motherGrandma ? $motherGrandma->profileLink('chart') : '?' }} +
Ayah & Ibu + {{ $father ? $father->profileLink('chart') : '?' }} + {{ $mother ? $mother->profileLink('chart') : '?' }} +
 {{ $user->profileLink('chart') }}
Anak-Anak & Cucu-Cucu + {{ $child->profileLink('chart') }} +
    + @foreach($child->childs as $grand) +
  • {{ $grand->profileLink('chart') }}
  • + @endforeach +
+
+
+@foreach ($siblings->chunk(4) as $chunkedSiblings) +
+ @foreach ($chunkedSiblings as $sibling) +
+
+ + + + + + + + + + + +
Saudara{{ $sibling->profileLink('chart') }}
Keponakan & Cucu-Cucu +
    + @foreach($sibling->childs as $child) +
  1. + {{ $child->profileLink('chart') }} +
      + @foreach($child->childs as $grand) +
    • {{ $grand->profileLink('chart') }}
    • + @endforeach +
    +
  2. + @endforeach +
+
+
+
+ @endforeach +
+@endforeach +@endsection diff --git a/resources/views/users/partials/parent-spouse.blade.php b/resources/views/users/partials/parent-spouse.blade.php index e3eb4fb..3490186 100644 --- a/resources/views/users/partials/parent-spouse.blade.php +++ b/resources/views/users/partials/parent-spouse.blade.php @@ -39,6 +39,31 @@ @endif + + Orang Tua + +
+ @if (request('action') == 'set_parent') + {{ link_to_route('users.show', 'Batal', [$user->id], ['class' => 'btn btn-default btn-xs']) }} + @else + {{ link_to_route('users.show', 'Set Orang Tua', [$user->id, 'action' => 'set_parent'], ['class' => 'btn btn-success btn-xs']) }} + @endif +
+ @if ($user->parent) + {{ $user->parent->husband->name }} & {{ $user->parent->wife->name }} + @endif + @if (request('action') == 'set_parent') + {{ Form::open(['route' => ['family-actions.set-parent', $user->id]]) }} +
+ {!! FormField::select('set_parent_id', $allMariageList, ['label' => false, 'class' => 'input-sm']) !!} + + {{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_parent_button']) }} + +
+ {{ Form::close() }} + @endif + + @if ($user->gender_id == 1) Isteri diff --git a/resources/views/users/partials/siblings.blade.php b/resources/views/users/partials/siblings.blade.php new file mode 100644 index 0000000..0cf7541 --- /dev/null +++ b/resources/views/users/partials/siblings.blade.php @@ -0,0 +1,14 @@ +
+

Saudara Kandung

+ + + @foreach($user->siblings() as $sibling) + + + + @endforeach + +
+ {{ $sibling->profileLink() }} ({{ $sibling->gender }}) +
+
\ No newline at end of file diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index 6be1ada..1b52753 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -1,16 +1,18 @@ @extends('layouts.app') @section('content') -
+
+ {{ link_to_route('users.chart', 'Lihat Chart Keluarga '.$currentUser->name, [$currentUser->id], ['class' => 'btn btn-default']) }} +
@include('users.partials.profile', ['user' => $currentUser]) + @include('users.partials.siblings', ['user' => $currentUser])
@include('users.partials.parent-spouse', ['user' => $currentUser]) @include('users.partials.childs', ['user' => $currentUser])
-
@endsection diff --git a/routes/web.php b/routes/web.php index 20f62ac..ba0020b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,5 +24,7 @@ Route::post('family-actions/{user}/set-mother', 'FamilyActionsController@setMoth Route::post('family-actions/{user}/add-child', 'FamilyActionsController@addChild')->name('family-actions.add-child'); Route::post('family-actions/{user}/add-wife', 'FamilyActionsController@addWife')->name('family-actions.add-wife'); Route::post('family-actions/{user}/add-husband', 'FamilyActionsController@addHusband')->name('family-actions.add-husband'); +Route::post('family-actions/{user}/set-parent', 'FamilyActionsController@setParent')->name('family-actions.set-parent'); Route::get('users/{user}', 'UsersController@show')->name('users.show'); +Route::get('users/{user}/chart', 'UsersController@chart')->name('users.chart'); diff --git a/storage/debugbar/.gitignore b/storage/debugbar/.gitignore new file mode 100755 index 0000000..c96a04f --- /dev/null +++ b/storage/debugbar/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index 2aef4d8..181dbbe 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -240,4 +240,28 @@ class ManageUserFamiliesTest extends TestCase 'wife_id' => $user->id, ]); } + + /** @test */ + public function user_can_set_parent_from_existing_couple_id() + { + $husband = factory(User::class)->states('male')->create(); + $wife = factory(User::class)->states('female')->create(); + $husband->addWife($wife); + + $marriageId = $husband->wifes->first()->pivot->id; + $user = $this->loginAsUser(); + + $this->visit(route('profile')); + $this->click('Set Orang Tua'); + $this->seeElement('select', ['name' => 'set_parent_id']); + + $this->submitForm('set_parent_button', [ + 'set_parent_id' => $marriageId, + ]); + + $this->seeInDatabase('users', [ + 'id' => $user->id, + 'parent_id' => $marriageId, + ]); + } }