
<?php
/*
 * The template for displaying the newsletter
 *
 * This is the template that displays the newsletter.
 * This template over-rides page-newsletter.php.
 *
 * @package IRIS OpenWebsite Version 4.0
 *
 */
?>
<?php //check_newsletter_status() ?>
<?php
/* Newsletter colours */
	$plugindir=plugin_dir_path( __DIR__);
require $plugindir .'Dompdf/src/Dompdf.php';
require $plugindir .'Dompdf/src/Options.php';
require $plugindir .'Dompdf/src/CanvasFactory.php';
require $plugindir .'Dompdf/src/Canvas.php';
require $plugindir .'Dompdf/lib/Cpdf.php';
require $plugindir .'Dompdf/src/FontMetrics.php';
require $plugindir .'Dompdf/src/Css/Stylesheet.php';
require $plugindir .'Dompdf/src/Css/Style.php';
require $plugindir .'Dompdf/vendor/autoload.php';
require $plugindir .'Dompdf/src/Helpers.php';
require $plugindir .'Dompdf/src/Adapter/CPDF.php';

use Dompdf\Dompdf;
use Dompdf\Options;
/* Newsletter colours */
$head_bg = get_field('head_bg',397);
$nav_bg = get_field('nav_bg',397);
$title_bg = get_field('title_bg',397);
$item_bg = get_field('item_bg',397);
$footer_bg = get_field('footer_bg',397);

$main_title_text = get_field('main_title_text',397);
$head_links = get_field('head_links',397);
$nav_text = get_field('nav_text',397);
$title_text = get_field('title_text',397);
$main_text = get_field('main_text',397);
$link_text = get_field('link_text',397);
$footer_text = get_field('footer_text',397);

?>
<?php $title = get_field('newsletter_title') ? get_field('newsletter_title') : 'Tax Tips &amp; News'; ?>
<?php $content_type = get_field( 'newsletter_content' ); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo ow_global_var( 'accountant_name' ); ?> | <?php echo $title; ?> |
<?php if ($content_type == 'tax_tips') {echo ow_tax_news_date();} else {echo date('F Y');} ?>
</title>
	<style>
		body {background-color:#dddddd;margin:0;padding:10px 0;font-family:arial;font-size:14px;color:<?php echo $main_text; ?>;}
		h1,h2,h3,h4,h5,h6,p,pre,ul,ol {margin:0 0 10px 0;}
		a {text-decoration:none;color:<?php echo $link_text; ?>;}
		address {font-style:normal;}
		blockquote {font-style:italic;}
	.download-details{
		background-image:url('<?php echo WP_PLUGIN_URL ;?>/ow-plugin/img/download-pdf.svg');  height: 225px;
    width: 50px;background-size: 100% 100%; background-color: transparent;
     padding:10px; border:0 none; cursor: pointer; float:none; margin:0 auto;display: block;margin-bottom: 10px;}
	#logo{max-width: 236px;
    max-height: 100px;}
	</style>
</head>
<body>

	
<?php 
	$tables='<table bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">';
	$tablend='</table>';
 /* ------------------------------------------------------------------------------------------------
  Header & Logo
  ------------------------------------------------------------------------------------------------ */
	$header=$tables.'<tr bgcolor="'.$head_bg.'">
    <th colspan="2" align="center"> <table width="100%" cellspacing="0">
        <tr>
          <td align="left" width="50%" style="vertical-align:top;padding: 10px" class="logo"><img src="'.get_template_directory_uri().'/img/logo.png" alt="'.ow_global_var( 'accountant_name' ).' Logo" id="logo" class="logo" ></td>
          <td align="right" width="50%" style="vertical-align:top;padding: 10px;"><h1 style="color:'.$main_title_text.';font-weight:400;">'.$title.'</h1>
            <p><span style="color:'.$head_links.';font-size:16px;font-weight:400;" >'.ow_global_var('accountant_phone').'</span></p>
            <p><a style="color:'.$head_links.';font-size:16px;font-weight:400;" href="http://www.'.ow_global_var( 'accountant_website' ).'">'.ow_global_var( 'accountant_website' ).'</a></p></td>
        </tr>
      </table>
    </th>
  </tr>
 '.$tablend;
 
  /* ------------------------------------------------------------------------------------------------
  Navigation
  ------------------------------------------------------------------------------------------------ */
  
	if(get_field('show_navigation') === true) : $header.=$tables.'
  <tr bgcolor="'.$nav_bg.'">
    <td colspan="2" align="center" style="padding: 10px"><table width="100%" cellspacing="0">
        <tr>';?>
          <?php
          $menu = wp_get_nav_menu_items( 'main navigation' );
          foreach ( $menu as $item ) {
              if ( $item->post_parent == 0 ) {
                  $header.='<td align="center"><a href="' . $item->url . '" style="color:' . $nav_text . ';">' . $item->title . '</a></td>';
              }
          }
        $header.='</tr>
      </table></td>
  </tr>'.$tablend; endif; 
	/* ------------------------------------------------------------------------------------------------
  Header Image.
  ------------------------------------------------------------------------------------------------ */
  ?>
  <?php if((get_field('show_header_image') === true) && ($image = get_field('head_image'))) :$header.=$tables.'
  <tr>
    <td colspan="4" align="center" style="padding: 0">'.wp_get_attachment_image( $image, 'newsletter-header').'</td>
  </tr>
 '.$tablend; endif; ?>
	
	<?php
  /* ------------------------------------------------------------------------------------------------
  Welcome Message
  ------------------------------------------------------------------------------------------------ */
  if(isset($_REQUEST['issue'])){
	  
	  $header.=$tables.'
  <tr>
  <td></td>
    <td style="padding:20px 20px 0 0" align="right" valign="top" class="date">'.date('d/m/Y',strtotime($_REQUEST['issue'])).'</td>
  </tr>';
  }else{
	  $header.=$tables.'
  <tr>
  <td></td>
    <td style="padding:20px 20px 0 0" align="right" valign="top" class="date">'.date('d/m/Y').'</td>
  </tr>';}?>
  <?php if(get_field('show_welcome_message') === true) : $header.='
  <tr>
    <td colspan="4"><table width="100%" cellspacing="0">
        <tr>';
		if(isset($_REQUEST['issue'])){		
			$monthname=date('F',strtotime($_REQUEST['issue']));
			$header.='<td colspan="2" style="vertical-align:top;padding: 20px">
				<h1>'.$monthname.' Tax Tips &amp; News</h1>
				<p>Welcome to the '.ow_global_var('accountant_name').' Tax Tips &amp; News monthly newsletter, bringing you the latest news to keep you one step ahead of the taxman.</p>
				<p>If you need further assistance just let us know or send us a question for our Question and Answer Section.</p>
				<p>We’re committed to ensuring none of our clients pay a penny more in tax than is necessary and they receive useful tax and business advice and support throughout the year.</p>
				<p>Please contact us for advice on your own specific circumstances. We’re here to help!</p>
			</td>';
	}else{
          $header.='<td colspan="2" style="vertical-align:top;padding: 20px">'.ow_newsletter_merge_tags( get_field('welcome_message') ).'</td>';
		}
        $header.='</tr>
      </table></td>
  </tr>
  '; endif; $header.=$tablend;?>
	<?php
  /* ------------------------------------------------------------------------------------------------
  Main Content
  ------------------------------------------------------------------------------------------------ */
  ?>
  <?php
	$content='';
	$fullcontent='';
  if ( $content_type == 'tax_tips' ):
      global $iris_db_old;
	if(isset($_REQUEST['issue'])){
		$monthold=date('Y-m-01',strtotime($_REQUEST['issue']));
		$tax_news_list = $iris_db_old->get_results( "SELECT * FROM t_news WHERE month_id = (SELECT newsMonthId FROM t_news_months WHERE newsMonthName = '".$monthold."')" );
	}else{
  $tax_news_list = $iris_db_old->get_results( "SELECT * FROM t_news WHERE month_id = (SELECT newsMonthId FROM t_news_months WHERE newsMonthCurrent = 'yes')" );
	}

  foreach ( $tax_news_list as $item ): 
	$content.=$tables.'
  <tr>
    <td style="padding:0 10px;"><table width="100%" cellspacing="0">
        <tr>
          <td bgcolor="'.$title_bg.'" style="color:'.$title_text.';padding:5px 10px;font-size:20px;">'.$item->title.'</td>
        </tr>
        <tr>
          <td bgcolor="'.$item_bg.'" style="padding:10px;">'.$item->nlsummary.' <a href="/news/tax-news/article/'.$item->id.'/">Read More...</a></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>
  '.$tablend;
	$fullcontent.=$tables.'
  <tr>
    <td style="padding:0 10px;"><table width="100%" cellspacing="0">
        <tr>
          <td bgcolor="'.$title_bg.'" style="color:'.$title_text.';padding:5px 10px;font-size:20px;">'.$item->title.'</td>
        </tr>
        <tr>
          <td bgcolor="'.$item_bg.'" style="padding:10px;">'.$item->details.'</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>
  '.$tablend; endforeach; ?>
	<?php
  /* ------------------------------------------------------------------------------------------------
  BUDGET
  ------------------------------------------------------------------------------------------------ */
  ?>
  <?php
  elseif ( $content_type == 'budget' ):
      global $iris_db_old;
  $budget = $iris_db_old->get_row( "SELECT * FROM t_budgets ORDER BY id DESC LIMIT 1" );
  $content.=$tables.'
  <tr>
    <td style="padding:0 10px;"><table width="100%" cellspacing="0"  colspan="2">
        <tr>
          <td bgcolor="'.$title_bg.'" style="color:'.$title_text.';padding:5px 10px;font-size:20px;">'.$budget->title.'</td>
        </tr>
        <tr>
          <td bgcolor="'.$item_bg.'" style="padding:10px;word-break: break-word;">'.str_replace( ['[[',']]'], '"', $budget->details ).'</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>'.$tablend;
	$fullcontent.=$tables.'
  <tr>
    <td style="padding:0 10px;"><table width="100%" cellspacing="0"  colspan="2">
        <tr>
          <td bgcolor="'.$title_bg.'" style="color:'.$title_text.';padding:5px 10px;font-size:20px;">'.$budget->title.'</td>
        </tr>
        <tr>
          <td bgcolor="'.$item_bg.'" style="padding:10px;word-break: break-word;">'.str_replace( ['[[',']]'], '"', $budget->details ).'</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>'.$tablend;
  elseif ($content_type == 'custom') : 
	$content.=$tables.'
  <tr>
    <td style="padding:0 10px"><table width="100%" cellspacing="0" colspan="2">
        <tr>
          <td style="padding: 10px;"  colspan="2">'.ow_newsletter_merge_tags( get_field( 'custom_content' ) ).'</td>
        </tr>
        <tr>
          <td  colspan="2">&nbsp;</td>
        </tr>
      </table></td>
  </tr>
 '.$tablend;
	$fullcontent.=$tables.'
  <tr>
    <td style="padding:0 10px"><table width="100%" cellspacing="0" colspan="2">
        <tr>
          <td style="padding: 10px;"  colspan="2">'.ow_newsletter_merge_tags( get_field( 'custom_content' ) ).'</td>
        </tr>
        <tr>
          <td  colspan="2">&nbsp;</td>
        </tr>
      </table></td>
  </tr>
 '.$tablend;endif; ?>
  <?php
  /* ------------------------------------------------------------------------------------------------
  About Us
  ------------------------------------------------------------------------------------------------ */
  ?>
  <?php $footer='';if(get_field('show_about_us') === true) : $footer.=$tables.'
  <tr>
    <td style="padding:0 10px"><table width="100%" cellspacing="0">
        <tr bgcolor="'.$title_bg.'" style="color:'.$title_text.';font-size:20px;">
          <td style="padding:5px 10px;">About Us</td>
        </tr>
        <tr bgcolor="'.$item_bg.'">
          <td style="padding: 10px;">'.ow_newsletter_merge_tags( get_field('about_us') ).'</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>
  '.$tablend; endif;?>
  <?php
  /* ------------------------------------------------------------------------------------------------
  Social Icons
  ------------------------------------------------------------------------------------------------ */
  ?>
  <?php if($social = get_field('social_icons')) : $footer.=$tables.'
  <tr>
    <td colspan="2" align="center">';
	if($facebook = ow_social_icon_png('facebook')) {$footer.=$facebook;};
      if($twitter = ow_social_icon_png('twitter')) {$footer.=$twitter;};
      if($linkedin = ow_social_icon_png('linkedin')) {$footer.=$linkedin;};
      if($googleplus = ow_social_icon_png('googleplus')) {$footer.=$googleplus;}
  $footer.='</td></tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  '.$tablend; endif; ?>
  <?php
  /* ------------------------------------------------------------------------------------------------
  Copyright Information
  ------------------------------------------------------------------------------------------------ */
  $footer.=$tables.'
  <tr align="center" bgcolor="'.$footer_bg.'" style="font-size:10px;">
    <td colspan="2" style="padding:15px 10px;color:'.$footer_text.';"><p>'.'Copyright &copy; '.date('Y').' '.ow_global_var('accountant_name').'. All rights reserved.
      <p>
      <p>'.ow_global_var('accountant_address').'</p>
      <p>'.ow_global_var('accountant_name').' is a trading name of '.ow_global_var('accountant_full_name').'
      <p>
       ';
	
        if ( ow_global_var( 'accountant_reg' ) != NULL ) {
             $footer.='<p>' . ow_global_var( 'accountant_reg' ) . ', Registered in England and Wales' . '</p>';
        }
        
     $footer.='</td>
  </tr>'.$tablend;?>
	<?php $pdfcontent=$header.$fullcontent.$footer.'<style>
		body {background-color:#fff;margin:0;padding:0;font-family:sans-serif;font-size:14px;color:'.$main_text.';}
		h1,h2,h3,h4,h5,h6,p,pre,ul,ol {margin:0 0 10px 0;}
		a {text-decoration:none;color:'.$link_text.';}
		address {font-style:normal;}
		blockquote {font-style:italic;}
		img{width:100%;}
		.logo img{width:auto;}
		.date{text-align:right;}
	#logo{max-width: 236px;
    max-height: 100px;}
@page { margin: 50px; }
.fff{display:none;}
</style> ';?>
	<?php
	if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "generate-one-sheet" ) { 
//echo $content;exit();

// instantiate and use the dompdf class
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($pdfcontent);
 
// (Optional) Setup the paper size and orientation
$dompdf->set_paper( 'a4' , 'portrait' );
// Render the HTML as PDF
$dompdf->render();
 
$doctitle = 'Newsletter - '.ow_global_var('accountant_name');
 /*?>//-------------------------WATERMARK Start
 // Instantiate canvas instance 
$canvas = $dompdf->getCanvas(); 
 
// Instantiate font metrics class 
$fontMetrics = new FontMetrics($canvas, $options); 
 
// Get height and width of page 
$w = $canvas->get_width(); 
$h = $canvas->get_height(); 
// Get font family file 
$font = $fontMetrics->getFont('times'); 
 
// Specify watermark text 
$text = "Maintained by IRIS WebPortal"; 
 
// Get height and width of text 
$txtHeight = $fontMetrics->getFontHeight($font, 10); 
$textWidth = $fontMetrics->getTextWidth($text, $font, 10); 
 
// Set text opacity 
$canvas->set_opacity(.2); 
 
// Specify horizontal and vertical position 
$x = (($w-$textWidth)/2); 
$y = (($h-$txtHeight)/2); 
 
// Writes text at the specified x and y coordinates 
$canvas->text($x, $y, $text, $font, 75); 
//------------------------WATERMARK END */
ob_end_clean();
$dompdf->stream($doctitle);
 
exit();
}?>
		<?php if ( $content_type == 'tax_tips' ):?>
	<div id="pdfdownload" style="display:none;    position: absolute;   right: calc(100vw - ((100vw - 644px) / 2));z-index:0;  top: 13%;"><form class="form-horizontal"  role="form" action="" method="post" >
    <button type="submit" class="download-details"></button>
    <input type="hidden" name="action" value="generate-one-sheet" />
</form></div><?php endif;?>
<table bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="0" width="660" align="center">
  <?php
 
  ?>
  <tr bgcolor="<?php echo $head_bg; ?>">
    
          <td align="left" width="50%" style="vertical-align:top;" id="pdfcontent"><?php echo $header.$content.$footer;?></td>
        
  </tr>
  
  
  
</table>
</body>
</html>
<?php
/*
 * This is an extra text area. If the customer wants to add a new text area/article, paste this code above, and add the required text area to the Newsletter field group using the Custom Fields plugin
 *
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
<tr>
	<td style="vertical-align:top;" colspan="2">
		<table width="100%">
			<tr bgcolor="<?php echo $title_bg; ?>" style="color:<?php echo $title_text; ?>;">
				<td style="padding:5px 10px;">NEW SECTION</td>
			</tr>
			<tr>
				<td style="padding: 10px;"><?php echo get_field('NEW_SECTION'); ?></td>
			</tr>
		</table>
	</td>
</tr>
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------	
*/

?><script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
     $("#pdfdownload").fadeIn(3000);
    });

</script>
