?
Current File : /home/sanghavirealty/public_html/appazwer/Http/Controllers/BlogController.php
<?php

namespace App\Http\Controllers;

use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

use App\Models\Blog;
use App\Models\Product;
use App\Models\Rating;
use App\Models\Comment;
use App\Models\Categorybeta;
use App\Models\Slugemit;
use App\Models\Banner;

use App\Models\Wishlist;

use App\Models\Widget;
use App\Models\WidgetSetting;



class BlogController extends Controller
{


  
  function findPostReset(){

    session()->forget('keywordpostvalue');
    return redirect('/');

  }


function findPost(Request $req){

  $req_sanitize = Blog::XSSSanitize($req) ;  

  if(!empty($req->keywordpostvalue)){

    $keywordpostvalue = $req_sanitize->keywordpostvalue ?? '' ;
    session()->put('keywordpostvalue', $keywordpostvalue);
    $keywordpostvalue = Session::get('keywordpostvalue');
  
    }else if(empty($req->keywordpostvalue)){
  
     session()->forget('keywordpostvalue');
     $keywordpostvalue = '' ;
     session()->put('keywordpostvalue', $keywordpostvalue);
     $keywordpostvalue = Session::get('keywordpostvalue');
    }else{
  
     $keywordpostvalue = '' ;
  
    
    }


    if($req->has('_token')     OR Session::has('keywordpostvalue') ) {

  // $scat = array(['0']) ;
  $skword = session()->get('keywordpostvalue');


  // $ResultDetails = "You are searching for :".$skword.','.$stype.','.$scity.','.$smprice.','.$smxprice.','$scat[] ;
  // var_dump($scat);


$query = BLOG::query();

$query->Where('currentstatus','0') ;




// if(!empty($scity)){ 

//   $query->Where('location', $scity);
//   // echo 'cityid='.$cityid ;
//   }



if(!empty($skword)){

  // echo 'keywordValye='.$keywordvalue ;

  $query->Where('title', 'LIKE' , "%{$skword}%");
  $query->orWhere('description', 'LIKE' , "%{$skword}%");
  $query->orWhere('subtitle', 'LIKE' , "%{$skword}%");
  $query->orWhere('ctaurltitle', 'LIKE' , "%{$skword}%");
}

$query->with('getCategorybetaTitle');

$query->orderBy('id', 'ASC');

$post = $query->select('id','parentid','title','categoryid')->paginate(6);
// $post = $query->select('id','parentid','titel','categoryid')->tosql();
// $post = $query->tosql();
// print_r($post);
// ->paginate(6);

// dd($post);

// foreach ($post as $list){
//   echo('<br>');
//   echo($list->id);
//   echo('-');
//   echo($list->parentid);

//   echo('_____________<br>');

// }


$ResultDetails = collect(['keyword'=>$skword]);


// ->WHEREIn('categories_id', '=' , $cat_array_id)

}  else {

// echo 'default loading';

$post = BLOG::Where('currentstatus','0')
->with('getCategorybetaTitle')
->paginate(6);

$cityid = Session::get('defaultCityValue');

$ResultDetails=collect(['city'=>$cityid]);
 


}

$parentArray =array()  ;



foreach( $post as $postlist){

  $id = $postlist->id;

//  print_r($id.'-');

  if($postlist->parentid != 0) {

    $id = $postlist->parentid;
    // print_r($id.'-');
   
  }
  array_push($parentArray,$id);
}

// dd($parentArray);

$query1 = BLOG::query();
$query1->Where('currentstatus','0') ;
$query1->WhereIN('id', $parentArray);
$query1->with('getCategorybetaTitle');
$query1->orderBy('id', 'ASC');
$finalpost = $query1->paginate(6);



return view('blog.category',[
  'posts' => $finalpost,


 
]);



}






  function getallCategory(){ 

    $post = Blog::Where('currentstatus','0')
    ->Where('parentid','0')
    ->with('getCategorybetaTitle')
    ->orderBy('id', 'DESC')
    ->paginate(12);
    
    $insideBanner = Banner::ShowData('InsideSponsorBanner');
    
    
      return view('blog.category',[
        'posts' => $post,
        'insideBanner'=>$insideBanner,
     
     
       
      ]);
    
    
    
    }


    function getInsideCategoryView ($slug){

      $getCategoryDetail = Categorybeta::Where('slug',$slug)->firstOrFail();
      $getInsideCategory = Categorybeta::Where('parentid',$getCategoryDetail->id)->get();
      $getListofCategories = $getInsideCategory->push($getCategoryDetail)->pluck('id')->toArray();

      $insideBanner = Banner::ShowData('InsideSponsorBanner');
      
      $post = Blog::Where('currentstatus','0')
              ->Where('parentid','0')
              ->whereIn('categoryid',$getListofCategories)
              ->with('getCategorybetaTitle')
              // ->with('getAuthorMemberTitle')
              ->orderBy('id', 'DESC')
              ->paginate(12);
    

      return view('blog.category',[
                'posts' => $post,
                'getInsideCategory'=>$getInsideCategory,
                'insideBanner'=>$insideBanner
                ]);


    }





    function showBlockData ($slug) {

      $getUserID = Auth::user()->id ;

      if(is_numeric($slug))
      {

        $getBlockParentData = Blog::Where('id', $slug)->first();

      }else{

        abort('403') ;

      }


     

   
         
      $dataviewer = Blog::WHERE('currentstatus','0')
       ->Where('parentid',$slug)
->with('getWidgetBlockType')
       ->orderBy('id', 'DESC')
      ->paginate(25);

      $parentid = $slug ;
 
    
      return view('blog.blockpagemanager',[
        
        'data'=>$dataviewer,
        'parentid'=>$parentid,
        'getBlockParentData'=>$getBlockParentData
    
    
    ]);







    }


    // function findProductCategory($slug){



    //   $getCategoryDetail = Categories::Where('slug',$slug)->firstOrFail();

    //   $post = Blog::Where('currentstatus','0')
    //             ->Where('categories_id',$getCategoryDetail->id)
    //             ->paginate(12);


    //   // return view('product.category',[
    //   //   'products' => $product,
  
 
    //     return view('blog.category',[
    //       'getCategoryDetail' => $getCategoryDetail,
    //       'posts' => $post,
    //       'slug'=>$slug
       
         
    //     ]);
  
  
  
    // }

   
    
function displayPost($slug){


  $gettheVideo = Widget::ShowValue(30);


  $dataviewer = Blog::WHERE('currentstatus','0')
  ->Where('slug',$slug)
  ->Where('parentid','0')
  ->with('getCategorybetaTitle')

  ->first();




  // $currentRating = Rating::Where('post_id',$dataviewer->id)
  //                   ->orderBy('id', 'desc')
  //                   ->first();

  // $currentCount = Rating::Where('post_id',$dataviewer->id)
  //                   ->orderBy('id', 'desc')
  //                   ->count();


$similarContent = Blog::WHERE('currentstatus','0')
                  ->Where('categoryid',$dataviewer->categoryid)
                  ->whereNotIn('id',[$dataviewer->id])
                  ->orderBy('id', 'desc') 
                  ->take(5)
                  ->get();



                  $keywordCloud = explode(',' , $dataviewer->keyword) ;

                  




  if($dataviewer->productfeatured){
    $productData = Product::featuredProductGrid($dataviewer->productfeatured);
  }else{
    $productData = [];
  }

  // $productData = [] ;
  // if(!empty($dataviewer->productfeatured)) {
  // $featuredproduct = explode(',' , $dataviewer->productfeatured) ;
  // $productData = Product::findMany([$featuredproduct]);
  // }

  $postid = $dataviewer->id ;
  // if(!empty($dataviewer->id)){

  //   $postid = $dataviewer->id ;
  //   // ->with('getProductRelation')

  //   $commentLoop = Comment::WHERE('currentstatus','0')
  //   ->WHERE('parentid' , '0' )
  //   ->WHERE('blog_id' , $dataviewer->id )
  //   ->with('replyComment.replyComment')
  //   ->with('getCommentMemberTitle.getCommentMemberTitle')
  //   ->get();

   


  $loopviewer = Blog::WHERE('currentstatus','0')
  ->Where('parentid',$postid)
  ->orderBy('orderby', 'ASC')
  ->get();


//   } else {

// abort(403);


//   }


if(Auth::Check()) {

  $getUserID = Auth::user()->id ;
  $postid = $dataviewer->id ;

  // $isThiswhishlist = Wishlist::Where('userid',$getUserID)
  //                    ->Where('productid',$postid)
  //                    ->count();

}else{

$isThiswhishlist = 'User Need To Log In' ;

}



 




  return view('blog.postpage',[
    'gettheVideo'=>$gettheVideo,
    'data'=>$dataviewer,
    'loopdata'=>$loopviewer,
    'productdata'=> $productData,
    // 'commentLoop'=>$commentLoop,
 
    // 'currentCount'=>$currentCount,
    'keywordCloud'=>$keywordCloud,
    'similarContent'=>$similarContent,
    // 'isThiswhishlist'=>$isThiswhishlist
  
  ]);



}


    function showData(){

        $getUserID = Auth::user()->id ;
         
        $dataviewer = Blog::WHERE('currentstatus','0')
        ->Where('parentid','0')
         ->with('getCategorybetaTitle')
         ->orderBy('id', 'DESC')
        ->paginate(20);

   
      
        return view('blog.pagemanager',['data'=>$dataviewer]);
        
        }

        function blogBlockCreate ($slug = 0){

          $widgetContent = Widget::ShowData('CMSType');

          if(!empty($slug)){

              $dataviewer = Blog::find($slug) ;  
          
              } else {
          
                $dataviewer = 0 ;
              }



          return view('blog.createblogblock',[
                
            'data'=>$dataviewer ,
            'typeData' => $widgetContent
          
          
          ]);


        }



 function updateBlockUpdate(Request $req){


$getUserID = Auth::user()->id ;


if(empty($req->masterid)){



Blog::insert([
 
  'typeid'=>$req->type,
  'orderby'=>$req->orderby,
  'parentid'=>$req->parentid,
  'userid'=>$getUserID,
  'createby'=>$getUserID,
  'createdate'=>Carbon::Now(),
  'createip'=>$req->ip()
]);

$getBlockData =  Blog::WHERE('currentstatus','0')
->WHERE('parentid',$req->parentid)
->WHERE('typeid',$req->type)
->orderBy('id', 'DESC')
->first();




$getBlockID = $getBlockData->id ;

}else{


  $getBlockID = $req->masterid ;


}

$PostUpdate = Blog::find($getBlockID) ; 

$PostUpdate->modifyby=$getUserID;
$PostUpdate->modifydate=Carbon::Now();
$PostUpdate->modifyip=$req->ip();


switch ($req->type) {


  case "84":
    $PostUpdate->productfeatured=$req->productfeatured;
    $PostUpdate->orderby=$req->orderby;
    $PostUpdate->isDirty('productfeatured');
    $PostUpdate->isDirty('orderby');
    break;


    case "86":
      $PostUpdate->quote=$req->quote;
      $PostUpdate->orderby=$req->orderby;
      $PostUpdate->isDirty('quote');
      $PostUpdate->isDirty('orderby');
      break;

      case "87":
        $PostUpdate->quote=$req->quote;
        $PostUpdate->orderby=$req->orderby;
        $PostUpdate->isDirty('quote');
        $PostUpdate->isDirty('orderby');
        break;

  case "88":
    $PostUpdate->productfeatured=$req->productfeatured;
    $PostUpdate->orderby=$req->orderby;
    $PostUpdate->isDirty('productfeatured');
    $PostUpdate->isDirty('orderby');
    break;

 

    case "90":
      $PostUpdate->title=$req->title;
      $PostUpdate->subtitle=$req->subtitle;
      $PostUpdate->ctaurltitle=$req->ctaurltitle;
      $PostUpdate->ctaurl=$req->ctaurl;
      $PostUpdate->orderby=$req->orderby;
      $PostUpdate->isDirty('title');
      $PostUpdate->isDirty('subtitle');
      $PostUpdate->isDirty('ctaurltitle');
      $PostUpdate->isDirty('ctaurl');
      $PostUpdate->isDirty('orderby');
      break;


      case "91":
        $PostUpdate->title=$req->title;
        $PostUpdate->subtitle=$req->subtitle;
        $PostUpdate->description=$req->description;
        $PostUpdate->orderby=$req->orderby;
        $PostUpdate->isDirty('title');
        $PostUpdate->isDirty('subtitle');
        $PostUpdate->isDirty('description');
        $PostUpdate->isDirty('orderby');
        break;

        case "92":

          $PostUpdate->videoembed=$req->videoembed;
          $PostUpdate->orderby=$req->orderby;
          $PostUpdate->isDirty('videoembed');
          $PostUpdate->isDirty('orderby');
          break;

          case "93":
   

            $PostUpdate->stDate=$req->stDate;
            $PostUpdate->enDate=$req->enDate;
            $PostUpdate->stTime=$req->stTime;
            $PostUpdate->enTime=$req->enTime;
            $PostUpdate->venue=$req->venue;
       
            $PostUpdate->isDirty('stDate');
            $PostUpdate->isDirty('enDate');
            $PostUpdate->isDirty('stTime');
            $PostUpdate->isDirty('enTime');
            $PostUpdate->isDirty('venue');
            
            break;



          case "98":
          $PostUpdate->bannerFeatured=$req->bannerFeatured;
          $PostUpdate->orderby=$req->orderby;
          $PostUpdate->isDirty('bannerFeatured');
          $PostUpdate->isDirty('orderby');
          break;







          

  default:
    echo "Your favorite color is neither red, blue, nor green!";
}


$PostUpdate->isDirty('modifyby');
$PostUpdate->isDirty('modifydate');
$PostUpdate->isDirty('modifyip');

$PostUpdate->save();


    //upload multiple images    
  
    if($req->hasfile('uploadImage'))
    {
      $indexcount = 1 ;

       foreach($req->file('uploadImage') as $file)
       {
        $getExt = $file->extension();
        $random = substr(md5(mt_rand()), 0, 5);
        $fileName = 'bimg23'.$getBlockID.'_'.$random.'.'.$getExt ;        
        $path = $file->storeAs('public/product', $fileName );
        $CMSImage = Blog::where('id',$getBlockID)->update(['image'.$indexcount => $fileName]);
        $indexcount++ ;
        // echo  $fileName 
       }
    }


    return redirect('/admin-blog-manager'.'/'.$req->parentid)->with('sucess', 'updated sucessfully');

        }






        function CreatePostPage($slug = 0){


            $widgetContent = Widget::ShowData('CMSType');
            $categoryCombo = Categorybeta::categorybetaCombo(0);


            if(!empty($slug)){

                $dataviewer = Blog::find($slug) ;  
            
                } else {
            
                  $dataviewer = 0 ;
            
                }
          
              return view('blog.createblock',[
                
              'data'=>$dataviewer ,
              'navData' => $categoryCombo,
              'typeData' => $widgetContent
            
            
            ]);
              
          }


          
function updatePost(Request $req){
    $getUserID = 1 ;
    // return $req->all();
  
  
// slugGenerator
  
  
  if(!empty($req->blockid)){
   
    $PostUpdate = Blog::find($req->blockid) ; 
  
    $PostUpdate->languageid=$req->language;
    $PostUpdate->templateid=$req->template;
    $PostUpdate->typeid=$req->type;
    $PostUpdate->categoryid=$req->navigation;
    $PostUpdate->title=$req->title;
    $PostUpdate->subtitle=$req->subtitle;
    $PostUpdate->description=$req->description;
   
    $PostUpdate->location=$req->location;
    $PostUpdate->quote=$req->quote;
    $PostUpdate->meta=$req->metades;
    $PostUpdate->keyword=$req->keyword;
    $PostUpdate->videoembed=$req->videoembed;
    $PostUpdate->userid=$getUserID;
    $PostUpdate->modifyby=$getUserID;
    $PostUpdate->modifydate=Carbon::Now();
    $PostUpdate->modifyip=$req->ip();
  
  
    $PostUpdate->isDirty('languageid');
    $PostUpdate->isDirty('templateid');
    $PostUpdate->isDirty('typeid');
    $PostUpdate->isDirty('categoryid');
    $PostUpdate->isDirty('title');
    $PostUpdate->isDirty('subtitle');
    $PostUpdate->isDirty('description');
    $PostUpdate->isDirty('location');
    $PostUpdate->isDirty('quote');
    $PostUpdate->isDirty('meta');
    $PostUpdate->isDirty('keyword');
    $PostUpdate->isDirty('videoembed');
    $PostUpdate->isDirty('userid');
    $PostUpdate->isDirty('modifyby');
    $PostUpdate->isDirty('modifydate');
    $PostUpdate->isDirty('modifyip');
  
    $PostUpdate->save();
  
    $getCMSBlock =  Blog::WHERE('currentstatus','0')
  ->WHERE('id',$req->blockid)
  ->orderBy('id', 'DESC')
  ->first();
  
  $getCMSBlockID = $getCMSBlock->id ;
  
  
  }else{
  
  
  
  
  
    
  
    $finalCategoryID = '';
    //get user id is pending 
    
  
      $req->validate([
    'title' => 'bail|required',
      ]
    );


    $finalSlug = Slugemit::slugGenerator($req->title);
    $dbslugchecker = Blog::Where('slug', $finalSlug)->get();
                if(count($dbslugchecker) == 0) {
        
                  } else {
                    $t=time();
                    $finalSlug = $finalSlug.'_'.$t ;
                  }
  
        
    Blog::insert([
          'languageid'=>$req->language,
          'templateid'=>$req->template,
          'typeid'=>$req->type,
          'categoryid'=>$req->navigation,
          'title'=>$req->title,
          'slug'=>$finalSlug,
          'subtitle'=>$req->subtitle,
          'description'=>$req->description,
          'location'=>$req->location,
          'quote'=>$req->quote,
          'meta'=>$req->metades,
          'keyword'=>$req->keyword,
          'videoembed'=>$req->videoembed,
          'userid'=>$getUserID,
          'createby'=>$getUserID,
          'createdate'=>Carbon::Now(),
          'createip'=>$req->ip()
        ]);
  
  $getCMSBlock =  Blog::WHERE('currentstatus','0')
  ->WHERE('title',$req->title)
  ->orderBy('id', 'DESC')
  ->first();
  
  $getCMSBlockID = $getCMSBlock->id ;
  
  }
  
   // print_R( $req->all() );
  
  
  
  
    //upload multiple images    
  
        if($req->hasfile('uploadImage'))
        {
          $indexcount = 1 ;
  
           foreach($req->file('uploadImage') as $file)
           {

  
            $getExt = $file->extension();
            $random = substr(md5(mt_rand()), 0, 5);
            $fileName = 'bimg23'.$getCMSBlockID.'_'.$random.'.'.$getExt ;        
            $path = $file->storeAs('public/product', $fileName );

            
  
            $CMSImage = Blog::where('id',$getCMSBlockID)->update(['image'.$indexcount => $fileName]);


  
            $indexcount++ ;
  
            // echo  $fileName ;
          
           }
        }
  
      //upload single document
  
        if($req->hasfile('uploadDoc'))
        {
          $indexcount = 1 ;
  
           foreach($req->file('uploadDoc') as $filedoc)
           {
  
            $getExt = $filedoc->extension();
            $random = substr(md5(mt_rand()), 0, 5);
            $fileName = 'blogDocument'.$getCMSBlockID->id.'_'.$random.'.'.$getExt ;        
            $path = $filedoc->storeAs('public/product', $fileName );
  
            $CMSImage = Blog::where('id',$getCMSBlockID->id)->update(['document'.$indexcount => $fileName]);
  
            $indexcount++ ;
  
            // echo  $fileName ;
          
           }
        }
   return redirect('/admin-blog-manager')->with('sucess', 'updated sucessfully');
  
      
  }





          function dropPost($lid=0,$id=0){

            // dd($lid.'+'.$id);
              
            $getid = $lid ;
            $getUserID = Auth::user()->id ;
            $PostUpdate = Blog::find($getid) ; 
          
          
            if (is_numeric($id) && $id == '1' && $id < '10' ) {
          
             
              abort(403);
          
          
            } elseif (is_numeric($id) && $id == '2'  && $id < '10' ) {
          
          
               \Storage::delete('storage/specialassets/.$PostUpdate->image');
             
                // Storage::delete('public/specialassets/.$PostUpdate->image');
          
                // $PostUpdate->image = null ;
                // $PostUpdate->isDirty('image');
                // $PostUpdate->save();
          
            } elseif (is_numeric($id) && $id == '3'  && $id < '10'  ) {
          
                // File::delete(public_path('public/lectureassets/cover/'.$PostUpdate->document));
                // $PostUpdate->document = null ;
                // $PostUpdate->save();
          
                abort(403);
          
            } elseif (is_numeric($id) && $id == '4'  && $id < '10'  ) {
          
                // File::delete(public_path('lectureassets/cover/'.$PostUpdate->video));
                // $PostUpdate->video = null ;
                // $PostUpdate->save();
                abort(403);
          
            } elseif (is_numeric($id) && $id == '9'  && $id < '10'  ) {
          
                    if (is_numeric($getid) > 0 ) {
          
                    //query based deletes the record
          
                    $deletedRow = Blog::where('currentstatus', 0)
                    ->where('id', $getid)
                    ->where('createby', $getUserID)
                    ->delete();
                    
                    //model based delete
                    // $PostUpdate->delete();
          
                    } else { 
                        abort(403);
                    } 
          
             } else {       
                abort(403);
                }
          
                return redirect('admin-blog-manager/')->with('sucess', 'Record has been deleted');
          
          }









}