116 lines
3.7 KiB
PHP
116 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Image;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class TenantController extends BaseController
|
|
{
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
public function newView() {
|
|
return view("tenant.new");
|
|
}
|
|
|
|
public function newTenant(Request $request) {
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|max:255',
|
|
'url' => 'required|unique:tenants|regex:/^[a-z0-9]{8,30}$/i'
|
|
]);
|
|
|
|
$tenant = new Tenant();
|
|
$tenant->name = $validated["name"];
|
|
$tenant->url = $validated["url"];
|
|
$tenant->template = "default";
|
|
$tenant->owner = Auth::user()->id;
|
|
$tenant->saveOrFail();
|
|
|
|
$this->switchTenantById($tenant->id);
|
|
|
|
|
|
return redirect("/d");
|
|
}
|
|
|
|
public function switchTenant($url, Request $request) {
|
|
$tenant = Tenant::getByUrl($url);
|
|
if($tenant->owner == Auth::id()) {
|
|
$this->switchTenantById($tenant->id);
|
|
return Redirect::back();
|
|
} else {
|
|
return Redirect::back()->with(["msg" => "No Access to given Tenant"]);
|
|
}
|
|
}
|
|
|
|
private function switchTenantById($id) {
|
|
session(["current_tenant_id" => $id]);
|
|
}
|
|
|
|
public function watermarkView() {
|
|
return view("tenant.watermark");
|
|
}
|
|
|
|
public function deleteWatermark() {
|
|
DB::beginTransaction();
|
|
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
|
|
$oldImage = Image::query()->where("id", "=", $tenant->watermark)->first();
|
|
if(!is_null($oldImage)) {
|
|
Storage::disk($oldImage->driver)->delete($oldImage->path);
|
|
$oldImage->deleted_at = date("Y-m-d H:i:s");
|
|
$oldImage->saveOrFail();
|
|
}
|
|
$tenant->watermark = null;
|
|
$tenant->saveOrFail();
|
|
|
|
DB::statement("UPDATE images SET refreshCache = 1 WHERE gallery IN (SELECT `id` FROM `galleries` WHERE `tenant` = 1);");
|
|
DB::commit();
|
|
return \redirect("/s/watermark");
|
|
}
|
|
|
|
public function watermarkSave(Request $request) {
|
|
$validated = $request->validate([
|
|
'files.0' => 'required|image'
|
|
]);
|
|
$path = $validated["files"][0]->store("watermark/".session("current_tenant_id"));
|
|
|
|
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
|
|
|
|
$oldImage = Image::query()->where("id", "=", $tenant->watermark)->first();
|
|
if(!is_null($oldImage)) {
|
|
Storage::disk($oldImage->driver)->delete($oldImage->path);
|
|
$oldImage->deleted_at = date("Y-m-d H:i:s");
|
|
$oldImage->saveOrFail();
|
|
}
|
|
|
|
|
|
$image = new Image();
|
|
$image->path = $path;
|
|
$image->driver = env('FILESYSTEM_DRIVER', 'local');
|
|
$image->filename = $validated["files"][0]->getClientOriginalName();
|
|
$image->gallery = null;
|
|
$image->size = $validated["files"][0]->getSize();
|
|
$image->typ = "watermark";
|
|
$image->tenant = $tenant->id;
|
|
$image->saveOrFail();
|
|
|
|
|
|
$tenant->watermark = $image->id;
|
|
$tenant->saveOrFail();
|
|
|
|
DB::beginTransaction();
|
|
DB::statement("UPDATE images SET refreshCache = 1 WHERE gallery IN (SELECT `id` FROM `galleries` WHERE `tenant` = 1);");
|
|
DB::commit();
|
|
|
|
return \redirect("/s/watermark");
|
|
}
|
|
}
|