<?php
function partnership_func( $atts )
{
	$html = '';
	$myposts = get_posts( array(
		'numberposts' => -1,
		'orderby'    => 'menu_order',
		'order' => 'ASC',
		'post_type' => 'partnership'
	) );
	foreach($myposts as $m)
	{
		$img = get_the_post_thumbnail_url($m->ID, 'full');
		$meta = get_post_meta($m->ID);
		$st_color = $meta['st_subtitle_color3'][0];
		
		$html .= '<div class="l-grid-lb-lm2-sl-1 col is-sp-b"><a href="'.get_permalink($m->ID).'" class="btn-cover">
                      <div class="photo photo-16x9 is-bgc-black is-mb-m"><img src="'.$img.'" alt="'.$m->post_title.'" class="photo-img btn-cover-bg">
                        <div class="photo-label is-bottom is-center">
                          <h2 class="title title-5 is-mb-ss is-en-b" style="color:'.$st_color.'">'.$m->post_title.'</h2>
                        </div>
                      </div></a></div>';
	}
	
    return $html;
}
add_shortcode( 'partnership', 'partnership_func' );
function home_box_func($atts)
{
	$url = $atts['url'];
	$title = $atts['title'];
	$link = $atts['link'];
	
	$html = '<div class="relative_div pointer home_block" onclick="window.location=\''.$link.'\';">
		<img src="'.$url.'">
		<div class="title_div"><div class="center">'.$title.'</div></div>
	</div>';
	
	return $html;
}
add_shortcode( 'home_box', 'home_box_func' );
function home2_box_func($atts)
{
	$url = $atts['url'];
	$text = $atts['text'];
	$href = $atts['link'];
	$open_window = $atts['open_window'];
	
	if($open_window == 1)
	{
		
		$html = '<a href='.$href.' target="_blank"><div class="eventBox">			
			<div class="bgPhoto bgCover" style="background-image: url('.$url.');"></div>
<div class="inner ellipsisLines6">'.$text.'</div>
		</div></a>';
		
	} else {
		
	$html = '<a href='.$href.'><div class="eventBox">			
			<div class="bgPhoto bgCover" style="background-image: url('.$url.');"></div>
<div class="inner ellipsisLines6">'.$text.'</div>
		</div></a>';
	}
	
	return $html;
}
add_shortcode( 'home2_box', 'home2_box_func' );
function fund1_func()
{
	if($_GET['type'] == '')
	{
		$section1 = get_posts( array(
			'numberposts' => -1,
			'orderby'    => 'title',
			'order' => 'DESC',
			'post_type' => 'cpfis_reports1'
		) );
		
		foreach($section1 as $s)
		{				
			$meta = get_post_meta($s->ID);
			$file1 = $meta['st_pdf1'][0];
			$file1 = wp_get_attachment_url($file1);
			$file2 = $meta['st_pdf2'][0];
			$file2 = wp_get_attachment_url($file2);
			$file3 = $meta['st_pdf3'][0];
			$file3 = wp_get_attachment_url($file3);
			$file4 = $meta['st_pdf4'][0];
			$file4 = wp_get_attachment_url($file4);
			
			if($file1 != '')
			{
				$file1 = '<a href="'.$file1.'">View</a>';
			} else {
				$file1 = '-';
			}
			
			if($file2 != '')
			{
				$file2 = '<a href="'.$file2.'">View</a>';
			} else {
				$file2 = '-';
			}
			
			if($file3 != '')
			{
				$file3 = '<a href="'.$file3.'">View</a>';
			} else {
				$file3 = '-';
			}
			
			if($file4 != '')
			{
				$file4 = '<a href="'.$file4.'">View</a>';
			} else {
				$file4 = '-';
			}
			
			$html .= '<tr>
			<td class="graybg">'.$s->post_title.'</td>
			<td>'.$file1.'</td>
			<td>'.$file2.'</td>
			<td>'.$file3.'</td>
			<td>'.$file4.'</td>
			</tr>';
		}
		
		
		$section2 = get_posts( array(
			'numberposts' => -1,
			'orderby'    => 'title',
			'order' => 'DESC',
			'post_type' => 'cpfis_reports2'
		) );
		
		
		foreach($section2 as $s)
		{				
			$meta = get_post_meta($s->ID);
			$file1a = $meta['st_pdf1'][0];
			$file1a = wp_get_attachment_url($file1a);
			$file2a = $meta['st_pdf2'][0];
			$file2a = wp_get_attachment_url($file2a);
			$file3a = $meta['st_pdf3'][0];
			$file3a = wp_get_attachment_url($file3a);
			$file4a = $meta['st_pdf4'][0];
			$file4a = wp_get_attachment_url($file4a);
			
			if($file1a != '')
			{
				$file1a = '<a href="'.$file1a.'">View</a>';
			} else {
				$file1a = '-';
			}
			
			if($file2a != '')
			{
				$file2a = '<a href="'.$file2a.'">View</a>';
			} else {
				$file2a = '-';
			}
			
			if($file3a != '')
			{
				$file3a = '<a href="'.$file3a.'">View</a>';
			} else {
				$file3a = '-';
			}
			
			if($file4a != '')
			{
				$file4a = '<a href="'.$file4a.'">View</a>';
			} else {
				$file4a = '-';
			}
			
			$html2 .= '<tr>
			<td class="graybg">'.$s->post_title.'</td>
			<td>'.$file1a.'</td>
			<td>'.$file2a.'</td>
			<td>'.$file3a.'</td>
			<td>'.$file4a.'</td>
			</tr>';
		}
		
		
		$section3 = get_posts( array(
			'numberposts' => -1,
			'orderby'    => 'title',
			'order' => 'DESC',
			'post_type' => 'cpfis_reports3'
		) );
		
		
		
		foreach($section3 as $s)
		{				
			$meta = get_post_meta($s->ID);
			$file1b = $meta['st_pdf1'][0];
			$file1b = wp_get_attachment_url($file1b);
			$file2b = $meta['st_pdf2'][0];
			$file2b = wp_get_attachment_url($file2b);
			$file3b = $meta['st_pdf3'][0];
			$file3b = wp_get_attachment_url($file3b);
			$file4b = $meta['st_pdf4'][0];
			$file4b = wp_get_attachment_url($file4b);
			
			if($file1b != '')
			{
				$file1b = '<a href="'.$file1b.'">View</a>';
			} else {
				$file1b = '-';
			}
			
			if($file2b != '')
			{
				$file2b = '<a href="'.$file2b.'">View</a>';
			} else {
				$file2b = '-';
			}
			
			if($file3b != '')
			{
				$file3b = '<a href="'.$file3b.'">View</a>';
			} else {
				$file3b = '-';
			}
			
			if($file4b != '')
			{
				$file4b = '<a href="'.$file4b.'">View</a>';
			} else {
				$file4b = '-';
			}
			
			$html3 .= '<tr>
			<td class="graybg">'.$s->post_title.'</td>
			<td>'.$file1b.'</td>
			<td>'.$file2b.'</td>
			<td>'.$file3b.'</td>
			<td>'.$file4b.'</td>
			</tr>';
		}
		
		
		include('fund1.php');
		
	}
}
add_shortcode( 'fund1', 'fund1_func' );
function fund2_func()
{
	if($_GET['type'] == 2)
	{
		$section1 = get_posts( array(
			'numberposts' => -1,
			'orderby'    => 'title',
			'order' => 'DESC',
			'post_type' => 'funds_flow_reports'
		) );
		
		
		foreach($section1 as $s)
		{				
			$meta = get_post_meta($s->ID);
			$file1 = $meta['st_pdf1'][0];
			$file1 = wp_get_attachment_url($file1);
			$file2 = $meta['st_pdf2'][0];
			$file2 = wp_get_attachment_url($file2);
			$file3 = $meta['st_pdf3'][0];
			$file3 = wp_get_attachment_url($file3);
			$file4 = $meta['st_pdf4'][0];
			$file4 = wp_get_attachment_url($file4);
			
			if($file1 != '')
			{
				$file1 = '<a href="'.$file1.'">View</a>';
			} else {
				$file1 = '-';
			}
			
			if($file2 != '')
			{
				$file2 = '<a href="'.$file2.'">View</a>';
			} else {
				$file2 = '-';
			}
			
			if($file3 != '')
			{
				$file3 = '<a href="'.$file3.'">View</a>';
			} else {
				$file3 = '-';
			}
			
			if($file4 != '')
			{
				$file4 = '<a href="'.$file4.'">View</a>';
			} else {
				$file4 = '-';
			}
			
			$html .= '<tr>
			<td class="graybg">'.$s->post_title.'</td>
			<td>'.$file1.'</td>
			<td>'.$file2.'</td>
			<td>'.$file3.'</td>
			<td>'.$file4.'</td>
			</tr>';
		}
		
		include('fund2.php');
	}
}
add_shortcode( 'fund2', 'fund2_func' );
function download_pdf_func($atts)
{
	$file = $atts['file'];
	$text = $atts['text'];
	$lock = $atts['lock'];
	
	if($lock == '')
		$lock = 'Yes';
	
	$file = str_replace('.pdf','',$file);
	$file = str_replace('.PDF','',$file);
	
	if($_SESSION['member_login'] == 1 || $lock == 'No')
	{		
		$_SESSION['read_file'] = 1;
		$url = 'https://imas.org.sg/download_pdf.php?file_url='.$file;	
		$html = '<a href="'.$url.'" class="btn btnSmall icon-arrow-right" href="#" target="_blank">'.$text.'</a><br><br>';
		
	} else {
		
	$url = 'https://imas.org.sg/download_pdf.php?file_url='.$file;	
	$html = '<a onclick="prompt_login();return false;" class="btn btnSmall icon-arrow-right">'.$text.'</a><br><br>';
		
	}
	
	return $html;
}
add_shortcode('download','download_pdf_func');
function link_pdf_func($atts)
{
	$file = $atts['file'];
	$text = $atts['text'];
	$lock = $atts['lock'];
	$type = $atts['type'];
	
	if($lock == '')
		$lock = 'Yes';
	
	$file = str_replace('.pdf','',$file);
	$file = str_replace('.PDF','',$file);
	
	if($_SESSION['member_login'] == 1 || $lock == 'No')
	{		
		$_SESSION['read_file'] = 1;
		$url = 'https://imas.org.sg/download_pdf.php?file_url='.$file.'&type='.$type;	
		$html = '<a href="'.$url.'" style="color: #107ac0; text-decoration: underline;" target="_blank">'.$text.'</a><br><br>';
		
	} else {
		
	$url = 'https://imas.org.sg/download_pdf.php?file_url='.$file.'&type='.$type;	
	$html = '<a href="#" onclick="alert(\'Kindly login to access the file\');return false;" style="color: #107ac0; text-decoration: underline;" href="#" target="_blank">'.$text.'</a><br><br>';
		
	}
	
	return $html;
}
add_shortcode('link','link_pdf_func');
function members_func($atts)
{
	include('members.php');
	
}
add_shortcode('members','members_func');
function members2_func($atts)
{
	include('members2.php');
	
}
add_shortcode('members2','members2_func');
function button_func($atts)
{
	$link = $atts['link'];
	$text = $atts['text'];
	$target = $atts['target'];
	
	$html = '<a href="'.$link.'" class="btn btnSmall icon-arrow-right" target="'.$target.'">'.$text.'</a>';
	
	return $html;
}
add_shortcode('button', 'button_func');
function newletters_func()
{
	$args2 = array(
		'post_type' => 'imas-newsletter',
		'posts_per_page' => -1,
		 'orderby' => 'menu_order',
		 'order' => 'ASC',
	);
	$news = get_posts($args2);
	
	$html = '<div id="newsletter">';
	
	foreach($news as $b)
	{		
		$url = wp_get_attachment_url( get_post_thumbnail_id($b->ID), 'full' );
		$meta = get_post_meta($b->ID);
		$file = $meta['st_pdf'][0];
		$file = wp_get_attachment_url($file);
		
		$restrictedfile = $meta['st_restrictedfile'][0];
	
		$pdf2 = $meta['st_pdf2'][0];
		$pdf2 = str_replace('.pdf','',$pdf2);
		
		
		if($_SESSION['member_login'] == 1 || $restrictedfile != 1)
		{		
			$_SESSION['read_file'] = 1;
			$url2 = 'https://imas.org.sg/download_pdf.php?file_url='.$pdf2;	
			
			
			if($restrictedfile == 1)
			{
				
			$html .= '<div class="news_block"><a href="'.$url2.'" target="_blank"><img src="'.$url.'"><br>
		<strong>'.$b->post_title.'</strong></a>
		</div>';
			} else {
				
			$html .= '<div class="news_block"><a href="'.$file.'" target="_blank"><img src="'.$url.'"><br>
		<strong>'.$b->post_title.'</strong></a>
		</div>';
			}
			
		
		} else {
		
			$html .= '<div class="news_block"><a href="#" onclick="alert(\'Kindly login to access the file\');return false;"><img src="'.$url.'"><br>
		<strong>'.$b->post_title.'</strong></a>
		</div>';
		
		}
		
	}
	
	$html .= '</div>';
	
	return $html;
}
add_shortcode('newletters', 'newletters_func');
function members_publications_func()
{
	$args2 = array(
		'post_type' => 'members-publications',
		'posts_per_page' => -1,
		 'orderby' => 'menu_order',
		 'order' => 'ASC',
	);
	$news = get_posts($args2);
	
	$html = '<div id="newsletter">';
	
	foreach($news as $b)
	{		
		$url = wp_get_attachment_url( get_post_thumbnail_id($b->ID), 'full' );
		$meta = get_post_meta($b->ID);
		$file = $meta['st_pdf'][0];
		$file = wp_get_attachment_url($file);
		
		
		$html .= '<div class="news_block"><a href="'.$file.'" target="_blank"><img src="'.$url.'"><br>
		<strong>'.$b->post_title.'</strong></a>
		</div>';
	}
	
	$html .= '</div>';
	
	return $html;
}
add_shortcode('members-publications', 'members_publications_func');
function press_release_func()
{
	$today = date('Y-m-d');
		
	$args2 = array(
		'post_type' => 'events_block',
		'posts_per_page' => 2,
	   'meta_key' => 'st_date', //name of meta field
	   'orderby' => 'meta_value', 
	   'order' => 'DESC', // you can modify it as per your use
	   'meta_query' => array(
			array(
				'key'       => 'st_date',
				'value'     => $today,
				'compare' => '>='
				)
			)
	);
	$upcoming_events = get_posts( $args2 );
	
	$remaining = 4;
	
	$remaining = $remaining - sizeof($upcoming_events);
	$args4 = array(
		'post_type' => 'press_releases',
		'posts_per_page' => $remaining,
	);
	$press_releases = get_posts($args4);
	
	foreach($press_releases as $r)
	{
		$href = get_permalink($r->ID);
		$url = wp_get_attachment_url( get_post_thumbnail_id($r->ID), 'full' );
		$text = $r->post_title;
		
	
		$html .= '<div class="press"><a href='.$href.'><div class="eventBox">			
			<div class="bgPhoto bgCover" style="background-image: url('.$url.');"></div>
		<div class="inner ellipsisLines6">'.$text.'</div>
		</div></a></div>';
	}
	
	
	foreach($upcoming_events as $r)
	{
		$href = get_permalink($r->ID);
		$url = wp_get_attachment_url( get_post_thumbnail_id($r->ID), 'full' );
		$text = $r->post_title;
		
	
		$html .= '<div class="press"><a href='.$href.'><div class="eventBox">			
			<div class="bgPhoto bgCover" style="background-image: url('.$url.');"></div>
		<div class="inner ellipsisLines6">'.$text.'</div>
		</div></a></div>';
	}
	
	return $html;
}
add_shortcode('home-press-release', 'press_release_func');
function video_func($atts)
{
	$id = $atts['id'];
	if($id == '')
	{	
		$post_meta = get_post_meta(get_the_ID());
	} else {
		$post_meta = get_post_meta($id);
	}
	$video = $post_meta['st_video'][0];
	
	if($video != '')
	{
		if($_SESSION['member_login'] != 1)
		{
			
			$_SESSION['redirect'] = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'#video';
			$link = home_url().'/login';
			$html = '<div align="center">';
			$html = '<a href="'.$link.'" class="btn btnSmall icon-arrow-right">Watch recorded webinar here</a>';
			$html .= '<div>';
		} else {
			$html = '<div align="center" class="videoWrapper"><a name="video" id="video"></a>';
			$html .= $video;			
			$html .= '<div>';
		}
	}
	
	
	return $html;
}
add_shortcode('video', 'video_func');
?>