<?php

	define('IN_HUGLO', true);
	require_once('./common.php');
	require_once(ROOT_PATH . 'includes/menu_newsletter.php');
	require_once(ROOT_PATH . 'includes/class.phpmailer.php');

	$error = array();

	$category_id = isset($_GET['category_id']) ? (is_numeric($_GET['category_id']) ? $_GET['category_id'] : 23) : 23;

	$sql = 'SELECT c2.category_id, c2.category_parent_id, c1.category_name_fr , c2.category_name_fr AS category_s_name_fr, c1.category_name_en , c2.category_name_en AS category_s_name_en, c2.category_bg_img, c2.category_bg_color FROM ' . CATEGORY_TABLE . ' c1 LEFT JOIN ' . CATEGORY_TABLE . ' c2 ON c2.category_parent_id = c1.category_id WHERE c2.category_id = ' . $category_id;
	$result = mysql_query($sql);
	$category = mysql_fetch_assoc($result);
	mysql_free_result($result);

	$sql = 'SELECT * FROM ' . CONTENT_TABLE . ' WHERE category_id = 6';
	$result = mysql_query($sql);
	$newsletter = mysql_fetch_assoc($result);
	mysql_free_result($result);

	$sql = 'SELECT * FROM ' . NEWSLETTER_ARCHIVES_TABLE . ' WHERE category_id  = ' . $category_id . ' ORDER BY newsletter_archives_datetime DESC';
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)) {
		$template->assign_block_vars('archiverow', array(
			'U_LINK'	=>	'files/archives/' . $row['newsletter_archives_filename'],
			'T_NAME'	=>	date('d/m/Y', $row['newsletter_archives_datetime']) . ' - ' . $row['newsletter_archives_name_' . $l]
		));
	}
	mysql_free_result($result);

	$sql = 'SELECT newsletter_archives_filename FROM ' . NEWSLETTER_ARCHIVES_TABLE . ' ORDER BY newsletter_archives_datetime DESC LIMIT 1';
	$result = mysql_query($sql);
	$last = mysql_fetch_assoc($result);
	mysql_free_result($result);

	if(isset($_POST['submit'])) {
		$name = trim($_POST['name']);
		$email = trim($_POST['email']);
		
		if(empty($name) or empty($email)) {
			$error[] = $lang['FIELDS_EMPTY'];
		}
		
		if(!preg_match('#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#', $email)) {
			$error[] = $lang['MAIL_NOT_VALID'];
		}
		
		if(count($error) <= 0) {
			
			$mail = new PHPMailer(true);
			$mail->IsHTML(true);
	
			$mail->From = $email;
			$mail->FromName = $email;
	
			$mail->AddAddress('coralie.bancelin@huglo-lepage.com');
			//$mail->AddBCC('guillaume@peopleandtech.com');
	
			$mail->Subject  = 'Huglo Lepage Newsletter Inscription';
			$mail->Body = $email;
			
			$sql = 'INSERT INTO ' . NEWSLETTER_TABLE . $db->sql_insert_update('INSERT', array(
				'newsletter_name'	=>	(string) ucfirst($name),
				'newsletter_email'	=>	(string) strtolower($email)
			));
			mysql_query($sql);
			
			if(@$mail->Send()) {
				$error[] = $lang['NEWSLETTER_SUCCES'];
			} else {
				$error[] = $lang['NEWSLETTER_FAILED'];
			}
		}
	}

	$template->assign_vars(array(
		'T_NEWS'			=>	$newsletter['content_text_' . $l],
		'T_NEWS_NAME'		=>	$category['category_s_name_' . $l],
		'T_C_NAME'			=>	$category['category_s_name_' . $l],

		'T_ERROR'			=>	(count($error) > 0) ? join('<br />', $error) : false,

		'U_SUBMIT'			=>	BASE_PATH . '/newsletter.php' . (($category_id) ? '?category_id=' . $category_id : ''),

		'V_LASTN'			=>	($last['newsletter_archives_filename']) ? true : false,
		'U_LINK'			=>	BASE_PATH . '/files/archives/' . $last['newsletter_archives_filename'],

		'T_NAME_PAGE'		=>	$category['category_s_name_' . $l],
		'V_C_NAME'			=>	$category['category_name_' . $l],
		'V_BG'				=>	true,
		'V_BG_IMG'			=>	$category['category_bg_img'],
		'V_BG_COLOR'		=>	$category['category_bg_color'],
		
		'L_ARCHIVE_BY_DATES'	=>	$lang['ARCHIVE_BY_DATES'],
		'L_SELECT'				=>	$lang['SELECT'],
		'L_NEWSLETTER_DOWNLOAD'	=>	$lang['NEWSLETTER_DOWNLOAD'],
		'L_NAME'				=>	$lang['NAME'],
		'L_ADDRESS_EMAIL'		=>	$lang['ADDRESS_EMAIL'],
	));

	$template->set_filenames(array('body' => 'view_newsletter.html'));
	$template->display('body');

	unset($error);
	$db->sql_close();

?>
