You've already forked lubo_comment_query
150 lines
5.6 KiB
PHP
150 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Programs;
|
|
use App\Models\ProgramVideos;
|
|
use App\Models\VideoComments;
|
|
use App\Models\VideoParts;
|
|
use App\Util\TimeUtil;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProgramConstructController extends BaseController
|
|
{
|
|
// region Views
|
|
public function index(Request $request) {
|
|
$status = $request->get("status", "0");
|
|
$query = Programs::query()->with(["appends", "video_pivots.video"])->where("status", $status, 0)->orderByDesc("created_at");
|
|
$programs = $query->paginate(10)->withQueryString();;
|
|
return view("program.construct.index", [
|
|
"programs"=>$programs,
|
|
]);
|
|
}
|
|
|
|
public function add(Request $request) {
|
|
return view("program.construct.create", [
|
|
"program"=>new Programs()
|
|
]);
|
|
}
|
|
|
|
public function from_comment(Request $request, VideoComments $comment) {
|
|
return redirect(route("program.construct.batch_add"))->withInput([
|
|
"bvid"=>$comment->video->bvid,
|
|
"content"=>$comment->content
|
|
]);
|
|
}
|
|
|
|
public function batch_add(Request $request) {
|
|
return view("program.construct.batch_add");
|
|
}
|
|
|
|
public function edit(Request $request, Programs $program) {
|
|
return view("program.construct.create", [
|
|
"program"=>$program
|
|
]);
|
|
}
|
|
// endregion
|
|
|
|
// region Form Submit
|
|
public function create(Request $request) {
|
|
$request->validate([
|
|
"name" => ["required"]
|
|
]);
|
|
$program = new Programs();
|
|
$createPayload = $request->only(["name", "difficulty", "desc"]);
|
|
$program->fill($createPayload);
|
|
$program->status = $request->post("status", 0);
|
|
$program->save();
|
|
return redirect(route("program.construct.edit", ["program"=>$program->id]));
|
|
}
|
|
|
|
public function batch_create(Request $request) {
|
|
$request->validate([
|
|
'bvid' => ['required'],
|
|
'content' => ['required'],
|
|
]);
|
|
$bvid = $request->post("bvid");
|
|
$content = $request->post("content");
|
|
$count = ProgramVideos::query()->where("video_bvid", "=", $bvid)->count();
|
|
if ($count > 0) {
|
|
return back()->withInput()->withErrors([
|
|
"bvid" => "该BVID下已有{$count}个节目关联,请手动添加"
|
|
]);
|
|
}
|
|
$regex = "/^p(?P<part>\d{1,2})[-# _:,)]+(?P<time>(0?1[::])?\d{1,3}[::]\d{1,2})\s*(?P<content>.+)$/ui";
|
|
$video_parts = VideoParts::query()->where("bvid", "=", $bvid)->get();
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach (explode("\n", $content) as $line) {
|
|
$match = [];
|
|
$match_count = preg_match($regex, $line, $match);
|
|
if ($match_count === 0) {
|
|
continue;
|
|
}
|
|
$time = $match["time"];
|
|
$time = str_replace(":", ":", $time);
|
|
while (substr_count($time, ":") < 2) {
|
|
$time = "0:".$time;
|
|
}
|
|
$program = new Programs();
|
|
$program->name = $match["content"];
|
|
$video_pivot = new ProgramVideos();
|
|
$video_pivot->video_bvid = $bvid;
|
|
$video_pivot->start_part = $match["part"];
|
|
$video_pivot->start_time = $time;
|
|
$video_pivot->stop_part = $match["part"];
|
|
if (sizeof($video_parts) > 0) {
|
|
/**
|
|
* 计算开始时间
|
|
* @var VideoParts $cur_part
|
|
*/
|
|
$cur_part = $video_parts->where("part_num", "=", $match["part"])->first();
|
|
if ($cur_part) {
|
|
// 根据标题名称,判断开始时间
|
|
$title = $cur_part->title;
|
|
$date_str = substr($title, 0, 13);
|
|
$base_time = Carbon::createFromFormat("Ymd_Hi", $date_str);
|
|
$start_time = Carbon::createFromFormat("H:i:s", $time);
|
|
$video_pivot->created_at = TimeUtil::calculate_program_time(
|
|
$base_time,
|
|
$start_time->secondsSinceMidnight()
|
|
);
|
|
if ($cur_part->duration->diffInMinutes($start_time) < 20) {
|
|
$video_pivot->stop_part += 1;
|
|
if ($video_pivot->stop_part > $video_parts->pluck("part_num")->max()) {
|
|
$video_pivot->stop_part = $video_parts->pluck("part_num")->max();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$program->save();
|
|
$program->video_pivots()->save($video_pivot);
|
|
}
|
|
DB::commit();
|
|
} catch (QueryException $e) {
|
|
DB::rollBack();
|
|
return back()->withInput()->withErrors([
|
|
"content" => $e->getMessage()
|
|
]);
|
|
}
|
|
return redirect(route("program.construct.list"));
|
|
}
|
|
|
|
public function submit(Request $request, Programs $program) {
|
|
$request->validate([
|
|
"name" => ["required"]
|
|
]);
|
|
$updatePayload = $request->only(["name", "difficulty", "desc"]);
|
|
$program->status = $request->post("status", 0);
|
|
$program->update($updatePayload);
|
|
return view("program.construct.create", [
|
|
"program"=>$program
|
|
]);
|
|
}
|
|
// endregion
|
|
}
|