Ignore:
Timestamp:
10/18/21 19:54:18 (3 years ago)
Author:
beratkjufliju <kufliju@…>
Branches:
develop, master
Children:
b9c4a92
Parents:
bd9e8e3
Message:

added pagination, is_important

File:
1 edited

Legend:

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

    rbd9e8e3 re6c1f87  
    1717    public function index(Request $request)
    1818    {
     19        $queries = explode(" ", $request->search);
     20        $result = collect();
     21
     22        foreach ($queries as $query) {
     23            $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
     24        }
     25
     26        $result = $result->flatten();
     27
     28
    1929        if ($request->query('id')) {
    20             $documents = Document::where('department_id', $request->query('id'))->get();
    21         } else {
    22             $documents = Document::all();
     30
     31            $documentsInDeptSort = Document::with('department')->when($request->has('id'), function($query) use ($request) {
     32                $query->where('department_id', $request->query('id'));
     33            });
     34
     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')){
     59                $result = collect();
     60
     61                foreach ($queries as $query) {
     62                    $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
     63                }
     64                $result = $result->flatten();
     65                $documents = $result;
     66            }
     67            else
     68            {
     69                $documents = Document::paginate(20);
     70            }
    2371        }
    2472
    2573        $departments = Department::all();
     74
     75        $diskTotal = disk_total_space('/');
     76        $diskTotalSize = $diskTotal / 1073741824;
     77
     78        $diskFree  = disk_free_space('/');
     79        $used = $diskTotal - $diskFree;
     80
     81        $diskUsedSize = $used / 1073741824;
     82        $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
     83        $diskUse = round(100 - ($diskUse1)) . '%';
    2684
    2785        return view("dashboard.documents.index")->with([
     
    3088            "departments" => $departments,
    3189            "docsCount" => Department::withCount('document')->get(),
    32             'totalDocs' => Document::all()->count()
     90            "totalDocs" => Document::all()->count(),
     91            "countImportant" => Document::where('is_important', true)->get()->count(),
     92            "diskTotal" => $diskTotal,
     93            "diskTotalSize" => $diskTotalSize,
     94            "diskUse" => $diskUse,
     95            "diskUsedSize" => $diskUsedSize
     96
    3397        ]);
    3498
     
    40104            "departments" => Department::all()
    41105        ]);
     106    }
     107
     108    public function store(DocumentRequest $request, UploadService $uploadService)
     109    {
     110        $document = new Document();
     111        $user = auth()->user();
     112        $department = Department::find($request->department);
     113
     114        $document->user()->associate($user);
     115        $document->department()->associate($department);
     116
     117        $document->arch_id = $request->arch_id;
     118        $document->name = $request->name;
     119        $document->description = $request->description;
     120
     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);
     128
     129        $document->save();
     130
     131        foreach ($documentFile as $df) {
     132            $file = File::find($df);
     133            $file->document()->associate($document);
     134            $file->save();
     135        }
     136
     137        Alert::flash("New document created successfully");
     138
     139        return redirect()->route("dashboard.documents.index");
    42140    }
    43141
     
    73171    }
    74172
    75     public function store(DocumentRequest $request, UploadService $uploadService)
    76     {
    77         $document = new Document();
    78         $user = auth()->user();
    79         $department = Department::find($request->department);
    80 
    81         $document->user()->associate($user);
    82         $document->department()->associate($department);
    83 
    84         $document->arch_id = $request->arch_id;
    85         $document->name = $request->name;
    86         $document->description = $request->description;
    87 
    88         if (!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)) {
    89             Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
    90         }
    91 
    92         $documentFile = $uploadService->upload(File::class, [
    93             "file_item" => $request->file_item,
    94         ], "link", true);
    95 
     173    public function toggleImportant($id)
     174    {
     175        $document = Document::find($id);
     176        $document->is_important = !$document->is_important;
    96177        $document->save();
    97178
    98         foreach ($documentFile as $df) {
    99             $file = File::find($df);
    100             $file->document()->associate($document);
    101             $file->save();
    102         }
    103 
    104         Alert::flash("New document created successfully");
    105 
     179        if($document->is_important==true)
     180        Alert::flash("Document marked as important successfully");
     181        else
     182            Alert::flash("Document marked as not important successfully");
     183
     184        return redirect()->back();
     185    }
     186
     187    public function search(Request $request)
     188    {
     189        $queries = explode(" ", $request->q);
     190        $result = collect();
     191
     192        foreach ($queries as $query) {
     193            $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
     194        }
     195
     196        $result = $result->flatten();
     197
     198        $departments = Department::all();
     199
     200        return view("dashboard.documents.search")
     201            ->with("searchQuery", $request->q)
     202            ->with("results", $result)
     203            ->with("departments", $departments)
     204            ->with("countImportant", Document::where('is_important', true)->get()->count());
     205    }
     206
     207    public function destroy($id)
     208    {
     209        $document = Document::find($id);
     210        if (auth()->user()->hasPermission("delete_all_posts")) {
     211            $document->delete();
     212            Alert::flash($document->name . " deleted successfully");
     213        }
    106214        return redirect()->route("dashboard.documents.index");
    107215    }
    108 
    109216}
Note: See TracChangeset for help on using the changeset viewer.