search team folder contents

This commit is contained in:
Čarodej
2021-11-04 08:06:37 +01:00
parent cdaad931bb
commit 5a9f2985c9
6 changed files with 1211 additions and 759 deletions
@@ -1,8 +1,10 @@
<?php
namespace Domain\Browsing\Controllers;
use DB;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Domain\Items\Requests\SearchRequest;
use Domain\Files\Resources\FilesCollection;
@@ -15,17 +17,45 @@ class SearchFilesAndFoldersController
): array {
$user_id = Auth::id();
// Prepare queries
$query = remove_accents(
$request->input('query')
);
// Search files id db
$files = File::search($query)
// Get "shared with me" folders
$sharedWithMeFolderIds = DB::table('team_folder_members')
->where('user_id', $user_id)
->pluck('parent_id');
// Next get their folder tree for ids extraction
$folderWithinIds = Folder::with('folders:id,parent_id')
->whereIn('parent_id', $sharedWithMeFolderIds)
->get(['id']);
// Then get all accessible shared folders within
$accessible_parent_ids = Arr::flatten([filter_folders_ids($folderWithinIds), $sharedWithMeFolderIds]);
// Prepare eloquent builder
$folder = new Folder();
$file = new File();
// Prepare folders constrain
$folderConstrain = $folder
->where('user_id', $user_id)
->orWhereIn('id', $accessible_parent_ids);
// Prepare files constrain
$fileConstrain = $file
->where('user_id', $user_id)
->orWhereIn('parent_id', $accessible_parent_ids);
// Search files and folders
$files = File::search($query)
->constrain($fileConstrain)
->get();
$folders = Folder::search($query)
->where('user_id', $user_id)
->constrain($folderConstrain)
->get();
// Collect folders and files to single array
@@ -10,20 +10,20 @@ class NavigationTreeController
public function __invoke(): array
{
// Get signed user folders
$folders = Folder::with('folders:id,parent_id,id,name,team_folder')
$folders = Folder::with('folders:id,parent_id,name,team_folder')
->where('parent_id')
->where('team_folder', false)
->where('user_id', Auth::id())
->sortable()
->get(['id', 'parent_id', 'id', 'name', 'team_folder']);
->get(['id', 'parent_id', 'name', 'team_folder']);
// Get signed user team folders
$teamFolders = Folder::with('folders:id,parent_id,id,name,team_folder')
$teamFolders = Folder::with('folders:id,parent_id,name,team_folder')
->where('parent_id')
->where('team_folder', true)
->where('user_id', Auth::id())
->sortable()
->get(['id', 'parent_id', 'id', 'name']);
->get(['id', 'parent_id', 'name']);
// Get signed user folder which are shared with him
$sharedFolderIds = DB::table('team_folder_members')
@@ -31,10 +31,10 @@ class NavigationTreeController
->whereIn('permission', ['can-edit', 'can-view'])
->pluck('parent_id');
$sharedWithMeFolders = Folder::with('folders:id,parent_id,id,name,team_folder')
$sharedWithMeFolders = Folder::with('folders:id,parent_id,name,team_folder')
->whereIn('id', $sharedFolderIds)
->sortable()
->get(['id', 'parent_id', 'id', 'name']);
->get(['id', 'parent_id', 'name']);
return [
[