You've already forked lubo_comment_query
							
							
		
			
				
	
	
		
			142 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			5.1 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\ProgramVideoUtil;
 | |
| use Illuminate\Database\Eloquent\Builder;
 | |
| 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) {
 | |
|         $keyword = $request->get("keyword", "");
 | |
|         $status = $request->get("status", "0");
 | |
|         $query = Programs::query()->with(["appends", "video_pivots.video"])->where("status", $status, 0)->orderByDesc("created_at");
 | |
|         if ($keyword) {
 | |
|             $keyword_split = explode(" ", $keyword);
 | |
|             foreach ($keyword_split as $_keyword) {
 | |
|                 $_keyword = trim($_keyword);
 | |
|                 if (mb_strlen($_keyword) > 0) {
 | |
|                     $query->where(function (Builder $_query) use ($_keyword) {
 | |
|                         $_query
 | |
|                             ->where("name", "like", "%{$_keyword}%")
 | |
|                             ->orWhere("difficulty", "like", "%{$_keyword}%")
 | |
|                             ->orWhere("desc", "like", "%{$_keyword}%");
 | |
|                     });
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         $programs = $query->paginate(10)->withQueryString();;
 | |
|         return view("program.construct.index", [
 | |
|             "keyword" => $keyword,
 | |
|             "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>(\d{1,2}[::])?\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 = trim($match["content"]);
 | |
|                 $video_pivot = new ProgramVideos();
 | |
|                 $video_pivot->video_bvid = $bvid;
 | |
|                 $video_pivot->start_part = empty($match["part"]) ? 1 : $match["part"];
 | |
|                 $video_pivot->start_time = $time;
 | |
|                 $program->save();
 | |
|                 $program->video_pivots()->save($video_pivot);
 | |
|             }
 | |
|             DB::commit();
 | |
|         } catch (QueryException $e) {
 | |
|             DB::rollBack();
 | |
|             return back()->withInput()->withErrors([
 | |
|                 "content" => $e->getMessage()
 | |
|             ]);
 | |
|         }
 | |
|         ProgramVideoUtil::fix_created_at_by_part_info($bvid, true);
 | |
|         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
 | |
| }
 |