Browse Source

Added user family chart

Added set parent from existing couple
Added user siblings relation (currently for same father)
Added couple to husband and wife relation
Added laravel debugbar
pull/3/head
Nafies Luthfi 9 years ago
parent
commit
367e8ed5c7
  1. 10
      app/Couple.php
  2. 8
      app/Http/Controllers/FamilyActionsController.php
  3. 34
      app/Http/Controllers/UsersController.php
  4. 24
      app/User.php
  5. 0
      bootstrap/app.php
  6. 0
      bootstrap/autoload.php
  7. 0
      bootstrap/cache/.gitignore
  8. 1
      composer.json
  9. 125
      composer.lock
  10. 1
      config/app.php
  11. 2
      resources/views/layouts/app.blade.php
  12. 86
      resources/views/users/chart.blade.php
  13. 25
      resources/views/users/partials/parent-spouse.blade.php
  14. 14
      resources/views/users/partials/siblings.blade.php
  15. 6
      resources/views/users/show.blade.php
  16. 2
      routes/web.php
  17. 2
      storage/debugbar/.gitignore
  18. 24
      tests/Feature/ManageUserFamiliesTest.php

10
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);
}
}

8
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);
}
}

34
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

24
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);
}
}

0
bootstrap/app.php

0
bootstrap/autoload.php

0
bootstrap/cache/.gitignore

1
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.*",

125
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": {

1
config/app.php

@ -168,6 +168,7 @@ return [
*/
Laravel\Tinker\TinkerServiceProvider::class,
Luthfi\FormField\FormFieldServiceProvider::class,
Barryvdh\Debugbar\ServiceProvider::class,
/*
* Application Service Providers...

2
resources/views/layouts/app.blade.php

@ -71,8 +71,10 @@
</div>
</nav>
<div class="container">
@yield('content')
</div>
</div>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>

86
resources/views/users/chart.blade.php

@ -0,0 +1,86 @@
@extends('layouts.app')
@section('content')
<div class="pull-right">
{{ link_to_route('users.show', 'Lihat Profil '.$user->name, [$user->id], ['class' => 'btn btn-default']) }}
</div>
<h3 class="page-header text-center">{{ $user->profileLink('chart') }}</h3>
</div>
<div class="container-fluid">
<div class="panel panel-default table-responsive">
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th style="width: 10%">Kakek & Nenek</th>
<td class="text-center" colspan="{{ $colspan }}">
<span class="col-xs-3">{{ $fatherGrandpa ? $fatherGrandpa->profileLink('chart') : '?' }}</span>
<span class="col-xs-3">{{ $fatherGrandma ? $fatherGrandma->profileLink('chart') : '?' }}</span>
<span class="col-xs-3" style="border-left:1px solid #ccc">
{{ $motherGrandpa ? $motherGrandpa->profileLink('chart') : '?' }}
</span>
<span class="col-xs-3">{{ $motherGrandma ? $motherGrandma->profileLink('chart') : '?' }}</span>
</td>
</tr>
<tr>
<th>Ayah & Ibu</th>
<td class="text-center" colspan="{{ $colspan }}">
<span class="col-xs-6">{{ $father ? $father->profileLink('chart') : '?' }}</span>
<span class="col-xs-6" style="border-left:1px solid #ccc">{{ $mother ? $mother->profileLink('chart') : '?' }}</span>
</td>
</tr>
<tr>
<th>&nbsp;</th>
<th class="text-center" colspan="{{ $colspan }}">{{ $user->profileLink('chart') }}</th>
</tr>
<tr>
<th>Anak-Anak & Cucu-Cucu</th>
@foreach($childs as $child)
<td>
{{ $child->profileLink('chart') }}
<ul style="padding-left: 18px">
@foreach($child->childs as $grand)
<li>{{ $grand->profileLink('chart') }}</li>
@endforeach
</ul>
</td>
@endforeach
</tr>
</tbody>
</table>
</div>
@foreach ($siblings->chunk(4) as $chunkedSiblings)
<div class="row">
@foreach ($chunkedSiblings as $sibling)
<div class="col-sm-3">
<div class="panel panel-default table-responsive">
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th style="width: 35%">Saudara</th>
<th class="text-center" colspan="{{ $sibling->childs->count() }}">{{ $sibling->profileLink('chart') }}</th>
</tr>
<tr>
<th>Keponakan & Cucu-Cucu</th>
<td>
<ol style="padding-left: 15px">
@foreach($sibling->childs as $child)
<li>
{{ $child->profileLink('chart') }}
<ul style="padding-left: 18px">
@foreach($child->childs as $grand)
<li>{{ $grand->profileLink('chart') }}</li>
@endforeach
</ul>
</li>
@endforeach
</ol>
</td>
</tr>
</tbody>
</table>
</div>
</div>
@endforeach
</div>
@endforeach
@endsection

25
resources/views/users/partials/parent-spouse.blade.php

@ -39,6 +39,31 @@
@endif
</td>
</tr>
<tr>
<th class="col-sm-4">Orang Tua</th>
<td class="col-sm-8">
<div class="pull-right">
@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
</div>
@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]]) }}
<div class="input-group">
{!! FormField::select('set_parent_id', $allMariageList, ['label' => false, 'class' => 'input-sm']) !!}
<span class="input-group-btn">
{{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_parent_button']) }}
</span>
</div>
{{ Form::close() }}
@endif
</td>
</tr>
@if ($user->gender_id == 1)
<tr>
<th>Isteri</th>

14
resources/views/users/partials/siblings.blade.php

@ -0,0 +1,14 @@
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">Saudara Kandung</h3></div>
<table class="table">
<tbody>
@foreach($user->siblings() as $sibling)
<tr>
<td>
{{ $sibling->profileLink() }} ({{ $sibling->gender }})
</td>
</tr>
@endforeach
</tbody>
</table>
</div>

6
resources/views/users/show.blade.php

@ -1,16 +1,18 @@
@extends('layouts.app')
@section('content')
<div class="container">
<div class="pull-right">
{{ link_to_route('users.chart', 'Lihat Chart Keluarga '.$currentUser->name, [$currentUser->id], ['class' => 'btn btn-default']) }}
</div>
<h3 class="page-header text-center">{{ $currentUser->profileLink() }}</h3>
<div class="row">
<div class="col-md-6">
@include('users.partials.profile', ['user' => $currentUser])
@include('users.partials.siblings', ['user' => $currentUser])
</div>
<div class="col-md-6">
@include('users.partials.parent-spouse', ['user' => $currentUser])
@include('users.partials.childs', ['user' => $currentUser])
</div>
</div>
</div>
@endsection

2
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');

2
storage/debugbar/.gitignore

@ -0,0 +1,2 @@
*
!.gitignore

24
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,
]);
}
}
Loading…
Cancel
Save