200 lines
6.1 KiB
PHP
200 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Banner;
|
|
use App\Models\Video;
|
|
use App\Models\VideoCategory;
|
|
use App\Traits\ApiResponseHelper;
|
|
use App\Traits\ImageHelper;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class VideoController extends Controller
|
|
{
|
|
use ApiResponseHelper, ImageHelper;
|
|
|
|
public function index()
|
|
{
|
|
$pageConfigs = [
|
|
'pageHeader' => true,
|
|
'showMenu' => true
|
|
];
|
|
|
|
$breadcrumbs = [['link' => '/', 'name' => 'Home'], ['name' => 'Videos']];
|
|
$createUrl = [['url' => 'create', 'name' => "Create", 'slug' => 'video']];
|
|
$videos = Video::all();
|
|
|
|
return view('content.videos.index', [
|
|
'breadcrumbs' => $breadcrumbs,
|
|
'pageConfigs' => $pageConfigs,
|
|
'createUrl' => $createUrl,
|
|
'videos' => $videos
|
|
]);
|
|
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
|
|
$pageConfigs = [
|
|
'pageHeader' => true,
|
|
'showMenu' => true
|
|
];
|
|
|
|
$breadcrumbs = [['link' => '/admin/video', 'name' => 'Videos'], ['name' => 'Add']];
|
|
|
|
$categories = VideoCategory::all();
|
|
if ($categories->count() == 0) {
|
|
return redirect()->back()->with(['error' => 'Please add a video category first.']);
|
|
}
|
|
return view('content.videos.create', [
|
|
'pageConfigs' => $pageConfigs,
|
|
'breadcrumbs' => $breadcrumbs,
|
|
'categories' => $categories
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'title' => ['required', 'min: 3'],
|
|
'category' => ['required'],
|
|
'type' => ['required', 'in:SERVER,YOUTUBE'],
|
|
'url' => ['required', 'url'],
|
|
'thumbnail' => ['nullable', 'image'],
|
|
|
|
]);
|
|
try {
|
|
|
|
// $videoExtensions = ['mp4', 'mkv', 'avi', 'flv','m3u8','hls'];
|
|
// if ($request->type == 'SERVER') {
|
|
// $checkPassed = false;
|
|
// $urlArray = explode(".", $request->url);
|
|
// if (!empty($urlArray)) {
|
|
// foreach ($urlArray as $urlArr) {
|
|
// if (in_array(strtolower($urlArr), $videoExtensions)) {
|
|
// $checkPassed = true;
|
|
// }
|
|
// }
|
|
// }
|
|
// if ($checkPassed == false) {
|
|
// return redirect()->back()->with([
|
|
// 'error' => 'Server video url is wrong'
|
|
// ]);
|
|
// }
|
|
// }
|
|
|
|
DB::beginTransaction();
|
|
$video = new Video();
|
|
$video->title = $request->title;
|
|
$video->video_category_id = $request->category;
|
|
$video->type = $request->type;
|
|
$video->url = $request->url;
|
|
$video->created_by = Auth::user()->role;
|
|
|
|
|
|
if ($request->thumbnail) {
|
|
$prefix = 'thumbnail';
|
|
$bannerImagePath = $this->uploadFile($request->thumbnail, $prefix, false);
|
|
$video->thumbnail = $bannerImagePath;
|
|
}
|
|
$video->save();
|
|
DB::commit();
|
|
return redirect()->route('video.index')->with([
|
|
'success' => "Video created successfully"
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
DB::rollBack();
|
|
return redirect()->back()->with([
|
|
'error' => $exception->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
public function edit(Video $video)
|
|
{
|
|
|
|
$pageConfigs = [
|
|
'pageHeader' => true,
|
|
'showMenu' => true
|
|
];
|
|
|
|
$breadcrumbs = [['link' => '/admin/video', 'name' => 'Videos'], ['name' => 'Edit']];
|
|
$categories = VideoCategory::all();
|
|
return view('content.videos.edit', [
|
|
'pageConfigs' => $pageConfigs,
|
|
'breadcrumbs' => $breadcrumbs,
|
|
'video' => $video,
|
|
'categories' => $categories
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Video $video)
|
|
{
|
|
$request->validate([
|
|
'title' => ['required', 'min: 3'],
|
|
'category' => ['required'],
|
|
'url' => ['required', 'url'],
|
|
'thumbnail' => ['nullable', 'image'],
|
|
|
|
]);
|
|
try {
|
|
|
|
if ($video->created_by == 'ADMIN' && auth()->user()->role != 'ADMIN') {
|
|
return redirect()->back()->with([
|
|
'error' => 'You do not have the permission to perform this task'
|
|
]);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
$video->title = $request->title;
|
|
$video->video_category_id = $request->category;
|
|
$video->url = $request->url;
|
|
|
|
if ($request->hasFile('thumbnail')) {
|
|
$prefix = 'thumbnail';
|
|
$bannerImagePath = $this->updateFile($request->thumbnail, $video->thumbnail, $prefix, false);
|
|
$video->thumbnail = $bannerImagePath;
|
|
}
|
|
$video->update();
|
|
DB::commit();
|
|
return redirect()->back()->with([
|
|
'success' => "Video updated successfully"
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
DB::rollBack();
|
|
return redirect()->back()->with([
|
|
'error' => $exception->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function destroy(Video $video)
|
|
{
|
|
try {
|
|
|
|
if ($video->created_by == 'ADMIN' && auth()->user()->role != 'ADMIN') {
|
|
return redirect()->back()->with([
|
|
'error' => 'You do not have the permission to perform this task'
|
|
]);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
$this->deleteFile($video->thumbnail);
|
|
$video->delete();
|
|
DB::commit();
|
|
return redirect()->back()->with([
|
|
'success' => "Video deleted successfully"
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
DB::rollBack();
|
|
return redirect()->back()->with([
|
|
'error' => $exception->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
}
|