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 :
ImportDataController.php
| Size :
8.88
KB
Copy
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Imports\DemoDataImport; use App\Models\MasterGoal; use App\Models\Objective; use App\Models\KeyResult; use App\Models\User; use App\Models\Promise; use App\Models\Task; use App\Models\KeyResultLog; use App\Models\Tag; use Carbon\Carbon; use Str; use Hash; use Excel; use Webpatser\Uuid\Uuid; class ImportDataController extends Controller { public function importDataXls(Request $request) { if(env('IMPORT_TOKEN') == $request->import_token){ $rows = Excel::toArray(new DemoDataImport, $request->file); foreach ($rows[0] as $key => $row) { $this->importRow($row); } return response()->json([ 'status' => 'success' ], 200); } return response()->json([ 'status' => 'not allowed', ], 403); } public function importRow($row){ if(isset($row['user_id'])){ User::updateOrInsert( ['id' => $row['user_id']], [ 'first_name' => $row['first_name'], 'avatar' => $row['avatar'], 'last_name' => $row['last_name'], 'email' => $row['email'], 'slug' => Str::slug($row['first_name'].' '.$row['last_name'], '-'), 'working_days' => $row['working_days'], 'promise_time' => $row['promise_time'] . ":00", 'role_id' => $row['role_id'], 'instance_id' => 1, 'language' => 'ro', 'password' => Hash::make('123456'), 'created_at' => Carbon::now()->addDay(-40), ]); } if(isset($row['master_goal_id'])){ MasterGoal::updateOrInsert( ['id' => $row['master_goal_id']], [ 'name' => $row['master_goal'], 'slug' => Str::slug($row['master_goal'], '-'), 'instance_id' => 1, 'description' => $row['master_goal_description'], 'master_goal_id' => $row['master_goal_parent_id'], 'uuid' => (string) Uuid::generate(4) ]); } if(isset($row['objective_id'])){ Objective::updateOrInsert( ['id' => $row['objective_id']], [ 'name' => $row['objective'], 'slug' => Str::slug($row['objective'], '-'), 'instance_id' => 1, 'user_id' => $row['user_id'], 'description' => $row['objective_description'], 'start_date' => $this->transformDate($row['objective_start_date']), 'end_date' => $this->transformDate($row['objective_end_date']), 'master_goal_id' => $row['master_goal_id'], ]); $objective = Objective::findOrFail($row['objective_id']); $objective->update(['status' => setStatus($objective)]); } if(isset($row['key_result_id'])){ KeyResult::updateOrInsert( ['id' => $row['key_result_id']], [ 'name' => $row['key_result'], 'instance_id' => 1, 'user_id' => $row['user_id'], 'description' => $row['key_result_description'], 'start_date' => $this->transformDate($row['key_result_start_date']), 'end_date' => $this->transformDate($row['key_result_end_date']), 'progress_type' => $row['progress_type'], 'objective_id' => $row['objective_id'], 'start_value' => $row['start_value'], 'target' => $row['target'], 'unity' => $row['unity'], 'measurement' => $row['measurement'], 'uuid' => (string) Uuid::generate(4) ]); $keyResult = KeyResult::findOrFail($row['key_result_id']); $keyResult->update(['status' => setStatus($keyResult)]); } if(isset($row['promises'])){ $json = str_replace("\n", '', $row['promises']); $json = str_replace("\t", '', $json); $jsonDecode = json_decode($json, true); foreach ($jsonDecode as $promise) { Promise::updateOrInsert( ['id' => $promise['id']], [ 'name' => $promise['name'], 'key_result_id' => $row['key_result_id'], 'user_id' => $row['user_id'], 'instance_id' => 1, 'why_not' => $promise['why_not'], 'mood' => $promise['mood'], 'is_set' => $promise['is_set'], 'done_on_time' => $promise['done_on_time'], 'resolved_at' => $this->transformDate($promise['resolved_at']), 'created_at' => $this->transformDate($promise['created_at']), ]); } } if(isset($row['tasks'])){ $json = str_replace("\n", '', $row['tasks']); $json = str_replace("\t", '', $json); $jsonDecode = json_decode($json, true); foreach ($jsonDecode as $task) { Task::updateOrInsert( ['id' => $task['id']], [ 'name' => $task['name'], 'key_result_id' => $row['key_result_id'], 'user_id' => $row['user_id'], 'instance_id' => 1, 'done' => $task['done'], 'created_at' => $this->transformDate($task['created_at']), ]); } } if(isset($row['key_result_logs'])){ $json = str_replace("\n", '', $row['key_result_logs']); $json = str_replace("\t", '', $json); $jsonDecode = json_decode($json, true); foreach ($jsonDecode as $keyResultLog) { $calculateKeyResultPercentage = calculateKeyResultPercentage($keyResultLog['value'], $keyResult); // must be interogated before and with closure for 'diff_last_key_result_percent' $lastKeyResultPercent = $keyResult->percent; // must be interogated before and with closure for 'diff_last_value' $lastKeyResultLog = $keyResult->lastKeyResultLog(); $lastKeyResultLogValue = $lastKeyResultLog ? $lastKeyResultLog->value : 0; KeyResultLog::updateOrInsert( ['id' => $keyResultLog['id']], [ 'key_result_id' => $row['key_result_id'], 'description' => $keyResultLog['description'], 'status' => $keyResultLog['status'], 'value' => $keyResultLog['value'], 'user_id' => $row['user_id'], 'instance_id' => 1, 'updated_on_date' => $keyResultLog['updated_on_date'], 'key_result_percent' => $calculateKeyResultPercentage, 'objective_percent' => calculateObjectivePercentage($keyResult->objective), 'diff_last_key_result_percent' => $calculateKeyResultPercentage - $lastKeyResultPercent, 'diff_last_value' => $keyResultLog['value'] - $lastKeyResultLogValue, 'created_at' => $this->transformDate($keyResultLog['created_at']), ]); } } // if(isset($row['tags'])){ // $json = str_replace("\n", '', $row['tags']); // $json = str_replace("\t", '', $json); // $jsonDecode = json_decode($json, true); // foreach ($jsonDecode as $tag) { // Tag::updateOrInsert( // ['id' => $tag['id']], // [ // 'name' => $tag['name'], // 'color_id' => $tag['color_id'], // 'instance_id' => 1, // ]); // } // } // if(isset($row['tag_ids'])){ // $user = User::findOrFail($row['user_id']); // $user->tags()->sync(explode(',', $row['tag_ids'])); // } } // Transform a date value into a Carbon object. public function transformDate($value, $format = 'Y-m-d'){ try { return Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value)); } catch (\ErrorException $e) { return Carbon::createFromFormat($format, $value); } } public function okrsList() { return response()->json([ 'status' => 'success', 'data' => collect(config('okrs'))->groupBy('category')->toArray(), ], 200); } }
Back