706 lines
23 KiB
PHP
706 lines
23 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\User;
|
||
|
|
use App\MyFile;
|
||
|
|
use App\Setting;
|
||
|
|
use App\Department;
|
||
|
|
use App\SharedFile;
|
||
|
|
use Session;
|
||
|
|
use Auth;
|
||
|
|
|
||
|
|
class MyFilesController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Create a new controller instance.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->middleware('auth:web');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function search(Request $request, $directory=null)
|
||
|
|
{
|
||
|
|
$input = $request->all();
|
||
|
|
$request->session()->put('search_val', $input['search']);
|
||
|
|
if($directory != null)
|
||
|
|
{
|
||
|
|
return redirect()->route('my-files', $directory);
|
||
|
|
}
|
||
|
|
return redirect()->route('my-files');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index($directory=null)
|
||
|
|
{
|
||
|
|
$setting = Setting::first();
|
||
|
|
$parent_id = 0;
|
||
|
|
$parent_directories = [];
|
||
|
|
$last = null;
|
||
|
|
if($directory !== null)
|
||
|
|
{
|
||
|
|
$parent_id = base64_decode($directory);
|
||
|
|
$parent_directories = MyFile::getPathArray($parent_id);
|
||
|
|
$last = array_key_last($parent_directories);
|
||
|
|
}
|
||
|
|
$directories=Myfile::where('user_id', auth()->user()->id)->where('parent_id', $parent_id)->where('is_temp', 0)->orderBy('id', 'desc')->get();
|
||
|
|
return view('my-files.index', compact('setting', 'directories', 'parent_id', 'parent_directories', 'last'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create($directory=null)
|
||
|
|
{
|
||
|
|
if($directory)
|
||
|
|
{
|
||
|
|
$directory_id = base64_decode($directory);
|
||
|
|
$directory = MyFile::find($directory_id);
|
||
|
|
|
||
|
|
if($directory->user_id != auth()->user()->id)
|
||
|
|
{
|
||
|
|
$shared = SharedFile::where('shared_by', $directory->user_id)->where('permission', 2)->where(function($q){
|
||
|
|
$q->orWhere('user_id', auth()->user()->id)
|
||
|
|
->orWhere('department_id', auth()->user()->department_id);
|
||
|
|
})->get();
|
||
|
|
// dd($shared);
|
||
|
|
// return response()->json(['result' => 'success', 'data' => $shared->count()]);
|
||
|
|
// return response()->json(['result' => 'success', 'data' => $shared->first()->id]);
|
||
|
|
|
||
|
|
if(count($shared)>0)
|
||
|
|
{
|
||
|
|
$flag=false;
|
||
|
|
// return response()->json(['result' => 'success', 'data' => $shared->first()->file_id]);
|
||
|
|
foreach($shared as $sh)
|
||
|
|
{
|
||
|
|
if(preg_match("/;".$sh->file_id.";/", $directory->path_to_root . ";".$directory->id.";"))
|
||
|
|
{
|
||
|
|
$flag = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// return response()->json(['result' => 'success', 'data' => $directory->path_to_root . $directory->id.";"]);
|
||
|
|
if($flag==true)
|
||
|
|
{
|
||
|
|
$view = view('my-files.create')->with('directory_id', $directory->id)->with('flag', $flag)->render();
|
||
|
|
return response()->json(['result' => 'success', 'data' => $view]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return response()->json(['result'=>'error', 'data'=> __('We are having some problem. Please try later.')]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$directoryid = ($directory) ? $directory->id : 0;
|
||
|
|
$view = view('my-files.create')->with('directory_id', $directoryid)->render();
|
||
|
|
return response()->json(['result' => 'success', 'data' => $view]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
$input = $request->all();
|
||
|
|
if(isset($input['directory_name']) && $input['directory_name'] !== null)
|
||
|
|
{
|
||
|
|
$directory_input['_token']=$input['_token'];
|
||
|
|
$directory_input['name']=$input['directory_name'];
|
||
|
|
$directory_input['description']=$input['description'] ?? '';
|
||
|
|
$directory_input['year'] = date('Y');
|
||
|
|
$directory_input['is_temp'] = 0;
|
||
|
|
$directory_input['file_type'] = 0;
|
||
|
|
if($input['flag'])
|
||
|
|
{
|
||
|
|
$directory_input['user_id'] = MyFile::where('id', $input['parent_id'])->first()->user_id;
|
||
|
|
$directory_input['shared_user_id'] = auth()->user()->id;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
$directory_input['user_id'] = auth()->user()->id;
|
||
|
|
|
||
|
|
if($input['parent_id'] !== null)
|
||
|
|
{
|
||
|
|
// if directory_id is not belongs to auth()->user()->id ???
|
||
|
|
$directory_input['parent_id'] = $input['parent_id'];
|
||
|
|
$directory_input['path_to_root'] = MyFile::getPathToRootString($input['parent_id']);
|
||
|
|
}
|
||
|
|
$directory_save=MyFile::create($directory_input);
|
||
|
|
if($directory_save)
|
||
|
|
{
|
||
|
|
$input['parent_id'] = $directory_save['id'];
|
||
|
|
// $directory_path = directory_path($directory_save['id'], '');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
elseif($input['parent_id'] == null)
|
||
|
|
{
|
||
|
|
$input['parent_id'] = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
$input['user_id']=auth()->user()->id;
|
||
|
|
$input['path_to_root'] = MyFile::getPathToRootString($input['parent_id']);
|
||
|
|
$input['shared_user_id'] = null;
|
||
|
|
if($input['flag'])
|
||
|
|
{
|
||
|
|
$input['user_id'] = MyFile::where('id', $input['parent_id'])->first()->user_id;
|
||
|
|
$input['shared_user_id'] = auth()->user()->id;
|
||
|
|
}
|
||
|
|
|
||
|
|
// dd($input);
|
||
|
|
if(isset($input['file_ids']) && $input['file_ids']!='')
|
||
|
|
{
|
||
|
|
$file_ids_array = explode(';',$input['file_ids']);
|
||
|
|
if(is_array($file_ids_array) && !empty($file_ids_array)){
|
||
|
|
foreach($file_ids_array as $key => $file_id){
|
||
|
|
$file = MyFile::where('id',$file_id)->first();
|
||
|
|
if(isset($file))
|
||
|
|
{
|
||
|
|
MyFile::find($file->id)->update(['is_temp'=>0, 'user_id'=>$input['user_id'], 'shared_user_id'=>$input['shared_user_id'], 'path_to_root'=>$input['path_to_root'], 'description' =>$input['description'], 'parent_id'=>$input['parent_id']]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Session::flash('success_message', __('My files has been added successfully'));
|
||
|
|
return redirect()->back();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function storeFiles(Request $request)
|
||
|
|
{
|
||
|
|
$input = $request->all();
|
||
|
|
$file_name='';
|
||
|
|
|
||
|
|
// $setting = Setting::first();
|
||
|
|
$place_of_the_file='my-files/'.auth()->user()->id;
|
||
|
|
|
||
|
|
$input['place_of_the_file']= $place_of_the_file;
|
||
|
|
$input['user_id']=auth()->user()->id;
|
||
|
|
$input['file_type'] = 1;
|
||
|
|
$input['year'] = date('Y');
|
||
|
|
|
||
|
|
if(isset($input['files']) && is_array($input['files']) && !empty($input['files']))
|
||
|
|
{
|
||
|
|
foreach($input['files'] as $key => $file)
|
||
|
|
{
|
||
|
|
$file = $input['files'];
|
||
|
|
if(isset($file) && $file->getError() == 0 && $file->getClientOriginalName() != "")
|
||
|
|
{
|
||
|
|
$input['file_size'] = $file_size = ($file->getClientSize() / 1024)/1024;
|
||
|
|
$user = User::where('id', auth()->user()->id)->first();
|
||
|
|
$user_new_disk_uses = $user->user_disk_uses > 0 ? $user->user_disk_uses + $file_size : $file_size;
|
||
|
|
if($user_new_disk_uses < $user->user_disk_quota)
|
||
|
|
{
|
||
|
|
User::where('id', auth()->user()->id)->update(['user_disk_uses'=> $user_new_disk_uses]);
|
||
|
|
$input['name_org'] = $file->getClientOriginalName();
|
||
|
|
$input['name'] = $file_name = md5($file->getClientOriginalName() . microtime()) .'.'. $file->getClientOriginalExtension();
|
||
|
|
$saved_data=MyFile::create($input);
|
||
|
|
if($saved_data)
|
||
|
|
{
|
||
|
|
$file->move(public_path($place_of_the_file), $file_name);
|
||
|
|
$files_val[$key]['data']= $saved_data;
|
||
|
|
$files_val[$key]['type']= 'success';
|
||
|
|
$files_val[$key]['message']= __('File uploaded successfully');
|
||
|
|
|
||
|
|
if(!in_array($file->getClientOriginalExtension(), ['pdf', 'zip', 'tar', 'rar', 'exe', 'gz']))
|
||
|
|
{
|
||
|
|
shell_exec('export HOME=/tmp && /usr/bin/libreoffice --headless --convert-to pdf ' . public_path($place_of_the_file) . '/' . $file_name . ' --outdir ' . public_path($place_of_the_file));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$files_val[$key]['data']= [];
|
||
|
|
$files_val[$key]['type']= 'error';
|
||
|
|
$files_val[$key]['message']= __('Exceed Disk Quota');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$file = $input['files'];
|
||
|
|
$key = 0;
|
||
|
|
if(isset($file) && $file->getError() == 0 && $file->getClientOriginalName() != "")
|
||
|
|
{
|
||
|
|
$input['file_size'] = $file_size = ($file->getClientSize() / 1024)/1024;
|
||
|
|
$user = User::where('id', auth()->user()->id)->first();
|
||
|
|
$user_new_disk_uses = $user->user_disk_uses > 0 ? $user->user_disk_uses + $file_size : $file_size;
|
||
|
|
if($user_new_disk_uses < $user->user_disk_quota)
|
||
|
|
{
|
||
|
|
User::where('id', auth()->user()->id)->update(['user_disk_uses'=> $user_new_disk_uses]);
|
||
|
|
$input['name_org'] = $file->getClientOriginalName();
|
||
|
|
$input['name'] = $file_name = md5($file->getClientOriginalName() . microtime()) .'.'. $file->getClientOriginalExtension();
|
||
|
|
$saved_data=MyFile::create($input);
|
||
|
|
if($saved_data)
|
||
|
|
{
|
||
|
|
$file->move(public_path($place_of_the_file), $file_name);
|
||
|
|
$files_val[$key]['data']= $saved_data;
|
||
|
|
$files_val[$key]['type']= 'success';
|
||
|
|
$files_val[$key]['message']= __('File uploaded successfully');
|
||
|
|
|
||
|
|
if(!in_array($file->getClientOriginalExtension(), ['pdf', 'zip', 'tar', 'rar', 'exe', 'gz']))
|
||
|
|
{
|
||
|
|
shell_exec('export HOME=/tmp && /usr/bin/libreoffice --headless --convert-to pdf ' . public_path($place_of_the_file) . '/' . $file_name . ' --outdir ' . public_path($place_of_the_file));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$files_val[$key]['data']= [];
|
||
|
|
$files_val[$key]['type']= 'error';
|
||
|
|
$files_val[$key]['message']= __('Exceed Disk Quota');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return response()->json($files_val);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit($directory_enc)
|
||
|
|
{
|
||
|
|
if($directory_enc)
|
||
|
|
{
|
||
|
|
$directory = MyFile::find(base64_decode($directory_enc));
|
||
|
|
if($directory)
|
||
|
|
{
|
||
|
|
if($directory->user_id != auth()->user()->id)
|
||
|
|
return response()->json(['result'=>'error', 'data'=> __('We are having some problem. Please try later.')]);
|
||
|
|
|
||
|
|
$view = view('my-files.edit')->with('directory', $directory)->render();
|
||
|
|
return response()->json(['result' => 'success', 'data' => $view]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json(['result' => 'false', 'data' => __('We are having some problem. Please try later.')]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update()
|
||
|
|
{
|
||
|
|
$input = request()->all();
|
||
|
|
if(!isset($input['directory_id']))
|
||
|
|
{
|
||
|
|
Session::flash('success_message', __('My files has been added successfully'));
|
||
|
|
return redirect(route('my-files'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$directory = MyFile::find(base64_decode($input['directory_id']));
|
||
|
|
if($directory)
|
||
|
|
{
|
||
|
|
$directory->description = $input['description'];
|
||
|
|
|
||
|
|
if($directory->file_type == 0)
|
||
|
|
$directory->name = $input['directory_name'];
|
||
|
|
else if($directory->file_type == 1)
|
||
|
|
$directory->name_org = $input['directory_name'];
|
||
|
|
|
||
|
|
$directory->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
Session::flash('success_message', __('My files has been added successfully'));
|
||
|
|
return redirect()->back();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete($id)
|
||
|
|
{
|
||
|
|
$directory = MyFile::find(base64_decode($id));
|
||
|
|
if($directory)
|
||
|
|
{
|
||
|
|
if($directory->file_type == 1)
|
||
|
|
{
|
||
|
|
$directory->delete();
|
||
|
|
unlink(public_path($directory->place_of_the_file.'/'.$directory->name));
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$folders = MyFile::where('is_temp', 0)->where('user_id', auth()->user()->id)->where('path_to_root', 'LIKE', '%;'.$directory->id.';%')->orderByDesc('path_to_root')->get();
|
||
|
|
foreach($folders as $folder)
|
||
|
|
{
|
||
|
|
if(Str::contains($folder->path_to_root, ";".$directory->id.";"))
|
||
|
|
{
|
||
|
|
$folder->delete();
|
||
|
|
if($folder->file_type == 1)
|
||
|
|
unlink(public_path($folder->place_of_the_file.'/'.$folder->name));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$directory->delete();
|
||
|
|
SharedFile::deleteRelatedShares($directory);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Session::flash('success_message', __('Directory has been deleted successfully'));
|
||
|
|
return redirect()->route('my-files', base64_encode($directory->parent_id));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function view($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$file = MyFile::find($id);
|
||
|
|
if($file):
|
||
|
|
return view('my-files.view', compact('file'));
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->back();
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function download($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$file = MyFile::find($id);
|
||
|
|
|
||
|
|
if($file && $file->file_type == 1)
|
||
|
|
if(file_exists(public_path($file->getPathOrg())))
|
||
|
|
return response()->download(public_path($file->getPathOrg()), $file->getReadableName());
|
||
|
|
|
||
|
|
return back();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// File Share Methods
|
||
|
|
|
||
|
|
public function indexShare($directory=null)
|
||
|
|
{
|
||
|
|
$directory = MyFile::where('user_id', auth()->user()->id)->where('id', base64_decode($directory))->first();
|
||
|
|
if($directory)
|
||
|
|
{
|
||
|
|
$shared_files = SharedFile::where('shared_by', auth()->user()->id)->where('file_id', $directory->id)->get();
|
||
|
|
return view('my-files.share-index')->with('directory', $directory)->with('shared_files', $shared_files);
|
||
|
|
}
|
||
|
|
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->back();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function createShare($directory)
|
||
|
|
{
|
||
|
|
if(isset($directory))
|
||
|
|
{
|
||
|
|
$directory = MyFile::where('user_id', auth()->user()->id)->where('id', base64_decode($directory))->first();
|
||
|
|
if($directory->user_id != auth()->user()->id)
|
||
|
|
return response()->json(['result'=>'error', 'data'=> __('We are having some problem. Please try later.')]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$users = User::where('status', 1)->where('working_status', 1)->get(['id', 'first_name', 'last_name']);
|
||
|
|
$departments = Department::where('status', 1)->get(['id', 'name']);
|
||
|
|
$view = view('my-files.share')
|
||
|
|
->with('directory', $directory)
|
||
|
|
->with('users', $users)
|
||
|
|
->with('departments', $departments)
|
||
|
|
->render();
|
||
|
|
return response()->json(['result' => 'success', 'data' => $view]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function share(Request $request)
|
||
|
|
{
|
||
|
|
$input = $request->all();
|
||
|
|
$file = MyFile::where('user_id', auth()->user()->id)->where('id', $input['id'])->first();
|
||
|
|
if($file)
|
||
|
|
{
|
||
|
|
$input['parent_id'] = $file->parent_id;
|
||
|
|
$input['shared_by'] = auth()->user()->id;
|
||
|
|
$input['file_type'] = $file->file_type;
|
||
|
|
$input['file_id'] = $file->id;
|
||
|
|
$input['path_to_root'] = $file->path_to_root;
|
||
|
|
|
||
|
|
$departments = (isset($input['departments'])) ? Department::whereIn('id', $input['departments'])->get() : [];
|
||
|
|
$exist_department_ids = [];
|
||
|
|
|
||
|
|
|
||
|
|
foreach($departments as $department)
|
||
|
|
{
|
||
|
|
if(SharedFile::validShare($input['file_id'], $department->id, 'department', $input['permission']))
|
||
|
|
{
|
||
|
|
$input['department_id'] = $department->id;
|
||
|
|
$saved_data=SharedFile::create($input);
|
||
|
|
}
|
||
|
|
$exist_department_ids[]=$department->id;
|
||
|
|
}
|
||
|
|
|
||
|
|
// $exist_department_ids[]=SharedFile::where('file_id', $file->id)->where('permission', $input['permission'])->whereNotNull('department_id')->pluck('department_id');
|
||
|
|
|
||
|
|
// $users = (isset($input['users'])) ? User::whereIn('id', $input['users'])->whereNotIn('department_id', $exist_department_ids)->get() : [];
|
||
|
|
$users = (isset($input['users'])) ? User::whereIn('id', $input['users'])->get() : [];
|
||
|
|
foreach($users as $user)
|
||
|
|
{
|
||
|
|
// if(!SharedFile::where('permission', $input['permission'])->where('user_id', $user->id)->where('file_id', $file->id)->exists())
|
||
|
|
if(SharedFile::validShare($input['file_id'], $user->id, 'user', $input['permission']))
|
||
|
|
{
|
||
|
|
$input['user_id'] = $user->id;
|
||
|
|
$saved_data=SharedFile::create($input);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(isset($saved_data))
|
||
|
|
{
|
||
|
|
Session::flash('success_message', __('My files has been added successfully'));
|
||
|
|
return redirect()->back();
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->back();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function deleteShare($share_id)
|
||
|
|
{
|
||
|
|
$shareid = base64_decode($share_id);
|
||
|
|
$shared = SharedFile::find($shareid);
|
||
|
|
|
||
|
|
if($shared)
|
||
|
|
$shared->delete();
|
||
|
|
|
||
|
|
Session::flash('success_message', __('My files has been added successfully'));
|
||
|
|
return redirect()->back();
|
||
|
|
}
|
||
|
|
|
||
|
|
// =================================
|
||
|
|
|
||
|
|
public function sharedFiles($share_id, $directory=null)
|
||
|
|
{
|
||
|
|
$parent_id = null;
|
||
|
|
$directories = [];
|
||
|
|
$shared_files = [];
|
||
|
|
$permission = '';
|
||
|
|
$write = false;
|
||
|
|
$sharedby='';
|
||
|
|
$prev_url = '';
|
||
|
|
$share_id = base64_decode($share_id);
|
||
|
|
if($directory !== null)
|
||
|
|
{
|
||
|
|
$parent=MyFile::where('id', base64_decode($directory))->first();
|
||
|
|
if($parent)
|
||
|
|
{
|
||
|
|
$shared = SharedFile::where('file_id', $parent->id)
|
||
|
|
->where(function($q){
|
||
|
|
$q->where('user_id', auth()->user()->id)
|
||
|
|
->orWhere('department_id', auth()->user()->department_id);
|
||
|
|
})
|
||
|
|
->first();
|
||
|
|
|
||
|
|
|
||
|
|
// dd($share_id . " -- " . 'null'. " -- ". $parent->id);
|
||
|
|
if($shared)
|
||
|
|
{
|
||
|
|
$directories = MyFile::where('parent_id', $parent->id)->get();
|
||
|
|
$write = ($shared->permission == 'Write') ? true : false;
|
||
|
|
$permission = __($shared->permission);
|
||
|
|
$sharedby = $shared->sharedby->getFullName();
|
||
|
|
$share_id = (($share_id == 0) && ($shared->id !=0)) ? $shared->file_id : $share_id;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
if(preg_match("/;".$share_id.";/", $parent->path_to_root))
|
||
|
|
{
|
||
|
|
$shared = SharedFile::where('file_id', $share_id)->first();
|
||
|
|
// dd($shared);
|
||
|
|
if($shared)
|
||
|
|
{
|
||
|
|
$directories = MyFile::where('parent_id', $parent->id)->get();
|
||
|
|
$write = ($shared->permission == 'Write') ? true : false;
|
||
|
|
$permission = __($shared->permission);
|
||
|
|
$sharedby = $shared->sharedby->getFullName();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$shared_files = SharedFile::where('show_home', 1)
|
||
|
|
->where(function($q){
|
||
|
|
$q->where('user_id', auth()->user()->id)
|
||
|
|
->orWhere('department_id', auth()->user()->department_id);
|
||
|
|
})
|
||
|
|
->orderBy('id', 'desc')
|
||
|
|
->get();
|
||
|
|
$prev_url = route('shared-files', ['share_id'=>base64_encode($share_id), 'directory'=>null]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$prev_url = route('shared-files', ['shared_id'=>base64_encode($share_id), 'directory'=>base64_encode($parent->parent_id)]);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$shared_files = SharedFile::where('show_home', 1)
|
||
|
|
->where(function($q){
|
||
|
|
$q->where('user_id', auth()->user()->id)
|
||
|
|
->orWhere('department_id', auth()->user()->department_id);
|
||
|
|
})
|
||
|
|
->orderBy('id', 'desc')
|
||
|
|
->get();
|
||
|
|
$prev_url = route('shared-files', ['share_id'=>base64_encode($share_id), 'directory'=>null]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$shared_files = SharedFile::where('show_home', 1)
|
||
|
|
->where(function($q){
|
||
|
|
$q->where('user_id', auth()->user()->id)
|
||
|
|
->orWhere('department_id', auth()->user()->department_id);
|
||
|
|
})
|
||
|
|
->orderBy('id', 'desc')
|
||
|
|
->get();
|
||
|
|
$prev_url = route('shared-files', ['share_id'=>base64_encode($share_id), 'directory'=>null]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$setting = Setting::first();
|
||
|
|
$parent_id = (isset($parent)) ? $parent->id : null;
|
||
|
|
|
||
|
|
// dd($parent_id);
|
||
|
|
// $users = User::getUserRole()->get();
|
||
|
|
// $departments = Department::where('status', 1)->orderBy('id', 'asc')->get();
|
||
|
|
return view('my-files.shared-files', compact('shared_files', 'directories', 'parent_id', 'prev_url', 'sharedby', 'permission', 'share_id', 'write', 'setting'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function sharedView($directory)
|
||
|
|
{
|
||
|
|
$directory = MyFile::find(base64_decode($directory));
|
||
|
|
if($directory)
|
||
|
|
{
|
||
|
|
$found = false;
|
||
|
|
$shares = SharedFile::where(function($q){$q->orWhere('department_id', auth()->user()->department_id)->orWhere('user_id', auth()->user()->id);})->where('show_home', 1)->get(['id', 'file_id']);
|
||
|
|
foreach($shares as $share)
|
||
|
|
{
|
||
|
|
if(preg_match("/;".$share->file_id.";/", $directory->path_to_root) || ($share->file_id ==$directory->id))
|
||
|
|
$found=true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if($found)
|
||
|
|
return redirect(route('view-my-files', base64_encode($directory->id)));
|
||
|
|
// return $this->view(base64_encode($directory->id));
|
||
|
|
}
|
||
|
|
return back();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function xxdeleteFiles(Request $request)
|
||
|
|
{
|
||
|
|
$input = $request->all();
|
||
|
|
$file = MyFile::findOrFail($input['id']);
|
||
|
|
$delete_file = $file->delete();
|
||
|
|
if($delete_file):
|
||
|
|
SharedFile::where('file_id', $file->id)->delete();
|
||
|
|
$user = User::where('id',Auth::user()->id)->first();
|
||
|
|
$user_new_disk_uses = $user->user_disk_uses + $file->file_size;
|
||
|
|
User::where('id',Auth::user()->id)->update(['user_disk_uses'=> $user_new_disk_uses]);
|
||
|
|
unlink(public_path($file->place_of_the_files.'/'.$file->files_name));
|
||
|
|
endif;
|
||
|
|
echo __('Deleted File ').$file->files_name;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function xxdeleteDirectory($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$directory = Directory::findOrFail($id);
|
||
|
|
foreach(Directory::where('parent_id', $id)->get() as $related_directory)
|
||
|
|
{
|
||
|
|
foreach(MyFile::where('directory_id', $related_directory->id)->get() as $myfile)
|
||
|
|
{
|
||
|
|
$file = MyFile::findOrFail($myfile->id);
|
||
|
|
$file->delete();
|
||
|
|
SharedFile::where('file_id', $myfile->id)->delete();
|
||
|
|
unlink(public_path($file->place_of_the_files.'/'.$file->files_name));
|
||
|
|
}
|
||
|
|
$child_directory = Directory::findOrFail($related_directory->id);
|
||
|
|
$child_directory->delete();
|
||
|
|
SharedFile::where('directory_id', $related_directory->id)->delete();
|
||
|
|
}
|
||
|
|
$delete_directory = $directory->delete();
|
||
|
|
SharedFile::where('directory_id', $directory->id)->delete();
|
||
|
|
if($delete_directory):
|
||
|
|
Session::flash('success_message', __('Directory has been deleted successfully'));
|
||
|
|
return redirect()->route('my-files');
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->route('my-files');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function xxdelete($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$file = MyFile::findOrFail($id);
|
||
|
|
$delete_file = $file->delete();
|
||
|
|
if($delete_file):
|
||
|
|
SharedFile::where('file_id', $file->id)->delete();
|
||
|
|
unlink(public_path($file->place_of_the_files.'/'.$file->files_name));
|
||
|
|
Session::flash('success_message', __('File has been deleted successfully'));
|
||
|
|
return redirect()->back();
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->back();
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function xxindex($directory=null)
|
||
|
|
{
|
||
|
|
$setting = Setting::first();
|
||
|
|
$parent_id = 0;
|
||
|
|
$parent_directories = [];
|
||
|
|
if($directory !== null)
|
||
|
|
{
|
||
|
|
$parent_id = base64_decode($directory);
|
||
|
|
$parent_directories = Directory::getPathArray($parent_id);
|
||
|
|
$last = array_key_last($parent_directories);
|
||
|
|
}
|
||
|
|
$directories=Directory::where('user_id', Auth::user()->id)->orderBy('id', 'desc')->get();
|
||
|
|
$prev_url = url()->previous();
|
||
|
|
|
||
|
|
$users = User::getUserRole()->get();
|
||
|
|
$departments = Department::where('status', 1)->orderBy('id', 'asc')->get();
|
||
|
|
return view('my-files.index', compact('setting', 'directories', 'parent_id', 'parent_directories', 'last', 'prev_url', 'users', 'departments'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function xxsharedFiles($directory=null)
|
||
|
|
{
|
||
|
|
$parent_id = 0;
|
||
|
|
$parent_directory = [];
|
||
|
|
if($directory !== null)
|
||
|
|
{
|
||
|
|
$parent_id = base64_decode($directory);
|
||
|
|
$parent_directory=Directory::where('id', $parent_id)->first();
|
||
|
|
}
|
||
|
|
$shared_files=SharedFile::where('parent_id', 0)->where(function($q){$q->where('user_id', Auth::user()->id)->orWhere('department_id', Auth::user()->department_id);})->orderBy('id', 'desc');
|
||
|
|
$shared_file_parent_ids = $shared_files->pluck('directory_id');
|
||
|
|
$shared_directories = SharedFile::where('parent_id', '!=', 0)->where(function($q){$q->where('user_id', Auth::user()->id)->orWhere('department_id', Auth::user()->department_id);})->orderBy('id', 'desc')->get();
|
||
|
|
$extra = [];
|
||
|
|
// dd($shared_directories);
|
||
|
|
foreach($shared_directories as $sh)
|
||
|
|
{
|
||
|
|
if($sh->type == "D")
|
||
|
|
{
|
||
|
|
$path_array = Directory::getPathToRoot($sh->directory_id);
|
||
|
|
$exist = false;
|
||
|
|
foreach($shared_file_parent_ids as $shf)
|
||
|
|
{
|
||
|
|
if(in_array($shf, $path_array))
|
||
|
|
{
|
||
|
|
$exist = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if($exist == false)
|
||
|
|
$extra[]=$sh->id;
|
||
|
|
}
|
||
|
|
|
||
|
|
if($sh->type == "F")
|
||
|
|
{
|
||
|
|
$path_array = MyFile::getPathToRoot($sh->parent_id, true);
|
||
|
|
$exist = false;
|
||
|
|
foreach($shared_file_parent_ids as $shf)
|
||
|
|
{
|
||
|
|
if(in_array($shf, $path_array))
|
||
|
|
{
|
||
|
|
$exist = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if($exist == false)
|
||
|
|
$extra[]=$sh->id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$shared_files = $shared_files->get();
|
||
|
|
if(count($extra)>0)
|
||
|
|
$shared_files = $shared_files->merge(SharedFile::whereIn('id', $extra)->get());
|
||
|
|
|
||
|
|
$setting = Setting::first();
|
||
|
|
$prev_url = url()->previous();
|
||
|
|
|
||
|
|
$users = User::getUserRole()->get();
|
||
|
|
$departments = Department::where('status', 1)->orderBy('id', 'asc')->get();
|
||
|
|
return view('my-files.shared-files', compact('setting', 'shared_files', 'parent_id', 'parent_directory', 'prev_url', 'users', 'departments'));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|