route('login'); } $userLogged = User::where('id', '=', Auth::id())->get()->first(); $allNotifications = DB::table('notifications')->orderBy('id', 'desc')->limit('3')->get(); $siteData = SiteContext::current(); // the search form posts a query string, the old links still use the route segment $s = $s ?: request('s'); $query = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories') ->where('site_id', '=', session('siteID')); if($s){ $query->where('name', 'like', '%' . filter_var($s, FILTER_SANITIZE_STRING) . '%'); } $allCats = $query->orderBy('id', 'DESC')->paginate($perPage = 20, $columns = ['*'], $pageName = 'pg'); /** * How many posts use each category, in one grouped query over the page * being shown. Counting inside the loop would be a query per row. */ $usage = []; if($allCats->count()){ $usage = DB::connection('mysql_site_' . session('serverID'))->table('post_category') ->whereIn('category_id', $allCats->pluck('id')->all()) ->selectRaw('category_id, COUNT(*) as total') ->groupBy('category_id') ->pluck('total', 'category_id') ->all(); } return view('site.categories', ['userLogged' => $userLogged , 'allNotifications' => $allNotifications, 'topHref' => 'site', 'siteData' => $siteData, 'allCats' => $allCats, 'usage' => $usage, 's' => $s]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { if (Auth::check() === false) { Auth::logout(); return redirect()->route('login'); } $userLogged = User::where('id', '=', Auth::id())->get()->first(); $allNotifications = DB::table('notifications')->orderBy('id', 'desc')->limit('3')->get(); $siteData = SiteContext::current(); return view('site.categoryCreate', ['userLogged' => $userLogged , 'allNotifications' => $allNotifications, 'topHref' => 'site', 'siteData' => $siteData]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if (Auth::check() === false) { Auth::logout(); return redirect()->route('login'); } $siteData = SiteContext::current(); // the post editor creates categories in a modal and stays on the page $wantsJson = $request->expectsJson() || $request->ajax(); if(!$request->name){ if($wantsJson){ return response()->json(['success' => false, 'message' => __('site.page_post_cat_required')], 422); } return redirect()->route('site.posts.c.categories')->withInput()->with('error_name', __('site.page_post_cat_required')); } $slug = Str::slug(filter_var($request->name, FILTER_SANITIZE_STRING)); $name = filter_var($request->name, FILTER_SANITIZE_STRING); $existing = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories') ->where('site_id','=',session('siteID'))->where('slug','=',$slug)->get()->first(); if($existing){ if($wantsJson){ return response()->json(['success' => false, 'message' => __('site.page_post_cat_exists')], 409); } return redirect()->route('site.posts.c.categories')->withInput()->with('error_name', __('site.page_post_cat_exists')); } $id = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories')->insertGetId([ 'site_id' => session('siteID'), 'name' => $name, 'slug' => $slug, 'created_at' => NOW() ]); UserLog::write('post.category.create', ['entity' => 'post_category', 'entity_id' => $id, 'description' => $name]); if (isset($siteData) && $siteData->stream_port) { setLog($siteData->stream_port, 'Cadastrou uma categoria no site: '.$name.' / ID: '.$id, request()->ip()); } if($wantsJson){ return response()->json(['success' => true, 'id' => $id, 'name' => $name]); } return redirect()->route('site.posts.r.categories')->with('success', 'Categoria cadastrada com sucesso!'); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { if (Auth::check() === false) { Auth::logout(); return redirect()->route('login'); } if(!$id){ return redirect()->route('site.posts.r'); } $userLogged = User::where('id', '=', Auth::id())->get()->first(); $allNotifications = DB::table('notifications')->orderBy('id', 'desc')->limit('3')->get(); $siteData = SiteContext::current(); $dataCat = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories')->where('id','=',$id) ->where('site_id','=',session('siteID'))->get()->first(); return view('site.categoryEdit', ['userLogged' => $userLogged , 'allNotifications' => $allNotifications, 'topHref' => 'site', 'siteData' => $siteData, 'dataCat' => $dataCat]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { if (Auth::check() === false) { Auth::logout(); return redirect()->route('login'); } $siteData = SiteContext::current(); $wantsJson = $request->expectsJson() || $request->ajax(); if(!$request->name){ if($wantsJson){ return response()->json(['success' => false, 'message' => __('site.page_post_cat_required')], 422); } return redirect()->route('site.posts.c.categories')->withInput()->with('error_name', __('site.page_post_cat_required')); } $slug = Str::slug(filter_var($request->name, FILTER_SANITIZE_STRING)); $name = filter_var($request->name, FILTER_SANITIZE_STRING); /** * Scoped by site_id: the update ran on the id alone, so an id from * another site was writable from here. */ $current = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories') ->where('site_id','=',session('siteID'))->where('id','=',$id)->first(); if(!$current){ if($wantsJson){ return response()->json(['success' => false], 404); } return redirect()->route('site.posts.r.categories'); } $taken = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories') ->where('site_id','=',session('siteID'))->where('slug','=',$slug)->where('id','<>',$id)->exists(); if($taken){ if($wantsJson){ return response()->json(['success' => false, 'message' => __('site.page_post_cat_exists')], 409); } return redirect()->route('site.posts.u.categories', $id)->withInput()->with('error_name', __('site.page_post_cat_exists')); } DB::connection('mysql_site_' . session('serverID'))->table('posts_categories') ->where('site_id','=',session('siteID'))->where('id','=',$id)->update([ 'name' => $name, 'slug' => $slug ]); UserLog::write('post.category.update', ['entity' => 'post_category', 'entity_id' => $id, 'description' => $name]); if (isset($siteData) && $siteData->stream_port) { setLog($siteData->stream_port, 'Alterou uma categoria no site: '.$name.' / ID: '.$id, request()->ip()); } if($wantsJson){ return response()->json(['success' => true, 'id' => (int) $id, 'name' => $name]); } return redirect()->route('site.posts.r.categories')->with('success', 'Categoria atualizada com sucesso!'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { if(Auth::check() === false){ Auth::logout(); return redirect()->route('login'); } $verifyPost = DB::connection('mysql_site_' . session('serverID'))->table('posts_categories')->where('site_id','=',session('siteID'))->where('id','=',$id)->get(); if(!$verifyPost->count() || !$id){ return redirect()->route('site.posts.r'); } DB::connection('mysql_site_' . session('serverID'))->table('posts_categories')->where('id','=',$id)->delete(); // the links outlived the category, leaving posts pointing at nothing DB::connection('mysql_site_' . session('serverID'))->table('post_category')->where('category_id','=',$id)->delete(); $siteData = SiteContext::current(); $name = $verifyPost[0]->name ?? 'Sem informação'; UserLog::write('post.category.delete', ['entity' => 'post_category', 'entity_id' => $id, 'description' => $name]); if (isset($siteData) && $siteData->stream_port) { setLog($siteData->stream_port, 'Removeu uma categoria no site: '.$name.' / ID: '.$id, request()->ip()); } return redirect()->route('site.posts.r.categories')->with('success', 'Categoria removida com sucesso!'); } }