protected function importDocuments() { $allFiles = []; $pathToFiles = sprintf('%s/var/www/andr_v2/uploads/reldocs', storage_path()); $oldDocuments = DB::connection('oldissue')->select('select * from relateddoc where propid > 120 and stepid <> 0 '); foreach ($oldDocuments as $document) { if (!$document->filepaths) { continue; } if (!Issue::find($document->propid)) { continue; } $documentPath = sprintf('%s/propid_%s/stepid_%s/', $pathToFiles, $document->propid, $document->stepid); try { $getFileNamesFromFolder = array_diff(scandir($documentPath), ['.', '..']); if (!$getFileNamesFromFolder) { continue; } foreach ($getFileNamesFromFolder as $file) { if (!$file) { continue; } if (in_array($file, $allFiles)) { continue; } $allFiles[] = $file; $fullPathToFile = sprintf('%s%s', $documentPath, $file); do { $randomName = str_random(40); } while (UploadedFile::where('file_name', $randomName)->count() > 0); do { $codPublic = str_random(40); } while (Document::where('public_code', $codPublic)->count() > 0); $uploadedFileData = ['file_name' => $randomName, 'folder' => '/documents/', 'original_file_name' => $file]; $this->moveFile($fullPathToFile, $uploadedFileData['file_name']); $doc = factory(Document::class)->create(['public' => 1, 'uploaded_file_id' => factory(UploadedFile::class)->create($uploadedFileData)->id, 'public_code' => $codPublic, 'init_at' => $document->initat]); $translatableData = ['ro' => ['title' => $document->content ? $document->content : ''], 'en' => ['title' => $document->encontent ? $document->encontent : '']]; $doc->fill($translatableData); $doc->save(); try { $doc->steps()->attach($document->stepid); } catch (\Exception $e) { print_r("Shiit! O relatie Document - FlowStep nu s-a putut importa.\n"); } } } catch (\Exception $e) { print_r("Shiit! Un folder nu exista.\n"); } } echo sprintf("Au fost importate %s documente.\n", Document::count()); return true; }