kuvia/app/Http/Controllers/GalleryController.php
2021-01-12 20:45:20 +01:00

144 lines
5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Gallery;
use App\Models\Image;
use App\Models\Tenant;
use App\Rules\dateOrNull;
use App\Rules\urlOrNull;
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\Redirect;
use Illuminate\Support\Facades\Storage;
class GalleryController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function listView() {
$galleries = Gallery::query()->where("tenant", "=", session("current_tenant_id"))->orderByDesc("gallery_create_time")->orderByDesc("id")->get();
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
return view("gallery.index", ["galleries" => $galleries, "tenant" => $tenant]);
}
public function newView() {
return view("gallery.new");
}
public function newGallery(Request $request) {
$validated = $request->validate([
'name' => 'required|max:255',
'description' => 'max:15000000',
'url' => [new urlOrNull()],
'date' => [new dateOrNull()],
]);
//Check if Gallery Date is in the feature
if(!empty($validated["date"])) {
$t = new \DateTime($validated["date"]);
if($t > (new \DateTime())) {
return Redirect::back()->withErrors(["date" => "Gallery date can't be in the feature"])->withInput($validated);
}
}
if(empty($validated["url"])) {
$prepairdName = strtolower($validated["name"]);
$prepairdName = str_replace(" ", "-", $prepairdName);
$prepairdName = str_replace("ö", "oe", $prepairdName);
$prepairdName = str_replace("ü", "üe", $prepairdName);
$prepairdName = str_replace("ö", "oe", $prepairdName);
$prepairdName = str_replace("ß", "ss", $prepairdName);
while (strlen($prepairdName) < 8) {
$prepairdName = $prepairdName."0";
}
preg_match_all("@[a-z0-9\-]@", $prepairdName, $matches);
$newUrl = "";
foreach($matches[0] as $letter) {
$newUrl .= $letter;
}
$baseUrl = $newUrl;
$i = 1;
while (true) {
$r = Gallery::getByTenantAndUrl(session("current_tenant_id"), $newUrl);
if($r == null) {
break;
}
$newUrl = $baseUrl.$i;
$i++;
}
} else {
$newUrl = $validated["url"];
}
//Check if url is free
$galleryTMP = Gallery::getByTenantAndUrl(session("current_tenant_id"), $newUrl);
if(!empty($galleryTMP)) {
return Redirect::back()->withErrors(["url" => "URL is already used"])->withInput($validated);
}
$gallery = new Gallery();
$gallery->name = $validated["name"];
$gallery->url = $newUrl;
$gallery->description = $validated["description"];
$gallery->tenant = session("current_tenant_id");
if(!empty($validated["date"])) {
$gallery->gallery_create_time = $validated["date"];
} else {
$gallery->gallery_create_time = date("Y-m-d");
}
$gallery->saveOrFail();
return \redirect("/g/".$gallery->url."/upload");
}
public function imagesUploadView($name) {
return view("gallery.upload");
}
public function imageUpload($name, Request $request) {
$gallery = Gallery::getByTenantAndUrl(session("current_tenant_id"), $name);
$validated = $request->validate([
'files.0' => 'required|image'
]);
$path = $validated["files"][0]->store("uploads/".session("current_tenant_id")."/".$name);
$image = new Image();
$image->path = $path;
$image->driver = env('FILESYSTEM_DRIVER', 'local');
$image->filename = $validated["files"][0]->getClientOriginalName();
$image->gallery = $gallery->id;
$image->size = $validated["files"][0]->getSize();
$image->saveOrFail();
if(is_null($gallery->main_image)) {
$gallery->main_image = $image->id;
$gallery->saveOrFail();
}
return $name;
}
public function showImagesView($name) {
$gallery = Gallery::getByTenantAndUrl(session("current_tenant_id"), $name);
$tenant = Tenant::query()->where("id", "=", $gallery->tenant)->firstOrFail();
$images = Image::query()->where("gallery", "=", $gallery->id)->get();
return view("gallery.images", ["images" => $images, "gallery" => $gallery, "tenant" => $tenant]);
}
public function setDefault($name, Request $request) {
$gallery = Gallery::getByTenantAndUrl(session("current_tenant_id"), $name);
$gallery->main_image = $request->input("id");
$gallery->saveOrFail();
return "OK";
}
}