argument('file'); $file = storage_path($file_name); $data = $this->mapData($file); $created_data = []; $non_created_data = []; foreach($data as $single){ $user = $this->importSingle($single); if($user){ $created_data[] = $user; }else{ $non_created_data[] = $single; } }; // dd($created_data); $table_headers = []; foreach($created_data[0] as $key => $val ){ $table_headers[] = $key; } $this->table( $table_headers, $created_data ); $non_table_headers = []; foreach($non_created_data[0] as $key => $val ){ $non_table_headers[] = $key; } $this->table( $non_table_headers, $non_created_data ); // Specify the storage path and filename $exported_file_name = $file_name.'-'.rand(100,999).'.csv'; // Store the Excel file array_unshift($created_data,$table_headers); Excel::store(new CreatedUsersExport($created_data), $exported_file_name); $this->info("File saved to storage/app/".$exported_file_name); return 0; } protected function mapData($file){ $data = Excel::toArray(true, $file); $heading = $data[0][0]; unset($data[0][0]); $mapped_data = []; $i = 0; foreach($data[0] as $wrapper){ foreach($wrapper as $key => $element){ $mapped_data[$i][$heading[$key]] = $element; } $i++; } return $mapped_data; } protected function importSingle($single_array = []){ if(!empty($single_array['extension']) && ($single_array['extension'] > 100)){ if(empty(trim($single_array['user'])) && !empty($single_array['queue'])){ $single_array['user'] = $single_array['queue'].'-'.$single_array['extension']; } if(empty($single_array['email'])){ $email_first_part = $single_array['user'] ?? $single_array['queue']; $email_first_part = trim($email_first_part); $email_first_part = Str::slug($email_first_part); $single_array['email'] = $email_first_part.'-'.$single_array['extension'].'@egytelecoms.com'; } $group = $this->getOrCreateGroup($single_array['queue']); $name_exploded = explode(' ',trim($single_array['user'])); $firstname = $name_exploded[0]; $lastname = end($name_exploded); $userdata = [ 'id' => $single_array['extension'], 'email' => $single_array['email'], 'is_active' => 1, 'first_name' => $firstname, 'last_name' => $lastname, 'name' => $single_array['user'], 'access_to_all_entity' => 0, 'theme' => 'theme-black', 'created_at' => \Carbon\Carbon::now(), 'updated_at' => \Carbon\Carbon::now(), 'password' => \Illuminate\Support\Facades\Hash::make($this->default_password), 'company_id' => 1, ]; $the_password = $this->default_password; if(!empty($single_array['password'])){ $the_password = $single_array['password']; $userdata['password'] = \Illuminate\Support\Facades\Hash::make($the_password); } // return $userdata; \DB::table('users')->insertOrIgnore($userdata); User::find($single_array['extension'])->syncRoles(2); $created_user = User::find($single_array['extension']); $user_service = new UserService; $user_service->addAsteriskUser($created_user,$the_password); $userdata['password'] = $the_password; $userdata['queue'] = $group->name; $userdata['queue_id'] = $group->id; $userdata['created_at'] = date('Y-m-d'); $userdata['updated_at'] = date('Y-m-d'); return $userdata; } return false; } protected function getOrCreateGroup($group){ if(!empty($group)){ $count = \DB::table('groups')->where('name',$group)->count(); if($count >= 1){ return \DB::table('groups')->where('name',$group)->first(); }else{ $group_id = \DB::table('groups')->orderBy('id','DESC')->first()->id; $group_id++; $group_insertion_array = [ 'id' => $group_id, 'name' => $group, 'company_id' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now() ]; \DB::table('groups')->insert($group_insertion_array); $GroupObserver = new GroupObserver(); $GroupObserver->createTheGroup([],(object)$group_insertion_array); return \DB::table('groups')->where('name',$group)->first(); } }else{ return \DB::table('groups')->orderBy('id','ASC')->first(); } } }