$hash, 'mappings' => [], ]; /** @var UploadedFile $file */ foreach ( $request->files->get( 'files' ) as $entityType => $file ) { $contents = file_get_contents( $file->getPathname() ); // $contents = mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8'); // Store the csv in cache with an expiry of 10 minutes Cache::put( $hash . '-' . $entityType, base64_encode( $contents ), 600 ); // Parse CSV $csv_array = $this->getCsvData( $contents ); $class_map = $this->getEntityMap( $entityType ); $data['mappings'][ $entityType ] = [ 'available' => $class_map::importable(), 'headers' => array_slice( $csv_array, 0, 2 ), ]; } return response()->json( $data ); } public function import( ImportRequest $request ) { $data = $request->all(); if ( empty( $data['hash'] ) ) { // Create a reference $data['hash'] = $hash = Str::random( 32 ); /** @var UploadedFile $file */ foreach ( $request->files->get( 'files' ) as $entityType => $file ) { $contents = file_get_contents( $file->getPathname() ); // Store the csv in cache with an expiry of 10 minutes Cache::put( $hash . '-' . $entityType, base64_encode( $contents ), 600 ); } } CSVImport::dispatch( $data, auth()->user()->company() ); return response()->json( [ 'message' => ctrans( 'texts.import_started' ) ], 200 ); } private function getEntityMap( $entity_type ) { return sprintf( 'App\\Import\\Definitions\%sMap', ucfirst( $entity_type ) ); } private function getCsvData( $csvfile ) { if ( ! ini_get( 'auto_detect_line_endings' ) ) { ini_set( 'auto_detect_line_endings', '1' ); } $csv = Reader::createFromString($csvfile); $stmt = new Statement(); $data = iterator_to_array($stmt->process($csv)); if (count($data) > 0) { $headers = $data[0]; // Remove Invoice Ninja headers if (count($headers) && count($data) > 4) { $firstCell = $headers[0]; if (strstr($firstCell, (string)config('ninja.app_name'))) { array_shift( $data ); // Invoice Ninja... array_shift( $data ); // array_shift( $data ); // Entity Type Header } } } return $data; } }