Changeset ea7b12a for app/Http


Ignore:
Timestamp:
10/19/21 17:46:21 (3 years ago)
Author:
beratkjufliju <kufliju@…>
Branches:
develop, master
Children:
6b95845
Parents:
b9c4a92
Message:

added files crud in table

Location:
app/Http
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • app/Http/Controllers/Dashboard/DocumentsController.php

    rb9c4a92 rea7b12a  
    55use App\Helpers\Alert;
    66use App\Http\Requests\Dashboard\DocumentRequest;
     7use App\Http\Requests\Dashboard\FileRequest;
     8use App\Http\Requests\Dashboard\PasswordSettingsRequest;
    79use App\Models\Department;
    810use App\Models\Document;
    911use App\Models\File;
    1012use App\Services\UploadService;
     13use Carbon\Carbon;
    1114use Illuminate\Http\Request;
     15use Illuminate\Http\Response;
    1216use Illuminate\Support\Facades\Storage;
    1317use App\Http\Controllers\Controller;
     
    115119        $document->description = $request->description;
    116120
    117         $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
     121        $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
    118122
    119123        if (!Storage::disk('local')->has($location)) {
     
    121125        }
    122126
    123         foreach ($request->file_item as $file) {
    124             $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
    125             $newFile = new File();
    126             $newFile->link = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
    127             $newFile->document()->associate($document);
    128             $newFile->save();
    129         }
    130 
    131127        $document->save();
     128
     129        if ($request->has('file_item')) {
     130            foreach ($request->file_item as $file) {
     131                $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
     132                $newFile = new File();
     133                $newFile->name = $file->getClientOriginalName();
     134                $newFile->location = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
     135                $newFile->document()->associate($document);
     136                $newFile->save();
     137            }
     138        }
    132139
    133140        Alert::flash("New document created successfully");
     
    140147        return view("dashboard.documents.edit")->with([
    141148            "document" => Document::findOrFail($id),
    142             "departments" => Department::all()
     149            "departments" => Department::all(),
     150            "files" => File::where('document_id', $id)->get()
    143151        ]);
    144152    }
     
    147155    {
    148156        $document = Document::findOrFail($id);
     157        $files = File::where('document_id', $id)->get();
    149158
    150159        $department = Department::find($request->department);
    151160
    152161        $document->department()->associate($department);
     162
     163        $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
    153164
    154165        $document->name = $request->name;
     
    156167        $document->description = $request->description;
    157168
    158         $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $document->name;
    159         $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
     169        $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
    160170
    161171        if ($document->isDirty('name')) {
    162172            if (!Storage::disk('local')->has($location)) {
    163173                Storage::disk('local')->move($oldLocation, $location);
    164             }
    165         }
    166 
    167         $hasFileError = false;
     174                foreach($files as $file){
     175                    $file->location = "test";
     176                }
     177            }
     178        }
     179
    168180        if ($request->has('file_item')) {
    169181            foreach ($request->file_item as $file) {
    170182                $fileName = $file->getClientOriginalName();
    171                 if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
    172                     $hasFileError = true;
    173                     break;
     183                if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
     184                    // $hasFileError = true;
     185                    // break;
     186                    $NewFileName = time() . $fileName;
     187                    $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName);
     188                    $newFile = new File();
     189                    $newFile->name = rand() . $fileName;
     190                    $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName;
     191                    $newFile->document()->associate($document);
     192                    $newFile->save();
     193                } else {
     194                    $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
     195                    $newFile = new File();
     196                    $newFile->name = $fileName;
     197                    $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
     198                    $newFile->document()->associate($document);
     199                    $newFile->save();
    174200                }
    175                 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
    176                 $newFile = new File();
    177                 $newFile->link = $location . DIRECTORY_SEPARATOR . $fileName;
    178                 $newFile->document()->associate($document);
    179                 $newFile->save();
    180             }
    181         }
    182 
    183         if($hasFileError) {
    184             Alert::flash('Document with the same name exists', 'error');
    185             return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
     201            }
    186202        }
    187203
    188204        $document->save();
    189205
     206//        if($hasFileError) {
     207//            Alert::flash('Document with the same name exists', 'error');
     208//            return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
     209//        }
     210
     211
     212
    190213        Alert::flash("Document edited successfully");
    191214
    192         return redirect()->route("dashboard.documents.index");
     215        return redirect()->back();
    193216    }
    194217
     
    230253    {
    231254        $document = Document::find($id);
     255        $files = File::where('document_id', $id)->get();
    232256        if (auth()->user()->hasPermission("delete_all_documents")) {
     257           // @dd($files);
     258            foreach ($files as $file) {
     259                $file->delete();
     260            }
    233261            $document->delete();
     262            $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
     263            Storage::disk('local')->deleteDirectory($location);
    234264            Alert::flash($document->name . " deleted successfully");
    235265        }
    236266        return redirect()->route("dashboard.documents.index");
    237267    }
     268
     269    public function deleteFile($id)
     270    {
     271        $file = File::find($id);
     272        $file->delete();
     273        Storage::disk('local')->delete($file->location);
     274
     275        Alert::flash($file->name . " deleted successfully");
     276
     277        return redirect()->back();
     278    }
     279
     280    public function downloadFile($id)
     281    {
     282        $file = File::find($id);
     283        return Storage::download($file->location);
     284    }
     285
     286    public function renameFile(FileRequest $request, $id)
     287    {
     288        $file = File::find($id);
     289        $fileExtension = explode('.', $file->name)[1];
     290
     291        $file->name = $request->name . '.' . $fileExtension;
     292        $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
     293
     294        if(Storage::disk('local')->has($newLocation)) {
     295            Alert::flash("A file with the same name already exists", "error");
     296            return redirect()->back();
     297        }
     298        else {
     299            Storage::disk('local')->copy($file->location, $newLocation);
     300            Storage::disk('local')->delete($file->location);
     301
     302        $file->location = $newLocation;
     303        $file->save();
     304
     305        Alert::flash($file->name . " updated successfully");
     306        return redirect()->back();
     307        }
     308    }
    238309}
  • app/Http/Requests/Dashboard/DocumentRequest.php

    rb9c4a92 rea7b12a  
    55use App\Models\Department;
    66use App\Models\Document;
    7 use App\Models\FileType;
    87use Illuminate\Foundation\Http\FormRequest;
    98
     
    3231    public function rules()
    3332    {
    34 
    3533        $rules = [
    3634            "arch_id" => [
     
    5755        if ($this->isMethod("patch")) {
    5856            $fileRules = [
    59                 "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
     57                "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096"
    6058            ];
    6159        } else {
    6260            $fileRules = [
    63                 "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
     61                "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096"
    6462            ];
    6563        }
Note: See TracChangeset for help on using the changeset viewer.