Browse Source

WIP

pull/39/head
LIQRGV 6 years ago
parent
commit
0d68c20813
No known key found for this signature in database GPG Key ID: C52335070B62F025
  1. 14
      app/DescendantEnum.php
  2. 87
      app/Helpers/functions.php
  3. 1
      app/Http/Controllers/UsersController.php
  4. 45
      app/User.php
  5. 33
      resources/views/users/tree3.blade.php

14
app/DescendantEnum.php

@ -0,0 +1,14 @@
<?php
namespace App;
class DescendantEnum
{
const ANAK = 0;
const CUCU = 1;
const CICIT = 2;
const CANGGAH = 3;
const WARENG = 4;
}

87
app/Helpers/functions.php

@ -1,5 +1,6 @@
<?php
use App\DescendantEnum;
use App\User;
/**
@ -58,3 +59,89 @@ function userPhotoPath($photoPath, $genderId)
return asset('images/icon_user_'.$genderId.'.png');
}
/**
* Create family tree for specified user.
*
* @param User $user
* @return string
*/
function createFamilyTree(User $user) {
$linkToRoute = link_to_route('users.tree', $user->name, [$user->id], ['title' => $user->name.' ('.$user->gender.')']);
$header = <<<HTML
<span class="label">$linkToRoute</span>
HTML;
$createFamilyTree = function ($user, User $parent = null, $level = 1) use (&$createFamilyTree) {
$linkToRoute = link_to_route('users.tree', $user->name, [$user->id], ['title' => $user->name.' ('.$user->gender.')']);
$basicHeader = <<<HTML
<span class="label">$linkToRoute</span>
HTML;
$soleString = "";
if ($parent && $parent->childs->count() === 1) {
$soleString = "sole";
}
$header = <<<HTML
<div class="entry $soleString">
$basicHeader
HTML;
$childContainer = "";
if ($user->childs->count() !== 0 ) {
$nextLevel = $level + 1;
$childContainer = "<div class=\"branch lv$nextLevel\">";
foreach ($user->childs as $child) {
$childContainer .= $createFamilyTree($child, $user, $nextLevel);
}
$childContainer .= "</div>";
}
$footer = <<<HTML
</div>
HTML;
return $header . $childContainer . $footer;
};
$childContainer = <<<HTML
<div class="branch lv1">
HTML;
foreach ($user->childs as $child) {
$childContainer .= $createFamilyTree($child, $user, 1);
}
$childContainer .= <<<HTML
</div>
HTML;
return $header . $childContainer;
}
function showFamilyTreeCount(User $user, $limit = -1)
{
$userDescendantClass = new ReflectionClass(DescendantEnum::class);
$descendantString = array_flip($userDescendantClass->getConstants());
if(!$user) {
return "";
}
$descendantCounts = $user->getChildCount($limit);
$result = "";
foreach ($descendantCounts as $key => $descendantCount) {
if ($descendantCount === 0) {
break;
}
$currentDescendantString = ucfirst(strtolower($descendantString[$key]));
$result .= <<<HTML
<div class="col-md-1 text-right">Jumlah $currentDescendantString</div>
<div class="col-md-1 text-left"><strong style="font-size:30px">$descendantCount</strong></div>
HTML;
}
return $result;
}

1
app/Http/Controllers/UsersController.php

@ -101,6 +101,7 @@ class UsersController extends Controller
*
* @param \App\User $user
* @return \Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function edit(User $user)
{

45
app/User.php

@ -284,4 +284,49 @@ class User extends Authenticatable
{
return '<div title="'.$this->age_detail.'">'.$this->age.' '.trans_choice('user.age_years', $this->age).'</div>';
}
/**
* Get child count based on the level
*
* Example usage:
* $childCount = $user->getChildCount();
* $childCount[0]; // this indicate direct child
* $childCount[1]; // this indicate child of level 0
* $childCount[2]; // this indicate child of level 1
* and so on
*
* @param int $levelLimit
* @return array
*/
public function getChildCount($levelLimit = -1)
{
$childCount = [];
$this->_getChildCount($this, 0, $childCount, $levelLimit);
return $childCount;
}
private function _getChildCount(User $user, $currentLevel, &$childCount, $levelLimit)
{
$affectedLevel = $this->getAffectedLevel($currentLevel, $levelLimit);
if(!isset($childCount[$affectedLevel])) {
$childCount[$affectedLevel] = $user->childs->count();
} else {
$childCount[$affectedLevel] += $user->childs->count();
}
foreach ($user->childs as $child) {
$this->_getChildCount($child, $currentLevel + 1, $childCount, $levelLimit);
}
}
private function getAffectedLevel($currentLevel, $levelLimit)
{
if($levelLimit > 0 && $levelLimit < $currentLevel) {
return $levelLimit;
}
return $currentLevel;
}
}

33
resources/views/users/tree3.blade.php

@ -0,0 +1,33 @@
@extends('layouts.user-profile-wide')
@section('subtitle', trans('app.family_tree'))
@section('user-content')
<?php
$childsTotal = 0;
$grandChildsTotal = 0;
$ggTotal = 0;
$ggcTotal = 0;
$ggccTotal = 0;
?>
<div id="wrapper">
{!! createFamilyTree($user) !!}
</div>
<div class="container">
<hr>
<div class="row">
<div class="col-md-1">&nbsp;</div>
{!! showFamilyTreeCount($user, 4) !!}
<div class="col-md-1">&nbsp;</div>
</div>
</div>
@endsection
@section ('ext_css')
<link rel="stylesheet" href="{{ asset('css/tree.css') }}">
@endsection
Loading…
Cancel
Save