<?php
require_once DIR_ENGINE . 'newsletter.php';
require_once DIR_ENGINE . 'newsletter-admin.php';
if( $a == 'form' ){
	if( isset( $_POST['sOption'] ) ){
		if( isset( $_POST['newsletter_def_content'] ) ){
			$_POST['newsletter_def_content'] = changeTxt( $_POST['newsletter_def_content'], 'Nds' );
			saveVariables( $_POST, DB_CONFIG );
			saveVariables( $_POST, DB_CONFIG_LANG );
			header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=save&activeTabId=newsletter-config' );
			exit;
		} else{
			if( !isset( $aUrl ) ){
				$aUrl = parse_url( $_SERVER['REQUEST_URI'] );
				if( !isset( $aUrl['host'] ) ) $aUrl['host'] = $_SERVER['SERVER_NAME'];
				if( !isset( $aUrl['path'] ) ) $aUrl['path'] = null;
				if( isset( $aUrl['query'] ) ) $aUrl['query'] = '?' . $aUrl['query'];
				else $aUrl['query'] = null;
			}
			$aUrl['path'] = str_replace( 'admin.php', '', $aUrl['path'] );
			$oldLang = $lang;
			unset( $lang );
			require( DIR_LANG . LANGUAGE . '.php' );
			$clientLang = $lang;
			$lang = $oldLang;
			//Przygotowywanie listy e-maili zgodnie z wyborem
			$aEmails = Array( );
			if( isset( $_POST['aSendTo'] ) && is_array( $_POST['aSendTo'] ) ) foreach( $_POST['aSendTo'] as $sSendParam ){
				switch( $sSendParam ){
					case 'subscribers':
						$aNewEmails = throwNewsletterEmails( );
					break;
					case 'all-clients':
						$aNewEmails = $oDB->executeSQL( 'SELECT `sEmail` FROM `' . $oDB->throwTableName( DB_USERS ) . '`', true, DataBase::SINGLE_COLUMN );
					break;
					case 'buyers':
						$aNewEmails = $oDB->executeSQL( 'SELECT `sEmail` FROM `' . $oDB->throwTableName( DB_USERS ) . '` `u` WHERE EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` `o` WHERE `u`.`iUser` = `o`.`iUser` LIMIT 1 )', true, DataBase::SINGLE_COLUMN );
					break;
					default:
						if( preg_match( '/group\\-([0-9]+)/', $sSendParam, $aMatches ) ) $aNewEmails = $oDB->executeSQL( 'SELECT `sEmail` FROM `' . $oDB->throwTableName( DB_USERS ) . '` `u` WHERE EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` `o` WHERE `u`.`iUser` = `o`.`iUser` AND `o`.`iGroup` = ' . intval( $aMatches[1] ) . ' LIMIT 1 )', true, DataBase::SINGLE_COLUMN );
						else $aNewEmails = Array( );
					}
					$aEmails = array_merge( $aEmails, $aNewEmails );
				}
				$aEmails = array_flip( array_flip( $aEmails ) );
				$bSend = false;
				foreach( array_chunk( $aEmails, 30 ) as $aSendEmails ){
					$m_h = "From: " . $config['newsletter_email'] . "\n";
					$m_h.= "MIME-Version: 1.0\n";
					$m_h.= "Content-Transfer-Encoding: 8bit\n";
					$m_h.= "Content-Type: text/html; charset=\"utf-8\"\n";
					$m_h.= "Bcc: " . implode( ', ', $aSendEmails ) . "\n";
					require_once( $GLOBALS['config']['dir_libraries'] . 'emogrifier.php' );
					$aUrl = parse_url( 'http' . ( $_SERVER['HTTPS'] ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
					$aUrl['path'] = preg_replace( '|/[^/]*$|', '/', $aUrl['path'] );
					$oTpl->assign( 'aUrl', $aUrl );
					$oTpl->assign( 'newsletter_content', $_POST['newsletter_content'] );
					$oTpl->assign( 'sTitle', $_POST['sTitle'] );
					$oTpl->assign( 'clientLang', $clientLang );
					$sHtml = $oTpl->parse( 'newsletter_html.tpl' );
					$sCss = file_get_contents( $config['dir_templates'] . 'style_orders_email.css' );
					$oEm = new Emogrifier( $sHtml, $sCss );
					$m_b = $oEm->emogrify( );
					$m_b.= "\n";
					$m_h = str_replace( "\n", PHP_EOL, str_replace( "\r", '', $m_h ) );
					$m_b = str_replace( "\n", PHP_EOL, str_replace( "\r", '', $m_b ) );
					if( DEBUG_SELLSMART ) file_put_contents( 'email_log.txt', var_export( Array( '', '=?UTF-8?B?' . base64_encode( $_POST['sTitle'] ) . '?=', $m_b, $m_h ), true ) . "\n\n", FILE_APPEND );
					$bSend = @email( '', '=?UTF-8?B?' . base64_encode( $_POST['sTitle'] ) . '?=', $m_b, $m_h ) || $bSend;
				}
				if( $bSend ){
					$content.= $oTpl->parse( 'message_done.tpl' );
				} else{
					$content.= $oTpl->parse( 'message_newsletter_error.tpl' );
				}
			}
	} else{
		if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
		$aNewsletterEmails = throwNewsletterEmails( );
		$oTpl->assign( 'aNewsletterEmails', $aNewsletterEmails );
		$newsletter_content = changeTxt( $config['newsletter_def_content'], 'nlNds' );
		$newsletter_content = htmlspecialchars_decode( $newsletter_content );
		$newsletter_def_content = changeTxt( $config['newsletter_def_content'], 'nlNds' );
		$newsletter_def_content = htmlspecialchars_decode( $newsletter_def_content );
		$aUserGroups = throwUsersGroups( LANGUAGE );
		$oTpl->assign( 'aUserGroups', $aUserGroups );
		$oTpl->assign( 'newsletter_content', $newsletter_content );
		$oTpl->assign( 'newsletter_def_content', $newsletter_def_content );
		$content.= $oTpl->parse( 'newsletter_form.tpl' );
	}
} elseif( $a == 'delete' && isset( $sEmail ) && !empty( $sEmail ) ){
	deleteNewsletter( $sEmail );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=del&activeTabId=newsletter-edit' );
	exit;
}
?>