/**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function show($code)
 {
     if (Gate::denies('show-document')) {
         abort(403);
     }
     $document = Document::getByPublicCode($code);
     return $this->edit($document);
 }
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     view()->composer('frontend.layout.header', function ($view) {
         $view->with('user', Auth::user());
     });
     view()->composer('frontend.partials.domainsTree', function ($view) {
         $domains = Domain::getCurrentDomains();
         $tree = Domain::getTree(Domain::getDomainsForTree($domains));
         $view->with(['tree' => $tree, 'domains' => $tree[1]['subdomains']]);
     });
     view()->composer('frontend.partials.domainsTreeForReports', function ($view) {
         $domains = Domain::getCurrentDomains();
         $tree = Domain::getTree(Domain::getDomainsForTree($domains));
         $view->with(['tree' => $tree, 'domains' => $tree[1]['subdomains']]);
     });
     Document::deleted(function ($document) {
         $fileToDelete = UploadedFile::where('id', '=', $document['uploaded_file_id'])->get();
         $fileToDelete = $fileToDelete[0]['file_name'];
         File::delete(storage_path() . '/documents/' . $fileToDelete);
         $document->file()->delete();
         return true;
     });
 }
 public function queryDocument(Request $request)
 {
     $queryDocumentName = $request->input('name');
     $documentIds = DocumentTranslation::where('title', 'like', '%' . $queryDocumentName . '%')->where('locale', \App::getLocale())->lists('document_id');
     $documents = Document::whereIn('id', $documentIds)->get();
     $result = [];
     foreach ($documents as $document) {
         $result[] = ['id' => $document->id, 'title' => $document->title, 'file' => $document->file->original_file_name, 'data' => $document->init_at ? $document->init_at->format('d-m-Y') : '-', 'file_name' => $document->file->file_name];
     }
     return $result;
 }
 protected function importRemainingDocuments()
 {
     $initDocuments = Document::count();
     $lostDocuments = [];
     $documentsForExtendedIssues = [];
     $pathToFiles = sprintf('%s/var/www/andr_v2/uploads/reldocs/propid_/stepid_', storage_path());
     $dbPathToFile = '/var/www/andr_v2/uploads/reldocs/propid_/stepid_/';
     $oldDocuments = DB::connection('oldissue')->select('select * from relateddoc
         where propid > 120 and stepid <> 0
     ');
     $getFileNamesFromFolder = array_diff(scandir($pathToFiles), ['.', '..']);
     foreach ($oldDocuments as $document) {
         if (!$document->filepaths) {
             continue;
         }
         if (strpos($document->filepaths, $dbPathToFile) === 0) {
             $lostDocuments[] = $document;
         }
     }
     foreach ($lostDocuments as $document) {
         $fullPathToFile = $document->filepaths;
         $file = str_replace($dbPathToFile, '', $fullPathToFile);
         if (!in_array($file, $getFileNamesFromFolder)) {
             continue;
         }
         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(storage_path() . $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");
         }
     }
     echo sprintf("Au fost recuperate %s documente salvate la comun intr-un folder.\n", $count = Document::count() - $initDocuments);
     return true;
 }