Changeset b9c4a92 for app/Http


Ignore:
Timestamp:
10/18/21 23:00:23 (3 years ago)
Author:
Berat Kjufliju <kufliju@…>
Branches:
develop, master
Children:
ea7b12a
Parents:
e6c1f87
Message:

edited file upload, seeding and departments controller

Location:
app/Http
Files:
3 edited

Legend:

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

    re6c1f87 rb9c4a92  
    1414use Illuminate\Support\Facades\Auth;
    1515use Illuminate\Support\Facades\Storage;
     16use function Illuminate\Events\queueable;
    1617
    1718class DepartmentsController extends Controller
     
    3637        $department->code = $request->code;
    3738
    38         $len = Storage::disk('local')->path('');
     39        $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
    3940
    40         if(!Storage::disk('local')->has('Departments/' . $request->code)){
    41             Storage::disk('local')->makeDirectory('Departments/' . $request->code);
     41        if(!Storage::disk('local')->has($location)){
     42            Storage::disk('local')->makeDirectory($location);
     43
    4244        }
    43 
     45        $department->location = Storage::disk('local')->path('') . $location;
    4446        $department->user_id = auth()->id();
    45         $department->location =  Storage::disk('local')->path('') . 'Departments/' . $request->code;
    4647
    4748        $department->save();
     
    6364        $department = Department::findOrFail($id);
    6465
    65         $oldDepartmentCode = $department->code;
     66        $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
    6667
    6768        $department->name = $request->name;
    6869        $department->code = $request->code;
    6970
    70         $department->location =  Storage::disk('local')->path('') . 'Departments/' . $request->code;
    71 
    72         $files = Storage::allFiles($oldDepartmentCode);
    73 
    7471        if($department->isDirty('code'))
    7572        {
    76             if(!Storage::disk('local')->has('Departments/' . $request->code)){
    77                 Storage::disk('local')->move('Departments/' . $oldDepartmentCode, 'Departments/' . $department->code);
     73            $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
     74            if(!Storage::disk('local')->has($location)){
     75                Storage::disk('local')->move($oldLocation, $location);
     76                $department->location = Storage::disk('local')->path('') . $location;
    7877            }
    7978        }
  • app/Http/Controllers/Dashboard/DocumentsController.php

    re6c1f87 rb9c4a92  
    2626        $result = $result->flatten();
    2727
     28        $deptName = "";
     29        $deptCode = "";
    2830
    2931        if ($request->query('id')) {
    30 
    31             $documentsInDeptSort = Document::with('department')->when($request->has('id'), function($query) use ($request) {
     32            $deptName = Department::find($request->query('id'))->getOriginal('name');
     33            $deptCode = Department::find($request->query('id'))->getOriginal('code');
     34            $documentsInDeptSort = Document::with('department')->when($request->has('id'), function ($query) use ($request) {
    3235                $query->where('department_id', $request->query('id'));
    3336            });
    3437
    35             if($request->query('sort') == 'newest') {
    36                 $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(20);
    37             }
    38             else if($request->query('sort') == 'name') {
    39                 $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(20);
    40             }
    41             else{
    42                 $documents = Document::where('department_id', $request->query('id'))->paginate(20);
    43             }
    44         }
    45         else {
    46             if($request->query('sort') == 'newest') {
    47                 $documents = Document::orderBy('created_at', 'desc')->paginate(20);
    48             }
    49             else if($request->query('sort') == 'name') {
    50                 $documents = Document::orderBy('name', 'asc')->paginate(20);
    51             }
    52             else if($request->query('sort') == 'important'){
    53                 $documents = Document::where('is_important', true)->paginate(20);
    54             }
    55             else if($request->query('sort') == 'recent') {
    56                $documents = Document::orderBy('created_at', 'desc')->paginate(20);
    57             }
    58             else if($request->query('search')){
     38            if ($request->query('sort') == 'newest') {
     39                $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(16);
     40            } else if ($request->query('sort') == 'name') {
     41                $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(16);
     42            } else {
     43                $documents = Document::where('department_id', $request->query('id'))->paginate(16);
     44            }
     45        } else {
     46            if ($request->query('sort') == 'newest') {
     47                $documents = Document::orderBy('created_at', 'desc')->paginate(16);
     48            } else if ($request->query('sort') == 'name') {
     49                $documents = Document::orderBy('name', 'asc')->paginate(16);
     50            } else if ($request->query('sort') == 'important') {
     51                $documents = Document::where('is_important', true)->paginate(16);
     52            } else if ($request->query('sort') == 'recent') {
     53                $documents = Document::orderBy('created_at', 'desc')->paginate(16);
     54            } else if ($request->query('search')) {
    5955                $result = collect();
    6056
     
    6460                $result = $result->flatten();
    6561                $documents = $result;
    66             }
    67             else
    68             {
     62            } else {
    6963                $documents = Document::paginate(20);
    7064            }
     
    7670        $diskTotalSize = $diskTotal / 1073741824;
    7771
    78         $diskFree  = disk_free_space('/');
     72        $diskFree = disk_free_space('/');
    7973        $used = $diskTotal - $diskFree;
    8074
     
    9387            "diskTotalSize" => $diskTotalSize,
    9488            "diskUse" => $diskUse,
    95             "diskUsedSize" => $diskUsedSize
     89            "diskUsedSize" => $diskUsedSize,
     90            "deptName" => $deptName,
     91            "deptCode" => $deptCode,
    9692
    9793        ]);
     
    106102    }
    107103
    108     public function store(DocumentRequest $request, UploadService $uploadService)
     104    public function store(DocumentRequest $request)
    109105    {
    110106        $document = new Document();
     
    119115        $document->description = $request->description;
    120116
    121         if (!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)) {
    122             Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
    123         }
    124 
    125         $documentFile = $uploadService->upload(File::class, [
    126             "file_item" => $request->file_item,
    127         ], "link", true);
     117        $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;
     118
     119        if (!Storage::disk('local')->has($location)) {
     120            Storage::disk('local')->makeDirectory($location);
     121        }
     122
     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        }
    128130
    129131        $document->save();
    130132
    131         foreach ($documentFile as $df) {
    132             $file = File::find($df);
    133             $file->document()->associate($document);
    134             $file->save();
    135         }
    136 
    137133        Alert::flash("New document created successfully");
    138134
     
    142138    public function editShow($id)
    143139    {
    144 //        if (!auth()->user()->hasPermission("edit_all_documents")) {
    145 //            return redirect()->route("dashboard.documents.index");
    146 //        }
    147 
    148140        return view("dashboard.documents.edit")->with([
    149141            "document" => Document::findOrFail($id),
     
    164156        $document->description = $request->description;
    165157
     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;
     160
     161        if ($document->isDirty('name')) {
     162            if (!Storage::disk('local')->has($location)) {
     163                Storage::disk('local')->move($oldLocation, $location);
     164            }
     165        }
     166
     167        $hasFileError = false;
     168        if ($request->has('file_item')) {
     169            foreach ($request->file_item as $file) {
     170                $fileName = $file->getClientOriginalName();
     171                if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
     172                    $hasFileError = true;
     173                    break;
     174                }
     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]);
     186        }
     187
    166188        $document->save();
    167189
     
    177199        $document->save();
    178200
    179         if($document->is_important==true)
    180         Alert::flash("Document marked as important successfully");
     201        if ($document->is_important == true)
     202            Alert::flash("Document marked as important successfully");
    181203        else
    182204            Alert::flash("Document marked as not important successfully");
     
    208230    {
    209231        $document = Document::find($id);
    210         if (auth()->user()->hasPermission("delete_all_posts")) {
     232        if (auth()->user()->hasPermission("delete_all_documents")) {
    211233            $document->delete();
    212234            Alert::flash($document->name . " deleted successfully");
  • app/Http/Requests/Dashboard/DocumentRequest.php

    re6c1f87 rb9c4a92  
    3636            "arch_id" => [
    3737                "required",
    38                 function($attribute, $value, $fail) {
     38                function ($attribute, $value, $fail) {
    3939                    $arch_id = $this->request->get('arch_id');
    4040                    $deptId = explode('/', $arch_id)[0];
     41                    $archNum = explode('/', $arch_id)[1];
     42
     43                    if (empty($archNum)) {
     44                        $fail("Please enter documents Archive ID");
     45                    }
     46
    4147                    if ($deptId !== Department::find($this->request->get('department'))->code) {
    4248                        $fail("Document Archive ID field format is invalid");
Note: See TracChangeset for help on using the changeset viewer.