From 0d68c20813c766d6c24faf9621248f4ed652c212 Mon Sep 17 00:00:00 2001
From: LIQRGV <5935057+LIQRGV@users.noreply.github.com>
Date: Sat, 4 Jan 2020 17:16:02 +0700
Subject: [PATCH] WIP
---
app/DescendantEnum.php | 14 +++++
app/Helpers/functions.php | 87 ++++++++++++++++++++++++++++++++
app/Http/Controllers/UsersController.php | 3 +-
app/User.php | 45 +++++++++++++++++
resources/views/users/tree3.blade.php | 33 ++++++++++++
5 files changed, 181 insertions(+), 1 deletion(-)
create mode 100644 app/DescendantEnum.php
create mode 100644 resources/views/users/tree3.blade.php
diff --git a/app/DescendantEnum.php b/app/DescendantEnum.php
new file mode 100644
index 0000000..6c73269
--- /dev/null
+++ b/app/DescendantEnum.php
@@ -0,0 +1,14 @@
+name, [$user->id], ['title' => $user->name.' ('.$user->gender.')']);
+ $header = <<$linkToRoute
+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 = <<$linkToRoute
+HTML;
+
+ $soleString = "";
+ if ($parent && $parent->childs->count() === 1) {
+ $soleString = "sole";
+ }
+
+ $header = <<
+ $basicHeader
+HTML;
+ $childContainer = "";
+
+ if ($user->childs->count() !== 0 ) {
+ $nextLevel = $level + 1;
+ $childContainer = "
";
+ foreach ($user->childs as $child) {
+ $childContainer .= $createFamilyTree($child, $user, $nextLevel);
+ }
+ $childContainer .= "
";
+ }
+
+ $footer = <<
+HTML;
+
+ return $header . $childContainer . $footer;
+ };
+
+ $childContainer = <<
+HTML;
+ foreach ($user->childs as $child) {
+ $childContainer .= $createFamilyTree($child, $user, 1);
+ }
+ $childContainer .= <<
+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 .= <<Jumlah $currentDescendantString
+$descendantCount
+HTML;
+
+ }
+
+ return $result;
+}
diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php
index 5619a86..067bbe4 100644
--- a/app/Http/Controllers/UsersController.php
+++ b/app/Http/Controllers/UsersController.php
@@ -99,8 +99,9 @@ class UsersController extends Controller
/**
* Show the form for editing the specified User.
*
- * @param \App\User $user
+ * @param \App\User $user
* @return \Illuminate\View\View
+ * @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function edit(User $user)
{
diff --git a/app/User.php b/app/User.php
index cd98314..1e717e6 100644
--- a/app/User.php
+++ b/app/User.php
@@ -284,4 +284,49 @@ class User extends Authenticatable
{
return ''.$this->age.' '.trans_choice('user.age_years', $this->age).'
';
}
+
+ /**
+ * 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;
+ }
}
diff --git a/resources/views/users/tree3.blade.php b/resources/views/users/tree3.blade.php
new file mode 100644
index 0000000..47d0cc4
--- /dev/null
+++ b/resources/views/users/tree3.blade.php
@@ -0,0 +1,33 @@
+@extends('layouts.user-profile-wide')
+
+@section('subtitle', trans('app.family_tree'))
+
+@section('user-content')
+
+
+
+
+ {!! createFamilyTree($user) !!}
+
+
+
+
+
+
+ {!! showFamilyTreeCount($user, 4) !!}
+
+
+
+
+@endsection
+
+@section ('ext_css')
+
+@endsection
+