Ngiler SH3LL 360
Home
Information
Create File
Create Folder
:
/
home
/
tbf
/
membrubackend
/
app
/
Http
/
Controllers
/
Api
/
Information Server
MySQL :
OFF
Perl :
OFF
CURL :
ON
WGET :
OFF
PKEXEC :
OFF
Directive
Local Value
IP Address
89.40.16.97
System
Linux server.atelieruldeit.ro 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
User
tbf
PHP Version
7.3.33
Software
Apache
Doc root
Writable
close
Edit File :
MasterGoalController.php
| Size :
27.61
KB
Copy
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\MasterGoal; use App\Models\Instance; use App\Models\User; use App\Models\Objective; use App\Http\Resources\MasterGoalCollection; use App\Http\Resources\EditMasterGoalResource; use App\Http\Requests\MasterGoalFormRequest; use App\Http\Requests\MasterGoalChangeStatusFormRequest; use App\Http\Requests\MasterGoalChangeShareStatusFormRequest; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Database\Eloquent\ModelNotFoundException; use Auth; use Arr; use Session; class MasterGoalController extends Controller { protected $masterGoalsTree; protected $allVisions; protected $allObjectives; protected $allKeyResults; protected $masterGoalChildrenIds; protected $masterGoalWithUserInvolved; public function index(Instance $instance, Request $request) { $authUser = Auth::user(); $master_goals = $instance->master_goals()->whereNull('master_goal_id'); if($request->status){ $master_goals = $master_goals->where('status', $request->status); } return new MasterGoalCollection( $master_goals->latest()->get() ); } public function store(MasterGoalFormRequest $request) { $data = $request->validated(); $masterGoal = MasterGoal::create($data); // set manager users for this master goal if(isset($data['manager_users']) && $data['manager_users']){ $masterGoal->users()->sync($data['manager_users']); setAccessManagerToMasterGoal($data['manager_users'], [$masterGoal->id]); } // check if this master goal is assigend to another master goal that has manager users if(isset($data['master_goal_id'])){ setAccessManagerToMasterGoalParent($masterGoal->instance, $data['master_goal_id'], [$masterGoal->id]); } // set private_master_goal_ids for all users except the manager if($masterGoal->is_private){ $masterGoal->instance->users->each(function ($user) use ($masterGoal) { if(!checkManagerAccess($masterGoal->id, $user)){ setPrivateMasterGoalId($user, $masterGoal->id); } }); } return response()->json([ 'status' => 'success', 'data' => $masterGoal ], 200); } public function show(MasterGoal $masterGoal) { $authUser = Auth::user(); $masterGoals = $authUser->instance->master_goals()->whereNotNull('master_goal_id')->get()->prepend($masterGoal); $data = $this->recursiveMasterGoal($masterGoals, $masterGoal->id, $masterGoal->id, $authUser); // check if array values is not null $allVisions = $this->allVisions ? array_values($this->allVisions) : null; $allObjectives = $this->allObjectives ? array_values($this->allObjectives) : null; $allKeyResults = $this->allKeyResults ? array_values($this->allKeyResults) : null; return response()->json([ 'status' => 'success', 'data' => !empty($data) ? $data[0] : null, 'all_visions' => !empty($data) ? $allVisions : null, 'all_objectives' => !empty($data) ? $allObjectives : null, 'all_key_results' => !empty($data) ? $allKeyResults : null, ], 200); } public function publicLink($masterGoalUuid) { $masterGoal = MasterGoal::where('uuid',$masterGoalUuid)->first(); // check if the entity is available for public if($masterGoal){ if($masterGoal->share_status == 'off'){ throw (new ModelNotFoundException); } Session::put('instance_id', $masterGoal->instance_id); $masterGoals = $masterGoal->instance->master_goals()->get()->prepend($masterGoal); $data = $this->recursiveMasterGoal($masterGoals, $masterGoal->id, $masterGoal->id, null); // check if array values is not null $allVisions = $this->allVisions ? array_values($this->allVisions) : null; $allObjectives = $this->allObjectives ? array_values($this->allObjectives) : null; $allKeyResults = $this->allKeyResults ? array_values($this->allKeyResults) : null; Session::forget('instance_id'); return response()->json([ 'status' => 'success', 'data' => !empty($data) ? $data[0] : null, 'all_visions' => !empty($data) ? $allVisions : null, 'all_objectives' => !empty($data) ? $allObjectives : null, 'all_key_results' => !empty($data) ? $allKeyResults : null, ], 200); }else{ throw (new ModelNotFoundException); } } public function edit(MasterGoal $masterGoal) { return new EditMasterGoalResource( $masterGoal ); } public function update(MasterGoalFormRequest $request, MasterGoal $masterGoal) { $data = $request->validated(); $instance = $masterGoal->instance; $requestPrivate = $data['is_private']; // get an array with this master goal and all his children $masterGoals = $instance->master_goals()->whereNotNull('master_goal_id')->get()->prepend($masterGoal); $masterGoalAndChildrenIds = $this->recursiveMasterGoalForIds($masterGoals, $masterGoal->id, $masterGoal->id); $managerIds = $masterGoal->users()->pluck('user_id')->toArray(); if(isset($data['manager_users']) && $data['manager_users']){ // set new managers for this master goal $masterGoal->users()->sync($data['manager_users']); setAccessManagerToMasterGoal($data['manager_users'], $masterGoalAndChildrenIds); // remove old managers for this master goal $oldManagers = array_diff($managerIds, $data['manager_users']); if($oldManagers){ removeAccessManagerFromMasterGoal($oldManagers, $masterGoalAndChildrenIds); } }else{ if($managerIds){ $masterGoal->users()->sync([]); removeAccessManagerFromMasterGoal($managerIds, $masterGoalAndChildrenIds); } } // check if this master goal is assigend to another master goal that has manager users if(isset($data['master_goal_id']) ){ // if the parent of this master goal changed, set from all manager users, this master goal and his children if($data['master_goal_id'] != $masterGoal->master_goal_id){ setAccessManagerToMasterGoalParent($instance, $data['master_goal_id'], $masterGoalAndChildrenIds); if($masterGoal->master_goal_id){ removeAccessManagerToMasterGoalParent($instance, $masterGoal->master_goal_id, $masterGoalAndChildrenIds); } } }else{ // if there was before assigned to a parent master goal, remove from all manager users, this master goal and his children if($masterGoal->master_goal_id){ removeAccessManagerToMasterGoalParent($instance, $masterGoal->master_goal_id, $masterGoalAndChildrenIds); } } // update all this master goal children to private $instance->master_goals->whereIn('id', $masterGoalAndChildrenIds)->each(function ($masterGoal) use ($requestPrivate) { $masterGoal->update(['is_private' => $requestPrivate]); }); // update all this master goal and his children objectives to private $instance->objectives->whereIn('master_goal_id', $masterGoalAndChildrenIds)->each(function ($objective) use ($requestPrivate) { $objective->update(['is_private' => $requestPrivate]); }); $instance->users->each(function ($user) use ($masterGoalAndChildrenIds, $instance, $requestPrivate) { $managerMasterGoalIds = explode(',', $user->manager_master_goal_ids); $privateMasterGoalIds = array_filter(explode(',', $user->private_master_goal_ids)); $privateObjectiveIds = array_filter(explode(',', $user->private_objective_ids)); // set/remove private_master_goal_ids for all users if($requestPrivate){ $newPrivateMasterGoalIds = array_diff($masterGoalAndChildrenIds, array_filter($managerMasterGoalIds)); $doubleCheckPrivateMasterGoalIds = array_diff($privateMasterGoalIds, array_filter($managerMasterGoalIds)); $privateMasterGoalIds = array_unique(array_merge($newPrivateMasterGoalIds, $doubleCheckPrivateMasterGoalIds)); }else{ $newPrivateMasterGoalIds = array_diff($privateMasterGoalIds, $masterGoalAndChildrenIds); $privateMasterGoalIds = $newPrivateMasterGoalIds; } if($newPrivateMasterGoalIds){ $user->update(['private_master_goal_ids' => $privateMasterGoalIds ? implode(',', $privateMasterGoalIds) : null]); } // set/remove all private objectives ids, without those where he is responsible for key results or objectives or is manager of that objective if($requestPrivate){ $newPrivateObjectiveIds = $instance->objectives()->where([ ['is_private', 1], ['user_id', '!=', $user->id] ])->whereNotIn('master_goal_id', $managerMasterGoalIds)->get()->map(function ($objective) use ($user) { if(!in_array($user->id, $objective->key_results->pluck('user_id')->toArray())){ return $objective->id; } })->filter()->toArray(); }else{ $publicObjectiveIds = $instance->objectives->whereIn('master_goal_id', $masterGoalAndChildrenIds)->pluck('id')->toArray(); $newPrivateObjectiveIds = array_diff($publicObjectiveIds, $privateObjectiveIds); } $user->update(['private_objective_ids' => $newPrivateObjectiveIds ? implode(',', $newPrivateObjectiveIds) : null]); }); $masterGoal->update($data); // must be removed after we find the missing scenario updateUserPrivateObjectiveIds($instance); return response()->json([ 'status' => 'success', 'data' => [ 'id' => $masterGoal->id, 'slug' => $masterGoal->slug, ], ], 200); } public function changeStatus(MasterGoalChangeStatusFormRequest $request, MasterGoal $masterGoal) { $data = $request->validated(); $masterGoal->update($data); return response()->json([ 'status' => 'success', 'data' => [ 'id' => $masterGoal->id, 'slug' => $masterGoal->slug, ], ], 200); } public function changeShareStatus(MasterGoalChangeShareStatusFormRequest $request, MasterGoal $masterGoal) { $data = $request->validated(); $masterGoal->update($data); return response()->json([ 'status' => 'success', 'data' => [ 'id' => $masterGoal->id, 'slug' => $masterGoal->slug, ], ], 200); } public function destroy(MasterGoal $masterGoal) { $masterGoal->delete(); return response()->json([ 'status' => 'success' ], 200); } public function showAll(Request $request){ $authUser = Auth::user(); $rootMasterGoals = $authUser->instance->master_goals()->whereNull('master_goal_id')->get(); $masterGoalsWithParent = $authUser->instance->master_goals()->whereNotNull('master_goal_id')->get(); // check if the call is for edit master goal and remove his children in order to avoid bug $editParentId = $request->parent_id; foreach($rootMasterGoals as $rootMasterGoal) { // check if this auth user can see a private master goals $noPermisionToSee = $authUser && !$authUser->isAdmin() && isPrivateMasterGoal($rootMasterGoal->id, $authUser); $this->masterGoalsTree[] = [ 'id' => $rootMasterGoal->id, 'name' => $noPermisionToSee ? null : $rootMasterGoal->name, 'slug' => $noPermisionToSee ? null : $rootMasterGoal->slug, 'description' => $noPermisionToSee ? null : $rootMasterGoal->description, 'related_to' => null, 'level_depth' => 0, 'disable_for_manager' => !checkManagerAccess($rootMasterGoal->id, $authUser), ]; if($rootMasterGoal->id != $editParentId){ $children = $this->recursiveMasterGoalForDropdown($masterGoalsWithParent, $rootMasterGoal->id, $rootMasterGoal->id, 1, $authUser); } } return response()->json([ 'status' => 'success', 'data' => $this->masterGoalsTree, ], 200); } public function statusList() { return response()->json([ 'status' => 'success', 'data' => MasterGoal::getStatusList(), ], 200); } public function recursiveMasterGoal($masterGoals, $parentId, $rootId, $authUser) { $data = []; foreach ($masterGoals as $masterGoal) { if ($masterGoal->master_goal_id == $parentId || ($masterGoal->id == $rootId)) { $rootId = $masterGoal->id == $rootId ? 'passedRoot' : $rootId; // check if this auth user can see a private master goals $noPermisionToSee = $authUser && !$authUser->isAdmin() && isPrivateMasterGoal($masterGoal->id, $authUser); // for the left side of the page request $this->allVisions[$masterGoal->id] = [ 'id' => $masterGoal->id, 'name' => $noPermisionToSee ? null : $masterGoal->name, 'percentage_finished' => 0, ]; $element = [ 'id' => $masterGoal->id, 'name' => $noPermisionToSee ? null : $masterGoal->name, 'slug' => $noPermisionToSee ? null : $masterGoal->slug, 'description' => $noPermisionToSee ? null : $masterGoal->description, 'master_goal_id' => $masterGoal->master_goal_id, 'type' => 'master-goal', 'goal_count' => 0, 'has_one_overdue' => false, 'remaining_days' => 0, 'percentage_finished' => 0, 'rights' => [ 'create' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $masterGoal]) : false, 'edit' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $masterGoal]) : false, 'delete' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $masterGoal]) : false, 'create_objective' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $masterGoal]) : false, ], ]; // check for the children of this master goal $children = $this->recursiveMasterGoal($masterGoals, $masterGoal->id, $rootId, $authUser); if ($children) { $element['children'] = $children; // count all master goals and goals $goalCountArr = Arr::pluck($children, 'goal_count'); $element['goal_count'] = array_sum($goalCountArr) + count($goalCountArr); // has one overdue $hasOverdue = Arr::first($children, function ($child) { return $child['has_one_overdue'] ? true : false; }); if($hasOverdue){ $element['has_one_overdue'] = true; } // longest remaining days $sortRemainingDays = array_filter(Arr::pluck($children, 'remaining_days')); rsort($sortRemainingDays); $element['remaining_days'] = $sortRemainingDays ? reset($sortRemainingDays) : 0; // master goals percentage array $percentageArr = array_filter(Arr::pluck($children, 'percentage_finished'), function ($child) { return $child != null; }); // master goals percentage of children $percentageMasterGoalChildren = count($percentageArr) > 0 ? array_sum($percentageArr) / count($percentageArr) : 0; // percentage of goals of this master goal $objectivePercent = $masterGoal->objectives->sum('percent'); $objectivesCount = $masterGoal->objectives->count(); // master goal percentage finished $element['percentage_finished'] = $objectivesCount > 0 ? ($percentageMasterGoalChildren + $objectivePercent) / ($objectivesCount + 1) : $percentageMasterGoalChildren; // for the left side of the page request $this->allVisions[$masterGoal->id]['percentage_finished'] = $element['percentage_finished']; }else{ // percentage of goals of this master goal for last child $objectivePercent = $masterGoal->objectives->sum('percent'); $objectivesCount = $masterGoal->objectives->count(); $element['percentage_finished'] = $objectivesCount > 0 ? ($objectivePercent / $objectivesCount) : 0; // for the left side of the page request $this->allVisions[$masterGoal->id]['percentage_finished'] = $element['percentage_finished']; } // check if there are any objectives to this master goal foreach($masterGoal->objectives as $objective){ // check if this auth user can see a private objective $noPermisionToSee = $authUser && !$authUser->isAdmin() && isPrivateObjective($objective->id, $authUser); // check for objective end date and difference in days until the end of objective if($objective->end_date >= Carbon::today()->toDateString()){ // difference in days until the end of objective $diffInDays = Carbon::today()->diffInDays($objective->end_date); if($diffInDays > $element['remaining_days']){ $element['remaining_days'] = $diffInDays; } } // check for an overdue objective if($objective->status == Objective::STATUS_OVERDUE){ $element['has_one_overdue'] = true; } // count all master goals and goals $element['goal_count']++; // for the left side of the page request $this->allObjectives[$objective->id] = [ 'id' => $objective->id, 'name' => $noPermisionToSee ? null : $objective->name, 'percent' => $objective->percent, 'status' => $objective->status, ]; // get trashed users also $objectiveUser = $objective->user ? $objective->user : User::withTrashed()->find($objective->user_id); // let the man see public objective if($authUser){ $canViewObjective = !$authUser->isAdmin() && isPrivateObjective($objective->id, $authUser) ? false : true; }else{ $canViewObjective = false; } $objElement = [ 'user' => [ 'id' => $objectiveUser->id, 'name' => $objectiveUser->getFullName(), 'avatar' => $objectiveUser->avatar, ], 'id' => $objective->id, 'name' => $noPermisionToSee ? null : $objective->name, 'slug' => $noPermisionToSee ? null : $objective->slug, 'master_goal_id' => $objective->master_goal_id, 'description' => $noPermisionToSee ? null : $objective->description, 'type' => 'goal', 'start_date' => $objective->start_date, 'end_date' => $objective->end_date, 'percent' => $objective->percent, 'status' => $objective->status, 'total_key_results' => $objective->key_results->count(), 'key_results' => $objective->key_results->map(function ($keyResult) use ($noPermisionToSee, $authUser) { // for the left side of the page request $this->allKeyResults[$keyResult->id] = [ 'id' => $keyResult->id, 'name' => $noPermisionToSee ? null : $keyResult->name, 'percent' => $keyResult->percent, 'status' => $keyResult->status, ]; // get trashed users also $keyResultUser = $keyResult->user ? $keyResult->user : User::withTrashed()->find($keyResult->user_id); $lastKeyResultLog = $keyResult->lastKeyResultLog(); return [ 'id' => $keyResult->id, 'user' => [ 'id' => $keyResultUser->id, 'name' => $keyResultUser->getFullName(), 'role' => $keyResultUser->role->name, 'avatar' => $keyResultUser->avatar, ], 'name' => $noPermisionToSee ? null : $keyResult->name, 'description' => $noPermisionToSee ? null : $keyResult->description, 'promises_count' => $keyResult->promises->where('is_set', 1)->count(), 'tasks_count' => $keyResult->tasks->count(), 'key_results_logs_count' => $keyResult->key_result_logs->count(), 'percentage_finished' => $keyResult->percent, 'needs_an_update' => $keyResult->checkIfNeedsUpdate(), 'start_date' => $keyResult->start_date, 'end_date' => $keyResult->end_date, "tasks_completed" => $keyResult->tasks->where('done', 1)->count(), "need_evaluate_promise" => $authUser ? ($keyResult->checkIfNeedToEvaluatePromise() ? true : false) : false, "progress_type" => $keyResult->progress_type, "target" => $keyResult->target, "unity" => $keyResult->unity, "start_value" => $keyResult->start_value, "uuid" => $keyResult->uuid, 'current_value' => $lastKeyResultLog ? $lastKeyResultLog->value : $keyResult->start_value, 'status' => $keyResult->status, ]; })->toArray(), 'people' => $objective->key_results->groupBy('user_id')->map(function ($keyResults, $userId) use ($objective) { // get trashed users also $user = User::withTrashed()->find($userId); return [ 'avatar' => $user->avatar, 'full_name' => $user->getFullName(), 'role' => $user->role->name, 'key_results_for_this_objective' => sizeof($keyResults), ]; })->toArray(), 'rights' => [ 'view' => $canViewObjective, 'edit' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $objective]) : false, 'delete' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $objective]) : false, 'create_key_result' => $authUser ? $authUser->can('crudActionAndEntityInstance', [User::class, $objective]) : false, ], ]; if(isset($element['children'])){ array_push($element['children'], $objElement); }else{ $element['children'] = [$objElement]; } } $data[] = $element; } } return $data; } public function recursiveMasterGoalForDropdown($masterGoals, $parentId, $rootId, $levelDepth, $user) { foreach ($masterGoals as $masterGoal) { if ($masterGoal->master_goal_id == $parentId || ($masterGoal->id == $rootId)) { $rootId = $masterGoal->id == $rootId ? 'passedRoot' : $rootId; // check if this auth user can see a private master goals $noPermisionToSee = $user && !$user->isAdmin() && isPrivateMasterGoal($masterGoal->id, $user); $this->masterGoalsTree[] = [ 'id' => $masterGoal->id, 'name' => $noPermisionToSee ? null : $masterGoal->name, 'slug' => $noPermisionToSee ? null : $masterGoal->slug, 'description' => $noPermisionToSee ? null : $masterGoal->description, 'related_to' => $masterGoal->master_goal_id, 'level_depth' => $levelDepth, 'disable_for_manager' => !checkManagerAccess($masterGoal->id, $user), ]; // check for the children of this master goal $children = $this->recursiveMasterGoalForDropdown($masterGoals, $masterGoal->id, $rootId, $levelDepth+1, $user); } } return true; } public function recursiveMasterGoalForIds($masterGoals, $parentId, $rootId) { foreach ($masterGoals as $masterGoal) { if ($masterGoal->master_goal_id == $parentId || ($masterGoal->id == $rootId)) { $rootId = $masterGoal->id == $rootId ? 'passedRoot' : $rootId; $this->masterGoalChildrenIds[] = $masterGoal->id; // check for the children of this master goal $children = $this->recursiveMasterGoalForIds($masterGoals, $masterGoal->id, $rootId); } } return array_unique($this->masterGoalChildrenIds); } }
Back