kuvia/app/Http/Controllers/TenantController.php

168 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2021-01-12 14:49:45 +00:00
<?php
namespace App\Http\Controllers;
2021-01-27 00:38:17 +00:00
use App\Models\Gallery;
2021-01-20 20:25:29 +00:00
use App\Models\Image;
2021-01-12 14:49:45 +00:00
use App\Models\Tenant;
2021-01-27 00:11:16 +00:00
use App\Models\Theme;
2021-01-12 14:49:45 +00:00
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;
2021-01-20 21:35:41 +00:00
use Illuminate\Support\Facades\DB;
2021-01-12 14:49:45 +00:00
use Illuminate\Support\Facades\Redirect;
2021-01-20 20:59:42 +00:00
use Illuminate\Support\Facades\Storage;
2021-01-12 14:49:45 +00:00
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"];
2021-01-27 00:11:16 +00:00
$tenant->template = "kuvia-blog";
$tenant->gallery_default_theme = "kuvia-gallery";
2021-01-12 14:49:45 +00:00
$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]);
}
2021-01-20 20:25:29 +00:00
public function watermarkView() {
return view("tenant.watermark");
}
2021-01-20 21:35:41 +00:00
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");
}
2021-01-20 20:25:29 +00:00
public function watermarkSave(Request $request) {
$validated = $request->validate([
'files.0' => 'required|image'
]);
$path = $validated["files"][0]->store("watermark/".session("current_tenant_id"));
2021-01-20 20:59:42 +00:00
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
$oldImage = Image::query()->where("id", "=", $tenant->watermark)->first();
if(!is_null($oldImage)) {
2021-01-20 21:02:39 +00:00
Storage::disk($oldImage->driver)->delete($oldImage->path);
2021-01-20 20:59:42 +00:00
$oldImage->deleted_at = date("Y-m-d H:i:s");
$oldImage->saveOrFail();
}
2021-01-20 20:25:29 +00:00
$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";
2021-01-21 00:38:58 +00:00
$image->tenant = $tenant->id;
2021-01-20 20:25:29 +00:00
$image->saveOrFail();
2021-01-20 20:59:42 +00:00
2021-01-20 20:25:29 +00:00
$tenant->watermark = $image->id;
$tenant->saveOrFail();
2021-01-20 21:35:41 +00:00
DB::beginTransaction();
DB::statement("UPDATE images SET refreshCache = 1 WHERE gallery IN (SELECT `id` FROM `galleries` WHERE `tenant` = 1);");
DB::commit();
2021-01-20 20:43:58 +00:00
return \redirect("/s/watermark");
2021-01-20 20:25:29 +00:00
}
2021-01-27 00:11:16 +00:00
public function tenantThemeView() {
$themes = Theme::query()
->where("status", "=", "public")
->where("typ", "=", "tenant")
->get();
2021-01-27 00:38:17 +00:00
$gallery_themes = Theme::query()
->where("status", "=", "public")
->where("typ", "=", "gallery")
->get();
$gallery = Gallery::query()
->where("tenant", "=", session("current_tenant_id"))
->orderByDesc("id")
->first();
return view("tenant.theme", ["themes" => $themes, "gallery_themes" => $gallery_themes, "gallery" => $gallery]);
}
public function tenantThemeSet(Request $request) {
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
$theme = Theme::query()
->where("name", "=", $request->input("name"))
->where("typ", "=", "tenant")
->firstOrFail();
if($theme->status != "public") {
abort(400);
}
$tenant->template = $theme->name;
$tenant->saveOrFail();
return Redirect::back();
}
public function tenantGalleryDefaultTemplateSet(Request $request) {
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
$theme = Theme::query()
->where("name", "=", $request->input("name"))
->where("typ", "=", "gallery")
->firstOrFail();
if($theme->status != "public") {
abort(400);
}
$tenant->gallery_default_theme = $theme->name;
$tenant->saveOrFail();
return Redirect::back();
2021-01-27 00:11:16 +00:00
}
2021-01-12 14:49:45 +00:00
}