204 lines
6.4 KiB
PHP
204 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\ProfileRequest;
|
|
use App\Models\Category;
|
|
use App\Models\Material;
|
|
use App\Models\Order;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cookie;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected $mainCategories;
|
|
|
|
/**
|
|
* Show the application dashboard.
|
|
*
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
|
*/
|
|
public function index(){
|
|
$cat = Category::where('depth',1)
|
|
->orderBy('lft')->firstOrFail();
|
|
$materials = Material::where('category_id',$cat->id)->paginate(6);
|
|
return view('main')->with([
|
|
'cat' => $cat,
|
|
'materials' => $materials,
|
|
'sort' => 'all'
|
|
]);
|
|
}
|
|
|
|
public function category($cat_id){
|
|
//dd(\request());
|
|
$request = \request();
|
|
$sort = $request['sort'];
|
|
$sort = $sort ?? 'all';
|
|
//dd($request['sort']);
|
|
//todo restrict sort to be only {'all','rate','date'}
|
|
$cat = Category:: findOrFail($cat_id);
|
|
$materials = Material::where('category_id',$cat_id);
|
|
switch ($sort){
|
|
case 'rate':
|
|
$materials->orderBy('rating','DESC');
|
|
break;
|
|
case 'date':
|
|
$materials->orderBy('created_at','DESC');
|
|
break;
|
|
}
|
|
$materials = $materials->paginate(6);
|
|
//dd($materials);
|
|
return view('main')->with([
|
|
'cat' => $cat,
|
|
'materials' => $materials,
|
|
'sort' => $sort
|
|
]);
|
|
}
|
|
public function download($material_id){
|
|
//todo check limits
|
|
$material = Material::findOrFail($material_id);
|
|
|
|
$order = Order::where('user_id',auth()->id())
|
|
->where('material_id',$material_id)
|
|
// ->where('registered',1)//todo payed
|
|
->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);
|
|
//dd(public_path($material->trailer_url));
|
|
return response()->download($file, Str::slug($material->title));
|
|
}
|
|
}
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function material($material_id){
|
|
$material = Material::findOrFail($material_id);
|
|
$watch_list_cookie = Cookie::get('watchlist');
|
|
$material->view ++;
|
|
$material->save();
|
|
// dd($material->details);
|
|
if(!$watch_list_cookie){
|
|
$watch_list = [1 => $material_id];
|
|
Cookie::queue('watchlist', json_encode($watch_list), 450000);
|
|
|
|
}
|
|
else{
|
|
$watch_list = json_decode($watch_list_cookie,true);
|
|
//dd($watch_list);
|
|
if(!array_search($material_id,$watch_list)){
|
|
$watch_list[]=$material_id;
|
|
Cookie::queue('watchlist', json_encode($watch_list), 450000);
|
|
}
|
|
}
|
|
//todo update view count check cookie
|
|
// Cookie::queue('watchlist', json_encode([$material_id]), 450000);
|
|
// Cookie::queue('wishlist', $material_id, 450000);
|
|
// cookie()->forever('watchlist',$material_id,450000);
|
|
// dd(Cookie::get('watchlist'));
|
|
$order = Order::where('user_id',auth()->id())
|
|
->where('material_id',$material_id)
|
|
// ->where('registered',1)//todo payed
|
|
->first();
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
//dd($order);
|
|
return view('material',compact('material'))
|
|
->with('order',$order)
|
|
->with('cat',$material->category);
|
|
}
|
|
|
|
public function like($material_id){
|
|
//todo ajax post
|
|
$material = Material::findOrFail($material_id);
|
|
$like_list_cookie = Cookie::get('likelist');
|
|
|
|
if(!$like_list_cookie){
|
|
$like_list = [1 => $material_id];
|
|
Cookie::queue('likelist', json_encode($like_list), 450000);
|
|
$material->rate ++;
|
|
$material->save();
|
|
}
|
|
else{
|
|
$like_list = json_decode($like_list_cookie,true);
|
|
//dd($watch_list);
|
|
if(!array_search($material_id,$like_list)){
|
|
$like_list[]=$material_id;
|
|
$material->view ++;
|
|
$material->save();
|
|
Cookie::queue('watchlist', json_encode($like_list), 450000);
|
|
}
|
|
}
|
|
// return view('material',compact('material'))->with('cat',$material->category);
|
|
return $material->rate;
|
|
}
|
|
|
|
public function watch_list(){
|
|
$watch_list = json_decode(Cookie::get('watchlist'),true);
|
|
$materials = null;
|
|
if($watch_list){
|
|
$material_ids = array_values($watch_list);
|
|
$materials = Material::whereIn('id',$material_ids)->paginate(6);
|
|
}
|
|
return view('watched',compact('materials'));
|
|
}
|
|
|
|
public function like_list(){
|
|
$like_list = json_decode(Cookie::get('likelist'),true);
|
|
$materials = null;
|
|
if($like_list){
|
|
$material_ids = array_values($like_list);
|
|
$materials = Material::whereIn('id',$material_ids)->paginate(6);
|
|
}
|
|
return view('liked',compact($materials));
|
|
}
|
|
|
|
public function bought_list(){
|
|
$orders = Order::with('material')
|
|
->where('user_id',auth()->id())
|
|
// ->where('payed',0)
|
|
->paginate(6);
|
|
// dd($orders);/
|
|
//todo change payed true
|
|
return view('bought',compact('orders'));
|
|
}
|
|
|
|
|
|
public function profile(){
|
|
// dd(route()->getName());
|
|
return view('profile')->with('user',auth()->user());
|
|
}
|
|
|
|
public function profileUpdate(ProfileRequest $request){
|
|
$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();
|
|
}
|
|
}
|