<?php 

class Widget extends baseController
{
	
	
	   function add()
	  {
            error_reporting(-1);
			$data = $this->getDataRequest();
			if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') :
			 $folderPath = "../../uploads/logo/";
			 $folderPath1 = "../../uploads/cover/";
			 if(!empty($_FILES['logo']['name'])){
             $logo = rand(0,99999)."_".$_FILES['logo']['name'];
             $tpath = $folderPath . $logo;
             $pic = $this->compress_image($_FILES["logo"]["tmp_name"], $tpath,80);
             $dbfile1 = "uploads/logo/".$logo;
			 }else{
			   $dbfile1 = "";  
			 }
			 if(!empty($_FILES['cover_image']['name'])){
             $cover_image = rand(0,99999)."_".$_FILES['cover_image']['name'];
			 //$this->upload($location); 
			 $tpath1 = $folderPath1 . $cover_image;
			  $pic1 = $this->compress_image($_FILES["cover_image"]["tmp_name"], $tpath1,80);
			 
			 $dbfile2 = "uploads/cover/".$cover_image;
			 }else{
			    $dbfile2 = ""; 
			 }
			 $query = $this->db->prepare("insert into tbl_widget (stream,platform,user_id,theme,title,salogan,history,volume,background,logo,cover_image,hidelogo,hidecover,hidevolume,statictitle,hidetitle,hidesocial,autoplay) 
			 values(:stream,:platform,:user_id,:theme,:title,:salogan,:history,:volume,:background,:logo,:cover_image,:hidelogo,:hidecover,:hidevolume,:statictitle,:hidetitle,:hidesocial,:autoplay)");
             $query->bindParam(':stream',$data->stream);
              $query->bindParam(':platform',$data->platform);
			 $query->bindParam(':user_id',$data->user_id);
			 $query->bindParam(':theme',$data->theme);
             $query->bindParam(':title',$data->title);
             $query->bindParam(':salogan',$data->salogan);
             $query->bindParam(':history',$data->history);
             $query->bindParam(':volume',$data->volume);
             $query->bindParam(':background',$data->background);
             $query->bindParam(':logo',$dbfile1);
             $query->bindParam(':cover_image',$dbfile2);
             $query->bindParam(':hidelogo',$data->hidelogo);
             $query->bindParam(':hidecover',$data->hidecover);
             $query->bindParam(':hidevolume',$data->hidevolume);
             $query->bindParam(':statictitle',$data->statictitle);
             $query->bindParam(':hidetitle',$data->hidetitle);
             $query->bindParam(':hidesocial',$data->hidesocial);
             $query->bindParam(':autoplay',$data->autoplay);
             $query->execute(); 
			 $lastid = $this->db->lastInsertId();
			 $token = md5($lastid);
			 $update = $this->db->query("update tbl_widget set token ='".$token."' where id = $lastid");
			 $baseurl = dirname(siteUrl);
				 $url = dirname($baseurl).'/player.php?token='.$token.'';
				 $data = '<iframe name="contentPlayer" width="100%" frameborder=0 scrolling=no  src="'.$url.'"></iframe>';
				
				echo json_encode(["status"=>"success","widget"=>$data,"code"=>$url]);
        endif;

	  }
     
        function Remove()
          {
			  
              $data = $this->getDataRequest();
              $qry = $this->db->query("delete from tbl_widget where id =".$data->id);
              echo $qry ? "success" :  "fail";
          }

          function Update()
          {
              error_reporting(-1);
			   $data = $this->getDataRequest();
			    $folderPath = "../../uploads/logo/";
			    $folderPath1 = "../../uploads/cover/";
			    if(!empty($_FILES['logo']['name'])){
				   $imgfile=rand(0,99999)."_".$_FILES['logo']['name'];
				   $get = $this->getBy("tbl_widget","id =".$data->edit_id);
				   $datas = $get[0];
				   
				   if($datas['logo']!="")
					{
					unlink('../../'.$datas['logo']);
					}
				 $tpath = $folderPath . $imgfile;
				 $dbfile = 'uploads/logo/'.$imgfile;
				 $pic=$this->compress_image($_FILES["logo"]["tmp_name"], $tpath,80);
			   }else{
			      if($data->old_logo ==""){
		            $get = $this->getBy("tbl_widget","id =".$data->edit_id);
				    $datas = $get[0];
				   
				   if($datas['logo']!="")
					{
					unlink('../../'.$datas['logo']);
					}
			       }
				  $dbfile = $data->old_logo;
			   } 
			   if(!empty($_FILES['cover_image']['name'])){
				   $imgfile1=rand(0,99999)."_".$_FILES['cover_image']['name'];
				   $get = $this->getBy("tbl_widget","id =".$data->edit_id);
				   $datas = $get[0];
				   
				   if($datas['cover_image']!="")
					{
					unlink('../../'.$datas['cover_image']);
					}
				 $tpath1 = $folderPath1 . $imgfile1;
				 $dbfile1 = 'uploads/cover/'.$imgfile1;
				 $pic1=$this->compress_image($_FILES["cover_image"]["tmp_name"], $tpath1,80);
			   }else{
			       if($data->old_cover ==""){
		            $get = $this->getBy("tbl_widget","id =".$data->edit_id);
				    $datas = $get[0];
				   
				   if($datas['cover_image']!="")
					{
					unlink('../../'.$datas['cover_image']);
					}
			       }
				  $dbfile1 = $data->old_cover;
			   }
               $stream = !empty($data->stream) ? $data->stream : "";
               $platform = !empty($data->platform) ? $data->platform : "";
               $theme = !empty($data->theme) ? $data->theme : "";
               $title = !empty($data->title) ? $data->title : "";
               $salogan = !empty($data->salogan) ? $data->salogan : "";
               $history = !empty($data->history) ? $data->history : "";
               $volume = !empty($data->volume) ? $data->volume : "";
               $background = !empty($data->background) ? $data->background : "";
               $hidelogo = !empty($data->hidelogo) ? $data->hidelogo : "";
               $hidecover = !empty($data->hidecover) ? $data->hidecover : "";
               $hidevolume = !empty($data->hidevolume) ? $data->hidevolume : "";
               $statictitle = !empty($data->statictitle) ? $data->statictitle : "";
               $hidetitle = !empty($data->hidetitle) ? $data->hidetitle : "";
               $hidesocial = !empty($data->hidesocial) ? $data->hidesocial : "";
               $autoplay = !empty($data->autoplay) ? $data->autoplay : "";
			   $token = !empty($data->token) ? $data->token : md5($data->edit_id);
                $sql = "UPDATE tbl_widget SET stream = :stream,platform = :platform,theme = :theme,title = :title,salogan = :salogan,history = :history,volume = :volume,
				background= :background,logo=:logo,cover_image=:cover_image,hidelogo = :hidelogo,hidecover = :hidecover,hidevolume = :hidevolume,statictitle = :statictitle,hidetitle= :hidetitle,hidesocial=:hidesocial,autoplay=:autoplay,token= :token  WHERE id = :id";
                 $query = $this->db->prepare($sql);  
                 $query->bindParam(':stream',$stream);
                 $query->bindParam(':platform',$platform);
                 $query->bindParam(':theme',$theme);
                 $query->bindParam(':title',$title);
                 $query->bindParam(':salogan',$salogan);
                 $query->bindParam(':history',$history);
                 $query->bindParam(':volume',$volume);
                 $query->bindParam(':background',$background);
                 $query->bindParam(':logo',$dbfile);
                 $query->bindParam(':cover_image',$dbfile1);
                 $query->bindParam(':hidelogo',$hidelogo);
                 $query->bindParam(':hidecover',$hidecover);
                 $query->bindParam(':hidevolume',$hidevolume);
                 $query->bindParam(':statictitle',$statictitle);
                 $query->bindParam(':hidetitle',$hidetitle);
                 $query->bindParam(':hidesocial',$hidesocial);
                 $query->bindParam(':autoplay',$autoplay);
				 $query->bindParam(':token',$token);
                 $query->bindParam(':id',$data->edit_id);
                 $query->execute();
				 $baseurl = dirname(siteUrl);
				 $url = dirname($baseurl).'/player.php?token='.$token.'';
        
				 $data = '<iframe name="contentPlayer" width="100%" frameborder=0 scrolling=no  src="'.$url.'"></iframe>';
				
				echo json_encode(["status"=>"success","widget"=>$data,"code"=>$url]);
            
          }
		  function Upstatus()
          {
			 
			 $data = $this->getDataRequest();
			 
             $get = $this->getBy("applist","id =".$data->id);
			 $data = $get[0];
			 
             if($data['status'] == "0" ){
				 $val = "1";
			 }else{
				$val = "0";
			 }
			$query = $this->db->query("update applist set status = '".$val."' where id = ".$data['id']);
			echo $query ? "success" :  "fail";
			
			//print_r($query);
          }

}
?>