turkmentv/app/Http/Controllers/HomeController.php

391 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProfileRequest;
use App\Models\Category;
use App\Models\Subscription;
use App\Models\User_sub;
use App\Models\UserMaterial;
use App\Models\Topheaderadv;
use App\Models\Material;
use Illuminate\Pagination\Paginator;
use App\Models\Order;
use App\Models\Webpage;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Str;
class HomeController extends Controller
{
public function index(){
$materials = Material::orderBy('created_at','DESC')->paginate(6);
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
$title = 'TurkmenTV';
$keywords = 'TurkmenTV';
$meta_description = 'TurkmenTV';
return view('main')->with([
'cat' => "",
'materials' => $materials,
'sort' => 'all',
'topheaderadvs' => $topheaderadvs,
'title' => $title,
'keywords' => $keywords,
'meta_description' => $meta_description
]);
}
public function category($cat_id){
$request = \request();
$sort = $request['sort'];
$sort = $sort ?? 'all';
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
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();
$subscribed = false;
$user = auth()->user();
if($user != null){
$user_check = User_sub::where('user_id',$user->id)->first();
if($user_check != null){
if($user_check->status == 1){
$time = Carbon::parse($user_check->end_time)->format('Y-m-d h:i');
if($time >= now()->format('Y-m-d h:i')){
$subscribed = true;
}
else{
$subscribed = false;
}
}
else{
$subscribed = false;
}
}
else{
$subscribed = false;
}
}
if($subscribed){
$file = public_path($material->content_url);
$ext = pathinfo($file, PATHINFO_EXTENSION);
$file_name = Str::slug($material->title).'.'.$ext;
$headers = array(
'Content-Type: ' . mime_content_type( $file ),
);
return response()->download($file, $file_name,$headers);
}
$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);
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
$liked = false;
$order = null;
$subscribed = false;
$user = auth()->user();
if($user != null){
$user_check = User_sub::where('user_id',$user->id)->first();
if($user_check != null){
if($user_check->status == 1){
$time = Carbon::parse($user_check->end_time)->format('Y-m-d h:i');
if($time >= now()->format('Y-m-d h:i')){
$subscribed = true;
}
else{
$subscribed = false;
}
}
else{
$subscribed = false;
}
}
else{
$subscribed = false;
}
}
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('subscribed',$subscribed)
->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('likes','likes.material_id','materials.id')
->where('likes.user_id',auth()->id())
->where('likes.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('likes','likes.material_id','materials.id')
->where('likes.user_id',auth()->id())
->where('likes.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 tarif(){
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
// dd(route()->getName());
//todo use joins here, $user_sub == null ??? -> error 500 on $user_sub->subscription_type
$user_sub = User_sub::with('subscription')->where('user_id',auth()->id())->first();
// $subscription = Subscription::find($user_sub->subscription_type);
return view('tarif')->with('user_sub',$user_sub);
}
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(){
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
$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);
}
public function subscriptions(){
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
$subscriptions = Subscription::all();
$title = "Subscriptions";
$keywords = "Subscriptions, Turkmen TV subscriptions";
$meta_description = "Subscribe for Turkmen TV";
return view('web.subscriptions')->with([
'topheaderadvs' => $topheaderadvs,
'subscriptions' => $subscriptions,
'title' => $title,
'keywords' => $keywords,
'meta_description' => $meta_description
]);
}
public function radio($id){
return view('web.radio')->with([
'title' => 'Turkmen Tv | Radio',
'keywords' => 'Radio.tm, turkmentv radio',
'meta_description' => 'Turkmentv.gov.tm - radio diňlemek',
'id' => $id
]);
}
public function webpage($id){
$page = Webpage::find($id);
return view('webpage',[
'title' => 'Turkmen Tv | ' . $page->title,
'keywords' => 'Turkmentv, turkmentv, turkmen telewideniye, Radio.tm, turkmentv radio',
'meta_description' => 'Turkmentv.gov.tm - '. $page->title,
'page' => $page
]);
}
}