Exemplo n.º 1
0
 /**
  * Update the specified resource in storage.
  *
  * @return \Illuminate\Http\Response
  */
 public function update(SiteDocRequest $request, $id)
 {
     $site_id = $request->get('site_id');
     $type = $request->get('type');
     // Redirect on 'back' button
     if ($request->has('back')) {
         return view('/site/doc/list', compact('site_id', 'type'));
     }
     $doc = SiteDoc::findOrFail($id);
     // Check authorisation and throw 404 if not
     if (!Auth::user()->allowedTo('edit', 'site.doc', $doc)) {
         return view('errors/404');
     }
     // Get Original report filename path
     $orig_site = $doc->site_id;
     $orig_type = $doc->type;
     $orig_reportfile = $doc->reportUrl;
     //dd($request->all());
     $doc_request = $request->only('name', 'type', 'site_id', 'notes');
     $doc->update($doc_request);
     // if doc has altered 'site_id' or 'type' move the file to the new file location
     if ($doc->type != $orig_type || $doc->site_id != $orig_site) {
         // Make directory if non-existant
         if (!file_exists(public_path(pathinfo($doc->reportUrl, PATHINFO_DIRNAME)))) {
             mkdir(public_path(pathinfo($doc->reportUrl, PATHINFO_DIRNAME), 0755));
         }
         rename(public_path($orig_reportfile), public_path($doc->reportUrl));
         $orig_reportfile = $doc->reportUrl;
     }
     if ($doc->type == 'RISK') {
         $type = 'risk';
     }
     if ($doc->type == 'HAZ') {
         $type = 'hazard';
     }
     if ($doc->type == 'PLAN') {
         $type = 'plan';
     }
     // Handle attached file
     if ($request->hasFile('uploadfile')) {
         $file = $request->file('uploadfile');
         $path = "filebank/site/" . $doc->site_id . '/' . $type;
         $name = sanitizeFilename(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . strtolower($file->getClientOriginalExtension());
         // Ensure filename is unique by adding counter to similiar filenames
         $count = 1;
         while (file_exists(public_path("{$path}/{$name}"))) {
             $name = $doc->site_id . '-' . sanitizeFilename(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) . '-' . $count++ . '.' . strtolower($file->getClientOriginalExtension());
         }
         $file->move($path, $name);
         $doc->reportfile = $name;
         $doc->save();
         // Delete previous file
         if (file_exists(public_path($orig_reportfile))) {
             unlink(public_path($orig_reportfile));
         }
     }
     Toastr::success("Updated document");
     return view('site/doc/edit', compact('doc', 'site_id', 'type'));
 }