paginate(6); $topheaderadvs = Topheaderadv::where('home', 1)->first(); return view('main')->with([ 'cat' => "", 'materials' => $materials, 'sort' => 'all', 'topheaderadvs' => $topheaderadvs, ]); } public function category($cat_id){ $request = \request(); $sort = $request['sort']; $sort = $sort ?? 'all'; $topheaderadvs = Topheaderadv::where('home', 1)->first(); if($cat_id != 0){ $cat = Category:: findOrFail($cat_id); $materials = $cat->materials(); } else{ $cat = ''; $materials = Material::query(); } switch ($sort){ case 'rate': $materials->orderBy('like','DESC'); break; case 'date': $materials->orderBy('created_at','ASC'); break; case 'all': $materials->orderBy('created_at','DESC'); } $materials = $materials->paginate(6); //dd($materials); return view('main')->with([ 'cat' => $cat, 'materials' => $materials, 'sort' => $sort, 'topheaderadvs' => $topheaderadvs, ]); } public function download($material_id){ //todo check limits $material = Material::findOrFail($material_id); //$topheaderadvs = Topheaderadv::where('home', 1)->first(); $order = Order::where('user_id',auth()->id()) ->where('material_id',$material_id) ->where('payed',1) ->first(); if(!empty($order)){ if(!($order->download_count !=0 && $order->downloaded == $order->download_count) ||(!empty($order->last_date) && !(Carbon::today()->lte($order->last_date)))) { $file = public_path($material->content_url); $ext = pathinfo($file, PATHINFO_EXTENSION); $file_name = Str::slug($material->title).'.'.$ext; $order->downloaded++; $order->save(); $headers = array( 'Content-Type: ' . mime_content_type( $file ), ); // dd($file_name,$headers); return response()->download($file, $file_name,$headers); } else{ request()->session()->flash('status','danger'); request()->session()->flash('status_message','Indirme wagtyňyz ýada gezegiňiz gutardy.'); redirect()->back(); } //todo else show expired message } return redirect()->back(); } public function material($material_id){ $material = Material::findOrFail($material_id); $topheaderadvs = Topheaderadv::where('home', 1)->first(); $liked = false; $order = null; if(auth()->guest()){ $watch_list_cookie = Cookie::get('watchlist'); if(!$watch_list_cookie){ $watch_list = [1 => $material_id]; Cookie::queue('watchlist', json_encode($watch_list), 450000); $material->view ++; $material->save(); } else{ $watch_list = json_decode($watch_list_cookie,true); if(!array_search($material_id,$watch_list)){ $watch_list[]=$material_id; $material->view ++; $material->save(); Cookie::queue('watchlist', json_encode($watch_list), 450000); } } } else{ $um = UserMaterial::firstOrCreate( ['user_id' => auth()->id(), 'material_id' => $material_id], ['watched'=>1, 'liked'=>0] ); if($um->wasRecentlyCreated){ $material->view++; $material->save(); } $liked = $um->liked; $order = Order::where('user_id',auth()->id()) ->where('material_id',$material_id) ->where('payed',1) ->first(); } // CategoryMaterial::where('material_id',$material_id) // ->update(['views'=> DB::raw('views + 1')]); if(!empty($order)){ if($order->download_count !=0 && $order->downloaded == $order->download_count){ $order = null; }elseif (!empty($order->last_date) && !(Carbon::today()->lte($order->last_date))){ $order = null; } } return view('material',compact('material')) ->with('liked',$liked) ->with('order',$order) ->with('topheaderadvs', $topheaderadvs); // ->with('cat',$material->category); } public function like($material_id){ $material = Material::findOrFail($material_id); //$topheaderadvs = Topheaderadv::where('home', 1)->first(); $um = UserMaterial::where([ 'user_id'=>auth()->id(), 'material_id' =>$material_id]) ->first(); if(!$um->liked) { $material->like++; $material->save(); $um->liked = true; $um->save(); } return $material->like; } public function watch_list(){ $materials = Material::join('user_materials','user_materials.material_id','materials.id') ->where('user_materials.user_id',auth()->id()) ->where('user_materials.watched',1) ->paginate(6); $topheaderadvs = Topheaderadv::where('home', 1)->first(); return view('watched',compact('materials')); } public function like_list(){ $sort = request('sort','high'); $topheaderadvs = Topheaderadv::where('home', 1)->first(); $materials = Material::join('user_materials','user_materials.material_id','materials.id') ->where('user_materials.user_id',auth()->id()) ->where('user_materials.liked',1); switch ($sort){ case 'high': $materials->orderBy('like','DESC'); break; case 'low': $materials->orderBy('like','ASC'); break; } $materials = $materials->paginate(6); return view('liked',compact('materials'))->with('sort',$sort); } public function orders_list(){ $topheaderadvs = Topheaderadv::where('home', 1)->first(); $orders = Order::where('user_id',auth()->id())->paginate(20); return view('orders',compact('orders')); } public function bought_list(){ $topheaderadvs = Topheaderadv::where('home', 1)->first(); $orders = Order::with('material') ->where('user_id',auth()->id()) ->where('payed',1) ->paginate(6); return view('bought',compact('orders')); } public function profile(){ $topheaderadvs = Topheaderadv::where('home', 1)->first(); // dd(route()->getName()); return view('profile')->with('user',auth()->user()); } public function profileUpdate(ProfileRequest $request){ $topheaderadvs = Topheaderadv::where('home', 1)->first(); $user = auth()->user(); $user->name = $request['name']; $user->phone = $request['phone']; $user->email = $request['email']; if ( ! $request->input('password') == '') { $user->password = bcrypt($request->input('password')); } $user->save(); return redirect()->back(); } public function search(){ $topheaderadvs = Topheaderadv::where('home', 1)->first(); $request = \request(); $key = $request['key']; if(empty($key)) return redirect()->back(); $sort = $request['sort']; $sort = $sort ?? 'high'; $materials = Material::where('title','like',"%{$key}%"); switch ($sort){ case 'new': $materials->orderBy('updated_at','DESC'); break; case 'old': $materials->orderBy('updated_at','ASC'); break; case 'like': $materials->orderBy('like','DESC'); break; case 'view': $materials->orderBy('view','DESC'); break; } $materials = $materials->paginate(6); return view('search') ->with('key',$key) ->with('materials',$materials) ->with('sort',$sort) ->with('topheaderadvs', $topheaderadvs); } }