Ignore:
Timestamp:
10/31/21 21:28:46 (3 years ago)
Author:
beratkjufliju <kufliju@…>
Branches:
master
Children:
4521f25
Parents:
a55bb54
Message:

added version

File:
1 edited

Legend:

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

    ra55bb54 re78295c  
    1818use Illuminate\Support\Facades\Storage;
    1919use App\Http\Controllers\Controller;
     20use function Sodium\increment;
    2021
    2122class FoldersController extends Controller
     
    5859            else if ($request->query('sort') == 'no_of_files') {
    5960                $folders = Folder::orderBy('no_of_files', 'desc')->paginate(12);
    60             }
    61             else if ($request->query('sort') == 'important') {
    62                 $folders = Folder::where('is_important', true)->paginate(12);
    6361            }
    6462            else if ($request->query('sort') == 'recent') {
     
    9694            "docsCount" => Department::withCount('folder')->get(),
    9795            "totalDocs" => Folder::all()->count(),
    98             "countImportant" => Folder::where('is_important', true)->get()->count(),
    9996            "diskTotal" => $diskTotal,
    10097            "diskTotalSize" => $diskTotalSize,
     
    118115    {
    119116        $folder = new Folder();
     117
     118        $existingFolder = Folder::where(['department_id' => $request->department, 'name' => $request->name, 'arch_id' => $request->arch_id])->count();
     119
     120        $existingFolderName = Folder::where(['department_id' => $request->department, 'name' => $request->name])->count();
     121        $existingFolderArchId = Folder::where(['department_id' => $request->department, 'arch_id' => $request->arch_id])->count();
     122
     123        if($existingFolder > 0) {
     124            $folder->version = $existingFolder + 1;
     125        }
     126
     127        if(($existingFolderName > 0 && $existingFolderArchId <= 0) || ($existingFolderName <= 0 && $existingFolderArchId > 0)) {
     128            Alert::flash("Can't create another version since folder name or archive ID is different", "error");
     129
     130            return redirect()->route("dashboard.folders.index");
     131        }
     132
     133        $department = Department::find($request->department);
     134
    120135        $user = auth()->user();
    121         $department = Department::find($request->department);
    122136
    123137        $folder->user()->associate($user);
     
    128142        $folder->name = $request->name;
    129143        $folder->note = $request->note;
    130 
    131         if($request->has('is_important')){
    132             $folder->is_important = true;
    133         }else{
    134             $folder->is_important = false;
    135         }
    136144
    137145        $location = $folder->department->location . DIRECTORY_SEPARATOR . $request->name;
     
    178186    }
    179187
    180     public function edit(FolderRequest $request, $id)
    181     {
    182         $folder = Folder::findOrFail($id);
     188    public function destroy($id)
     189    {
     190        $folder = Folder::find($id);
     191
     192        $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id])->count();
     193
     194        if($existingFolders > 1 && $folder->version == 1) {
     195            Alert::flash($folder->name . " has versions", "error");
     196            return redirect()->back();
     197        }
     198
    183199        $files = File::where('folder_id', $id)->get();
    184200
    185         $department = Department::find($request->department);
    186 
    187         $folder->department()->increment('no_of_folders');
    188 
    189         $oldLocation = $folder->location;
    190 
    191         $folder->name = $request->name;
    192         $folder->arch_id = $request->arch_id;
    193         $folder->note = $request->note;
    194         $folder->updated_at = Carbon::now();
    195 
    196        $newLocation = Department::find($request->department)->location . DIRECTORY_SEPARATOR . $request->name;
    197 
    198         if($folder->department_id != $request->department){
    199             $folder->department()->decrement('no_of_folders');
    200             if (!Storage::disk('uploads')->has($newLocation)) {
    201                 Storage::disk('uploads')->move($oldLocation, $newLocation);
    202                 foreach($files as $file) {
    203                     $file->location = $newLocation . DIRECTORY_SEPARATOR . $file->name;
    204                     $file->save();
    205                 }
    206             }
    207         }
    208          if($folder->isDirty('name')) {
    209             if (!Storage::disk('uploads')->has($newLocation)) {
    210                 Storage::disk('uploads')->move($oldLocation, $newLocation);
    211                 foreach($files as $file) {
    212                     $file->location = $newLocation . DIRECTORY_SEPARATOR . $file->name;
    213                     $file->save();
    214                 }
    215             }
    216         }
    217 
    218         $folder->department()->associate($department);
    219 
    220         $folder->location = $newLocation;
    221 
    222         if ($request->has('file_item')) {
    223             foreach ($request->file_item as $file) {
    224                 $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension();
    225                     $file->storeAs($newLocation . DIRECTORY_SEPARATOR, $fileName);
    226                     $newFile = new File();
    227                     $newFile->name = $fileName;
    228                     $newFile->location = $newLocation . DIRECTORY_SEPARATOR . $fileName;
    229                     $newFile->folder()->associate($folder);
    230                     $newFile->folder()->increment('no_of_files');
    231                     $newFile->save();
    232             }
    233         }
    234 
    235         $folder->save();
    236 
    237         Alert::flash("Folder edited successfully");
    238 
    239         return redirect()->back();
    240     }
    241 
    242     public function toggleImportant($id)
    243     {
    244         $folder = Folder::find($id);
    245         $folder->is_important = !$folder->is_important;
    246         $folder->save();
    247 
    248         if ($folder->is_important == true)
    249             Alert::flash("Folder marked as important successfully");
    250         else
    251             Alert::flash("Folder marked as not important successfully");
    252 
    253         return redirect()->back();
    254     }
    255 
    256     public function destroy($id)
    257     {
    258         $folder = Folder::find($id);
    259         $files = File::where('folder_id', $id)->get();
    260         if (auth()->user()->hasPermission("delete_all_folders") && !$folder->is_important) {
     201        if($files->count() > 0) {
     202            Alert::flash($folder->name . " contains files", "error");
     203            return redirect()->back();
     204        }
     205
     206        if (auth()->user()->hasPermission("delete_all_folders")) {
    261207
    262208            foreach ($files as $file) {
    263209                $file->delete();
    264210            }
     211
    265212            $folder->delete();
    266213            $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
     
    270217            return redirect()->back();
    271218        }
    272         Alert::flash($folder->name . " is important", "error");
     219        Alert::flash($folder->name . " cannot be deleted", "error");
    273220        return redirect()->back();
    274221    }
     
    337284            "imageExt" => array("png", "jpg", "jpeg"),
    338285            "folders" => $folders,
    339             "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
     286            "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)),
    340287        ]);
    341288    }
Note: See TracChangeset for help on using the changeset viewer.