<?php

function dapallCertificateShortcode($atts, $content=null){ 
	
	/***Added this line to fix the elementor ,  for non-admin users content was not showing*****/
	if(isset($_REQUEST['tve']) && $_REQUEST['tve'] == true){return $posts;}		
	if(isset($_REQUEST['action']) && ($_REQUEST['action']  == "elementor" || isset($_REQUEST['elementor-preview']))){
		return $posts;
	} 
	if($GLOBALS['pagenow'] === 'post.php'){
		return $posts;
	}
	global $wpdb;
    extract(shortcode_atts(array(
        'id' => '',
    ), $atts));  
    
    if( !Dap_Session::isLoggedIn() ) {
	   return;
	} 
    wp_enqueue_style( 'allcertificatescss', plugin_dir_url(__FILE__).'includes/css/allCertificatesFrontend.css?v='.rand(10,1000));
    
    $id = 1;
    $allCertificatesData = Dap_DownloadAllCertificates::loadById($id);
	if(isset($allCertificatesData)){
		
		$dap_cdata = Dap_Product::loadProductByProductType("y");	
		
		$template_html = $allCertificatesData->getTemplateHtml();
		$customizerValue = $allCertificatesData->getCustomizerValues();
		$btnText = $allCertificatesData->getDownloadBtnText();
		$showCourses = $allCertificatesData->getCourseId();
		$btn_heigth = '12px';
		
		$downloadBtnCust = explode('||',$customizerValue);
		$btnStyle = '';
		 if(isset($downloadBtnCust[0])){
			$header_background_color = $downloadBtnCust[0];
		 }
		 if(isset($downloadBtnCust[1])){
			$tdBgColor = $downloadBtnCust[1];
		 }
		 if(isset($downloadBtnCust[2])){
			$btn_download_width = $downloadBtnCust[2];
		 }
		 if(isset($downloadBtnCust[3])){
			$btn_border_color = $downloadBtnCust[3];
		 }
		 if(isset($downloadBtnCust[4])){
			$btn_width = $downloadBtnCust[4].'px';
		 }
		 if(isset($downloadBtnCust[5])){
			$btn_background_color = $downloadBtnCust[5];
		 }
		 
		 if(isset($downloadBtnCust[6])){
			$btn_heigth = $downloadBtnCust[6].'px';;
		 }
		 $btnStyle = 'background:'.$btn_background_color.';width:'.$btn_width.';border-color:'.$btn_border_color.';padding-top:'.$btn_heigth.';padding-bottom:'.$btn_heigth;
		
		
		$session = Dap_Session::getSession();
		$user = $session->getUser();
		
		if (isset($user)) {
			$userId = $user->getId(); 
			$finalHtml = '';
			$showAll = false;
			if($showCourses == 0){
				$showAll = true;
			}else{
				$showCourses = explode(',' ,$showCourses);
			}
			
			foreach($dap_cdata as $cdata){
				$date = '';
				$productId = $cdata->getId();
				if(!$showAll && !in_array($productId,$showCourses)){continue;}
				$courseName = $cdata->getName();
				if(!$user->hasAccessTo($productId)){continue;}
				$downloadBtn = '';
				
				$courseCert = Dap_CourseCertificate::loadByCourseId($productId);
				
				$userCertificateData = Dap_UserCertificate::loadByCourseIdAndUserId($productId,$userId);
				
				
				if($userCertificateData != false && $courseCert != false){
					$certId = $courseCert->getId();
					$downloadBtn = downloadCertificateButton($productId,$certId ,$btnStyle,$btnText);
					
					$date = $userCertificateData->getDate();
				}
				
				$finalHtml .= '<tr style="background:'.$tdBgColor.';"><td>'.$courseName.'</td><td class="text-center">'.$downloadBtn.'</td><td class="text-center">'.$date.'</td></tr>';
				
				
			}
			
		}	
		if($finalHtml == ''){
			$finalHtml = '<tr><td>No Data Found</td><td></td><td></td></td></td></tr>';	
		}
	 //   print_r($allCertificatesData);die;
	 $template_html =  str_replace('<tr><td>%%DAPALLCERTIFICATES%%</td></tr>', $finalHtml, $template_html); 
	 $template_html .= "<script type=''>
	jQuery('.dap_cerficate_tiny_mce_editor').each(function(){
		jQuery(this).attr('contenteditable',false);
	})
</script>";
		 
		$style1 =  '<style>.dap_course_backend_tr{display:none} .certificate_container{display:block !important}</style>';
		$output = $template_html.$style1;
		return do_shortcode($output);
	}	
}
//$finalHtml
//<tr><td>%%DAPALLCERTIFICATES%%</td></tr>


