turkmentv/app/Http/Controllers/HomeController.php

380 lines
13 KiB
PHP
Raw Normal View History

2019-05-11 00:27:49 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProfileRequest;
use App\Models\Category;
2020-03-23 06:25:14 +00:00
use App\Models\Subscription;
use App\Models\User_sub;
2019-07-23 10:42:23 +00:00
use App\Models\UserMaterial;
2019-08-23 12:27:39 +00:00
use App\Models\Topheaderadv;
2019-05-11 00:27:49 +00:00
use App\Models\Material;
2021-05-24 12:16:50 +00:00
use Illuminate\Pagination\Paginator;
2019-05-11 00:27:49 +00:00
use App\Models\Order;
use Carbon\Carbon;
2021-05-24 11:57:59 +00:00
use Illuminate\Http\Request;
2019-05-11 00:27:49 +00:00
use Illuminate\Support\Facades\Cookie;
2019-05-11 08:50:45 +00:00
use Illuminate\Support\Str;
2019-05-11 00:27:49 +00:00
class HomeController extends Controller
{
2019-05-22 12:49:28 +00:00
2021-05-24 12:25:50 +00:00
public function index(){
2021-05-24 11:59:13 +00:00
$materials = Material::orderBy('created_at','DESC')->paginate(6);
2020-03-23 06:25:14 +00:00
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
2020-06-07 16:08:16 +00:00
$title = 'TurkmenTV';
$keywords = 'TurkmenTV';
$meta_description = 'TurkmenTV';
2021-05-24 11:46:37 +00:00
2019-05-11 00:27:49 +00:00
return view('main')->with([
2019-06-26 05:45:32 +00:00
'cat' => "",
2019-05-11 00:27:49 +00:00
'materials' => $materials,
2019-08-23 12:27:39 +00:00
'sort' => 'all',
2019-09-03 08:01:43 +00:00
'topheaderadvs' => $topheaderadvs,
2020-06-07 16:08:16 +00:00
'title' => $title,
'keywords' => $keywords,
'meta_description' => $meta_description
2019-05-11 00:27:49 +00:00
]);
}
2021-05-24 10:46:35 +00:00
public function category($cat_id){
$request = \request();
2019-05-11 00:27:49 +00:00
$sort = $request['sort'];
$sort = $sort ?? 'all';
2020-03-23 06:25:14 +00:00
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
2019-07-11 06:28:46 +00:00
2019-06-26 05:45:32 +00:00
if($cat_id != 0){
2021-05-24 11:46:37 +00:00
$cat = Category:: findOrFail($cat_id);
$materials = $cat->materials();
2019-06-26 05:45:32 +00:00
}
else{
$cat = '';
$materials = Material::query();
}
2019-05-11 00:27:49 +00:00
switch ($sort){
case 'rate':
2021-05-24 11:46:37 +00:00
$materials->orderBy('like','DESC');
2019-05-11 00:27:49 +00:00
break;
case 'date':
2021-05-24 11:46:37 +00:00
$materials->orderBy('created_at','ASC');
2019-05-11 00:27:49 +00:00
break;
2019-07-11 06:28:46 +00:00
case 'all':
2021-05-24 11:46:37 +00:00
$materials->orderBy('created_at','DESC');
2019-05-11 00:27:49 +00:00
}
2021-05-24 11:46:37 +00:00
$materials = $materials->paginate(6);
//dd($materials);
2019-05-11 00:27:49 +00:00
return view('main')->with([
'cat' => $cat,
'materials' => $materials,
2019-08-24 09:36:28 +00:00
'sort' => $sort,
2019-09-03 08:37:57 +00:00
'topheaderadvs' => $topheaderadvs,
2019-05-11 00:27:49 +00:00
]);
}
2019-06-26 05:45:32 +00:00
2019-05-11 00:27:49 +00:00
public function download($material_id){
//todo check limits
$material = Material::findOrFail($material_id);
2019-09-03 08:37:57 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2020-03-27 01:15:31 +00:00
$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);
}
2019-05-11 00:27:49 +00:00
$order = Order::where('user_id',auth()->id())
->where('material_id',$material_id)
2019-06-01 00:08:36 +00:00
->where('payed',1)
2019-05-11 00:27:49 +00:00
->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 ),
);
2019-09-09 09:53:22 +00:00
// dd($file_name,$headers);
2019-09-09 09:40:08 +00:00
return response()->download($file, $file_name,$headers);
2019-05-11 00:27:49 +00:00
}
else{
request()->session()->flash('status','danger');
2019-07-23 10:42:23 +00:00
request()->session()->flash('status_message','Indirme wagtyňyz ýada gezegiňiz gutardy.');
redirect()->back();
}
//todo else show expired message
2019-05-11 00:27:49 +00:00
}
2019-05-11 00:27:49 +00:00
return redirect()->back();
}
public function material($material_id){
$material = Material::findOrFail($material_id);
2020-03-23 06:25:14 +00:00
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
2019-07-23 10:42:23 +00:00
$liked = false;
$order = null;
2020-03-23 06:25:14 +00:00
$subscribed = false;
$user = auth()->user();
if($user != null){
$user_check = User_sub::where('user_id',$user->id)->first();
2020-03-27 00:11:19 +00:00
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;
}
2020-03-26 15:29:46 +00:00
}
else{
$subscribed = false;
}
2020-03-23 06:25:14 +00:00
}
2020-03-27 00:11:19 +00:00
else{
$subscribed = false;
}
2020-03-23 06:25:14 +00:00
}
2019-07-23 10:42:23 +00:00
if(auth()->guest()){
$watch_list_cookie = Cookie::get('watchlist');
2019-05-13 11:40:38 +00:00
2019-07-23 10:42:23 +00:00
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);
}
}
2019-05-11 00:27:49 +00:00
}
else{
2019-07-23 10:42:23 +00:00
$um = UserMaterial::firstOrCreate(
['user_id' => auth()->id(), 'material_id' => $material_id],
['watched'=>1, 'liked'=>0]
);
if($um->wasRecentlyCreated){
2019-08-06 10:01:35 +00:00
$material->view++;
2019-07-23 10:42:23 +00:00
$material->save();
2019-05-11 00:27:49 +00:00
}
2019-07-23 10:42:23 +00:00
$liked = $um->liked;
$order = Order::where('user_id',auth()->id())
->where('material_id',$material_id)
->where('payed',1)
->first();
2019-05-11 00:27:49 +00:00
}
2019-05-13 11:40:38 +00:00
2019-07-23 10:42:23 +00:00
// CategoryMaterial::where('material_id',$material_id)
// ->update(['views'=> DB::raw('views + 1')]);
2019-05-11 00:27:49 +00:00
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'))
2019-05-13 11:40:38 +00:00
->with('liked',$liked)
2019-09-03 08:37:57 +00:00
->with('order',$order)
2020-03-23 06:25:14 +00:00
->with('subscribed',$subscribed)
2019-09-03 08:37:57 +00:00
->with('topheaderadvs', $topheaderadvs);
2020-04-20 08:23:10 +00:00
2019-07-10 13:47:00 +00:00
// ->with('cat',$material->category);
2019-05-11 00:27:49 +00:00
}
public function like($material_id){
2019-07-23 10:42:23 +00:00
$material = Material::findOrFail($material_id);
2019-09-03 08:37:57 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2019-07-23 10:42:23 +00:00
$um = UserMaterial::where([
'user_id'=>auth()->id(),
'material_id' =>$material_id])
->first();
if(!$um->liked)
{
$material->like++;
2019-05-11 00:27:49 +00:00
$material->save();
2019-07-23 10:42:23 +00:00
$um->liked = true;
$um->save();
2019-05-11 00:27:49 +00:00
}
2019-05-13 11:40:38 +00:00
return $material->like;
2019-05-11 00:27:49 +00:00
}
public function watch_list(){
2020-04-20 08:23:10 +00:00
$materials = Material::join('likes','likes.material_id','materials.id')
->where('likes.user_id',auth()->id())
->where('likes.watched',1)
2019-07-23 10:42:23 +00:00
->paginate(6);
2020-03-23 06:25:14 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2019-05-11 00:27:49 +00:00
return view('watched',compact('materials'));
2020-04-20 08:23:10 +00:00
2019-05-11 00:27:49 +00:00
}
public function like_list(){
2019-07-23 10:42:23 +00:00
$sort = request('sort','high');
2020-03-23 06:25:14 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2020-04-20 08:23:10 +00:00
$materials = Material::join('likes','likes.material_id','materials.id')
->where('likes.user_id',auth()->id())
->where('likes.liked',1);
2019-07-23 10:42:23 +00:00
switch ($sort){
case 'high':
$materials->orderBy('like','DESC');
break;
case 'low':
$materials->orderBy('like','ASC');
break;
2019-05-11 00:27:49 +00:00
}
2019-07-23 10:42:23 +00:00
$materials = $materials->paginate(6);
2019-05-13 11:40:38 +00:00
return view('liked',compact('materials'))->with('sort',$sort);
2019-05-11 00:27:49 +00:00
}
2019-07-10 14:16:29 +00:00
public function orders_list(){
2020-03-23 06:25:14 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2019-07-10 14:16:29 +00:00
$orders = Order::where('user_id',auth()->id())->paginate(20);
return view('orders',compact('orders'));
}
2019-05-11 00:27:49 +00:00
public function bought_list(){
2020-03-23 06:25:14 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2019-05-11 00:27:49 +00:00
$orders = Order::with('material')
->where('user_id',auth()->id())
2019-06-18 20:27:42 +00:00
->where('payed',1)
2019-05-11 00:27:49 +00:00
->paginate(6);
2019-06-18 20:27:42 +00:00
2019-05-11 00:27:49 +00:00
return view('bought',compact('orders'));
}
public function profile(){
2020-03-23 06:25:14 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2019-05-11 00:27:49 +00:00
// dd(route()->getName());
return view('profile')->with('user',auth()->user());
}
2020-03-27 01:03:13 +00:00
public function tarif(){
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
// dd(route()->getName());
2020-04-20 10:05:23 +00:00
//todo use joins here, $user_sub == null ??? -> error 500 on $user_sub->subscription_type
2020-03-27 01:03:13 +00:00
$user_sub = User_sub::with('subscription')->where('user_id',auth()->id())->first();
2020-04-20 12:38:59 +00:00
// $subscription = Subscription::find($user_sub->subscription_type);
2020-04-20 12:43:27 +00:00
return view('tarif')->with('user_sub',$user_sub);
2020-03-27 01:03:13 +00:00
}
2019-05-11 00:27:49 +00:00
public function profileUpdate(ProfileRequest $request){
2020-03-23 06:25:14 +00:00
//$topheaderadvs = Topheaderadv::where('home', 1)->first();
2019-05-11 00:27:49 +00:00
$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();
}
2019-05-13 11:40:38 +00:00
public function search(){
2020-03-23 06:25:14 +00:00
$topheaderadvers = Topheaderadv::inRandomOrder()->get();
$topheaderadvs = $topheaderadvers[0];
2019-05-13 11:40:38 +00:00
$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)
2019-09-03 08:37:57 +00:00
->with('sort',$sort)
->with('topheaderadvs', $topheaderadvs);
2019-05-13 11:40:38 +00:00
}
2020-03-23 06:25:14 +00:00
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
]);
}
2021-05-05 07:29:10 +00:00
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
]);
}
2019-05-11 00:27:49 +00:00
}