<?php
/*======================================================================================
Module: Articles
======================================================================================*/

// Check authorisation
if (!isset($gCms)) exit;

// Get preferences
$urlstart = $this->GetPreference('urlstart') ? $this->GetPreference('urlstart') : 'Articles';
$availibility_use = $this->GetPreference('availibility_use');
$availibility_count = $this->GetPreference('availibility_count');
$availibility_show = $this->GetPreference('availibility_show');
$mode_categories = $this->GetPreference('mode_categories') ? $this->GetPreference('mode_categories') : 'Categories';
$mode_articles = $this->GetPreference('mode_articles') ? $this->GetPreference('mode_articles') : 'Articles';
$mode_article = $this->GetPreference('mode_article') ? $this->GetPreference('mode_article') : 'Article';
$mode_cart = $this->GetPreference('mode_cart') ? $this->GetPreference('mode_cart') : 'Cart';
$mode_cartsnippet = $this->GetPreference('mode_cartsnippet') ? $this->GetPreference('mode_cartsnippet') : 'CartSnippet';
$mode_form = $this->GetPreference('mode_form') ? $this->GetPreference('mode_form') : 'Form';
$mode_summary = $this->GetPreference('mode_summary') ? $this->GetPreference('mode_summary') : 'Summary';
$mode_ack = $this->GetPreference('mode_ack') ? $this->GetPreference('mode_ack') : 'Ack';

// Smarty parameters
$inline = isset($params['inline']) ? (int)$params['inline'] : true;
if (!$inline) $inline = false;
$page_cart = isset($params['page_cart']) ? urldecode(trim($params['page_cart'])) : '';
if ($page_cart && (int)$page_cart<1) $page_cart = $db->GetOne('SELECT content_id FROM '.cms_db_prefix().'content WHERE content_alias = ?', array($page_cart));
$page_categories = isset($params['page_categories']) ? urldecode(trim($params['page_categories'])) : '';
if ($page_categories && (int)$page_categories<1) $page_categories = $db->GetOne('SELECT content_id FROM '.cms_db_prefix().'content WHERE content_alias = ?', array($page_categories));
$page_articles = isset($params['page_articles']) ? urldecode(trim($params['page_articles'])) : '';
if ($page_articles && (int)$page_articles<1) $page_articles = $db->GetOne('SELECT content_id FROM '.cms_db_prefix().'content WHERE content_alias = ?', array($page_articles));
$page_article = isset($params['page_article']) ? urldecode(trim($params['page_article'])) : '';
if ($page_article && (int)$page_article<1) $page_article = $db->GetOne('SELECT content_id FROM '.cms_db_prefix().'content WHERE content_alias = ?', array($page_article));
$page_summary = isset($params['page_summary']) ? urldecode(trim($params['page_summary'])) : '';
if ($page_summary && (int)$page_summary<1) $page_summary = $db->GetOne('SELECT content_id FROM '.cms_db_prefix().'content WHERE content_alias = ?', array($page_summary));
$page_ack = isset($params['page_ack']) ? urldecode(trim($params['page_ack'])) : '';
if ($page_ack && (int)$page_ack<1) $page_ack = $db->GetOne('SELECT content_id FROM '.cms_db_prefix().'content WHERE content_alias = ?', array($page_ack));
$template_cart = isset($params['template_cart']) ? urldecode(trim($params['template_cart'])) : 'cart';
$template_cartsnippet = isset($params['template_cartsnippet']) ? urldecode(trim($params['template_cartsnippet'])) : 'cartsnippet';
$template_summary = isset($params['template_summary']) ? urldecode(trim($params['template_summary'])) : 'summary';
$template_ack = isset($params['template_ack']) ? urldecode(trim($params['template_ack'])) : 'ack';
$template = isset($params['template']) ? urldecode(trim($params['template'])) : '';
$email_template_owner = isset($params['email_template_owner']) ? urldecode(trim($params['email_template_owner'])) : 'email_owner';
$email_template_customer = isset($params['email_template_customer']) ? urldecode(trim($params['email_template_customer'])) : 'email_customer';
$returnid = isset($returnid) ? (int)$returnid : 0;
$returnid = isset($params['returnid']) ? (int)$params['returnid'] : $returnid;
// Parameters for Smarty and Requests
// Get Module-Parameters
$mparams = GetModuleParameters($id);
// Get Request-Module-ID
$mmact = isset($_REQUEST['mact']) ? explode(',',$_REQUEST['mact']) : array();
$mid = isset($mmact[1]) ? $mmact[1] : '';
// Parameter to set the output mode
$mode = '';
if (!isset($mparams['mode'])) $mode = isset($_REQUEST[$mid.'mode']) ? $_REQUEST[$mid.'mode'] : '';
if (!$mode && isset($params['mode']))  $mode = $params['mode']; // Available modes: cart,cartsnippet,summary,ack - default: cart
if (!$mode) $mode = $mode_cart;
$mode = isset($params['forcemode']) ? $params['forcemode'] : $mode; // Available modes: cart,cartsnippet,summary,ack - default: cart
if (!$mode) $mode = $mode_cart;

// Testing snippet for parameters
$smarty->assign('debug','<div style="display:block;overflow:auto;background:#fff;color:#000;padding:5px;border:2px solid #f00;"><u>$_REQUEST</u>:<br />'.print_r($_REQUEST,true).'<hr /><u>$_POST</u>:<br />'.print_r($_POST,true).'<hr /><u>$_GET</u>:<br />'.print_r($_GET,true).'<hr /><u>GetModuleParameters($id=\''.$id.'\')</u>:<br />'.print_r(GetModuleParameters($id),true).'<hr /><u>Variablen</u>:<br />Category: '.$category.'<br />Mode: '.$mode.'<br />Template: '.$template.'<br /><hr /><u>$params</u>:<br />'.print_r($params,true).'</div>');
$smarty->assign('debug_session','<div style="display:block;overflow:auto;background:#fff;color:#000;padding:5px;border:2px solid #f00;"><pre><u>$_SESSION</u>:<br />'.print_r($_SESSION,true).'</pre></div>');
#echo '<pre>';print_r($params);echo '</pre>';exit;

// Add article to card
if ($mode=='cartadd') {

	// Get Return ID
	if ($page_article) $returnid = $page_article;
	// Get params
	$article = (int)$params['articleid'];
	if (!$article) { header('Location: '.trim($_SERVER['HTTP_REFERER']));exit; }

	// Get article data
	$articledata = $db->GetRow('SELECT seo_url,returnid,title,availibility FROM '.cms_db_prefix().'module_articles_articles WHERE active=1 AND entry_id = ? LIMIT 1', array($article));
	if (!$articledata) { header('Location: '.trim($_SERVER['HTTP_REFERER']));exit; }

	// Build article URL
	$url = $articledata['seo_url'];
	if (!$url) {
		if (!$returnid && $articledata['returnid']) $returnid = $articledata['returnid'];
		if (!$returnid) $returnid = $this->GetPreference('returnid');
	}
	$url = $this->CreateLink($id, 'cart', $returnid, '', array('article'=>$article,'mode'=>'article'), '', true, false, '', false, $articledata['seo_url'] ? trim($articledata['seo_url']) : $this->PageID2Url($returnid).'/'.$returnid.'/'.$mode_article.'/'.$article.'/'.$this->UrlSeoDecode($articledata['title']));

	// Get out current amount of this article
	$amount = (int)$params['amount'];
	$availibility = (int)$articledata['availibility'];
	if ($availibility_use && $amount>$availibility) $amount = $availibility;
	if (!isset($_SESSION['articles'])) $_SESSION['articles'] = array();
	$c_amount = isset($_SESSION['articles']['cart_'.$article]) ? (int)$_SESSION['articles']['cart_'.$article] : 0;

	// Add article to sessions
	$cart_amount = $c_amount + $amount;
	if ($availibility_use) { $_SESSION['articles']['cart_'.$article] = $cart_amount>$availibility ? $availibility : $cart_amount; }
	else { $_SESSION['articles']['cart_'.$article] = $cart_amount; }

	// Return to article page
	header('Location: '.trim($url));exit();

}

// Check updates at cart form
if (isset($params['update_cart']) || isset($params['update_cart_x'])) {

	// Get Return ID
	if ($page_cart) $returnid = $page_cart;

	// Get params
	if (!isset($_SESSION['articles'])) { header('Location: '.trim($_SERVER['HTTP_REFERER']));exit; }

	// Update order amount of the articles
	if (is_array($params['article_amount'])) {
		foreach ($params['article_amount'] as $k=>$v) {
			$article = (int)$k;
			$availibility = (int)$db->GetOne('SELECT availibility FROM '.cms_db_prefix().'module_articles_articles WHERE active=1 AND entry_id = ? LIMIT 1', array($article));
			$amount = (int)$v;
			if ($availibility_use && $amount>$availibility) $amount = $availibility;
			$_SESSION['articles']['cart_'.$article] = $amount;
		}
	}

	// Delete articles
	if (is_array($params['article_delete'])) {
		foreach ($params['article_delete'] as $k=>$v) {
			$article = (int)$k;
			unset($_SESSION['articles']['cart_'.$article]);
		}
	}

	// Redirect to the cart
	if (count($_SESSION['articles'])>0) {
		// Set link to the full cart
		if (!$page_cart || (int)$page_cart<1) $page_cart = $returnid;
		$url = $this->CreateLink($id, 'cart', $page_cart, '', array('mode'=>$mode_cart), '', true, $inline, '', false, $this->PageID2Url($page_cart).'/'.$page_cart.'/'.$mode_cart);
		// Return to article page
		header('Location: '.trim($url));exit();
	}
	// Redirect to shop page, if cart is empty
	else {
		// Set link to the shop startpage
		$url = $this->CreateLink($id, 'default', $page_categories, '', $category ? array('category'=>$category,'mode'=>$mode_categories) : array('mode'=>$mode_categories), '', true, $inline, '', false, $this->PageID2Url($page_categories).'/'.$page_categories.'/'.$mode_categories);
		// Return to article page
		header('Location: '.trim($url));exit();
	}

}

// Delete cart
if (isset($params['delete_cart']) || isset($params['delete_cart_x'])) {

	// Delete Session vars
	if (isset($_SESSION['articles'])) { unset($_SESSION['articles']); }
	if (isset($_SESSION['articles_order'])) { unset($_SESSION['articles_order']); }

	// Set link to the shop startpage
	$url = $this->CreateLink($id, 'default', $page_categories, '', $category ? array('category'=>$category,'mode'=>$mode_categories) : array('mode'=>$mode_categories), '', true, $inline, '', false, $this->PageID2Url($page_categories).'/'.$page_categories.'/'.$mode_categories);

	// Return to article page
	header('Location: '.trim($url));exit();

}

// After sending the cart and order form
if (isset($params['send_cart']) || isset($params['send_cart_x'])) {

	// Get Return ID
	if ($page_cart) $returnid = $page_cart;

	// Get params
	if (!isset($_SESSION['articles'])) { header('Location: '.trim($_SERVER['HTTP_REFERER']));exit; }

	// Get and check form data
	if (!isset($_SESSION['articles_order'])) $_SESSION['articles_order'] = array();

	$formerrors = array();

	// salutation
	$aform_salutation = isset($params['aform_salutation']) ? (int)$params['aform_salutation'] : 0;
	$_SESSION['articles_order']['salutation'] = $aform_salutation;
	// firstname
	$aform_firstname = isset($params['aform_firstname']) ? trim($params['aform_firstname']) : '';
	$_SESSION['articles_order']['firstname'] = $aform_firstname;
	if (!$aform_firstname) $formerrors['aform_firstname'] = $this->Lang('no_value',$this->Lang('firstname'));
	// lastname
	$aform_lastname = isset($params['aform_lastname']) ? trim($params['aform_lastname']) : '';
	$_SESSION['articles_order']['lastname'] = $aform_lastname;
	if (!$aform_lastname) $formerrors['aform_lastname'] = $this->Lang('no_value',$this->Lang('lastname'));
	// org
	$aform_org = isset($params['aform_org']) ? trim($params['aform_org']) : '';
	$_SESSION['articles_order']['org'] = $aform_org;
	// tel
	$aform_tel = isset($params['aform_tel']) ? trim($params['aform_tel']) : '';
	$_SESSION['articles_order']['tel'] = $aform_tel;
	// mail
	$aform_mail = isset($params['aform_mail']) ? trim($params['aform_mail']) : '';
	$_SESSION['articles_order']['mail'] = $aform_mail;
	if (!$aform_mail) $formerrors['aform_mail'] = $this->Lang('no_value',$this->Lang('mail'));
	// street
	$aform_street = isset($params['aform_street']) ? trim($params['aform_street']) : '';
	$_SESSION['articles_order']['street'] = $aform_street;
	if (!$aform_street) $formerrors['aform_street'] = $this->Lang('no_value',$this->Lang('street'));
	// plz
	$aform_plz = isset($params['aform_plz']) ? preg_replace('/[^0-9]/','',$params['aform_plz']) : '';
	$_SESSION['articles_order']['plz'] = $aform_plz;
	if (!$aform_plz) $formerrors['aform_plz'] = $this->Lang('wrong_value',$this->Lang('plz'));
	// city
	$aform_city = isset($params['aform_city']) ? trim($params['aform_city']) : '';
	$_SESSION['articles_order']['city'] = $aform_city;
	if (!$aform_city) $formerrors['aform_city'] = $this->Lang('no_value',$this->Lang('city'));
	// country
	$aform_country = isset($params['aform_country']) ? trim($params['aform_country']) : '';
	$_SESSION['articles_order']['country'] = $aform_country;
	if (!$aform_country) $formerrors['aform_country'] = $this->Lang('no_value',$this->Lang('country'));
	// newsletter
	$aform_newsletter = isset($params['aform_newsletter']) ? 1 : 0;
	$_SESSION['articles_order']['newsletter'] = $aform_newsletter;
	// notice
	$aform_notice = isset($params['aform_notice']) ? trim($params['aform_notice']) : '';
	$_SESSION['articles_order']['notice'] = $aform_notice;
	// payment
	$aform_payment = isset($params['aform_payment']) ? (int)$params['aform_payment'] : 0;
	$_SESSION['articles_order']['payment'] = $aform_payment;
	if (!isset($params['aform_payment'])) $formerrors['aform_payment'] = $this->Lang('no_value',$this->Lang('payment'));
	// bank
	$aform_bank = isset($params['aform_bank']) ? trim($params['aform_bank']) : '';
	$_SESSION['articles_order']['bank'] = $aform_bank;
	if ($aform_payment==2 && !$aform_bank) $formerrors['aform_bank'] = $this->Lang('no_value',$this->Lang('bank'));
	// blz
	$aform_blz = isset($params['aform_blz']) ? preg_replace('/[^0-9]/','',$params['aform_blz']) : '';
	$_SESSION['articles_order']['blz'] = $aform_blz;
	if ($aform_payment==2 && !$aform_blz) $formerrors['aform_blz'] = $this->Lang('no_value',$this->Lang('blz'));
	// konto
	$aform_konto = isset($params['aform_konto']) ? preg_replace('/[^0-9]/','',$params['aform_konto']) : '';
	$_SESSION['articles_order']['konto'] = $aform_konto;
	if ($aform_payment==2 && !$aform_konto) $formerrors['aform_konto'] = $this->Lang('no_value',$this->Lang('konto'));
	// inhaber
	$aform_inhaber = isset($params['aform_inhaber']) ? trim($params['aform_inhaber']) : '';
	$_SESSION['articles_order']['inhaber'] = $aform_inhaber;
	if ($aform_payment==2 && !$aform_inhaber) $formerrors['aform_inhaber'] = $this->Lang('no_value',$this->Lang('inhaber'));

	// Redirect to Summary Page, if no error
	if (count($formerrors)==0) {
		// Set link to the full cart
		if (!$page_summary || (int)$page_summary<1) $page_summary = $returnid;
		$url = $this->CreateLink($id, 'cart', $page_summary, '', array('mode'=>$mode_summary), '', true, $inline, '', false, $this->PageID2Url($page_summary).'/'.$page_summary.'/'.$mode_summary);
		// Return to article page
		header('Location: '.trim($url));exit();
	}

}

// After sending the summary
if (isset($params['send_summary']) || isset($params['send_summary_x'])) {

	// Get Return ID
	if ($page_summary) $returnid = $page_summary;

	// Get params
	if (!isset($_SESSION['articles'])) { header('Location: '.trim($_SERVER['HTTP_REFERER']));exit; }

	// Get and check form data
	if (!isset($_SESSION['articles_order'])) { header('Location: '.trim($_SERVER['HTTP_REFERER']));exit; }

	$formerrors = array();

	// agb
	$aform_agb = isset($params['aform_agb']) ? 1 : 0;
	$_SESSION['articles_order']['agb'] = $aform_agb;
	if (!$aform_agb) $formerrors['aform_agb'] = $this->Lang('no_agb');

	// Redirect to Ack Page, if no error
	if (count($formerrors)==0) {
		// Set link to the full cart
		if (!$page_ack || (int)$page_ack<1) $page_ack = $returnid;
		$url = $this->CreateLink($id, 'cart', $page_ack, '', array('mode'=>$mode_ack), '', true, $inline, '', false, $this->PageID2Url($page_ack).'/'.$page_ack.'/'.$mode_ack);
		// Return to ack page
		header('Location: '.trim($url));exit();
	}

}

// In the Summary: Button "Modify order-data" pressed
if (isset($params['data_change']) || isset($params['data_change_x'])) {

	// Get Return ID
	if ($page_cart) $returnid = $page_cart;

	// Set link to the full cart
	if (!$page_cart || (int)$page_cart<1) $page_cart = $returnid;
	$url = $this->CreateLink($id, 'cart', $page_cart, '', array('mode'=>$mode_cart), '', true, $inline, '', false, $this->PageID2Url($page_cart).'/'.$page_cart.'/'.$mode_cart);
	// Return to cart page
	header('Location: '.trim($url));exit();

}

// Button "Back to the shop" pressed
if (isset($params['to_shop']) || isset($params['to_shop_x'])) {

	// Get Return ID
	if ($page_categories) $returnid = $page_categories;

	// Set link to shop start page
	if (!$page_categories || (int)$page_categories<1) $page_categories = $returnid;
	$url = $this->CreateLink($id, 'default', $page_categories, '', array('mode'=>$mode_categories), '', true, $inline, '', false, $this->PageID2Url($page_categories).'/'.$page_categories.'/'.$mode_categories);
	// Return to article page
	header('Location: '.trim($url));exit();

}

// Show full cart
if ($mode==$mode_cart || $mode==$mode_summary || $mode==$mode_ack) {

	if ($mode==$mode_cart) {
		// Set template + ReturnID
		if (!$template) $template = $template_cart;
		if ($page_cart) $returnid = $page_cart;
		if (!$returnid) $returnid = $this->GetPreference('returnid');
	}
	if ($mode==$mode_summary) {
		// Set template + ReturnID
		if (!$template) $template = $template_summary;
		if ($page_summary) $returnid = $page_summary;
		if (!$returnid) $returnid = $this->GetPreference('returnid');
	}
	if ($mode==$mode_ack) {
		// Set template + ReturnID
		if (!$template) $template = $template_ack;
		if ($page_ack) $returnid = $page_ack;
		if (!$returnid) $returnid = $this->GetPreference('returnid');
	}

	// Get/Set error array
	$formerrors = isset($formerrors) ? $formerrors : array();

	// Get form data
	if (!isset($aform_salutation)) $aform_salutation = isset($_SESSION['articles_order']['salutation']) ? (int)$_SESSION['articles_order']['salutation'] : 0;
	if (!isset($aform_firstname)) $aform_firstname = isset($_SESSION['articles_order']['firstname']) ? $_SESSION['articles_order']['firstname'] : '';
	if (!isset($aform_lastname)) $aform_lastname = isset($_SESSION['articles_order']['lastname']) ? $_SESSION['articles_order']['lastname'] : '';
	if (!isset($aform_org)) $aform_org = isset($_SESSION['articles_order']['org']) ? $_SESSION['articles_order']['org'] : '';
	if (!isset($aform_tel)) $aform_tel = isset($_SESSION['articles_order']['tel']) ? $_SESSION['articles_order']['tel'] : '';
	if (!isset($aform_mail)) $aform_mail = isset($_SESSION['articles_order']['mail']) ? $_SESSION['articles_order']['mail'] : '';
	if (!isset($aform_street)) $aform_street = isset($_SESSION['articles_order']['street']) ? $_SESSION['articles_order']['street'] : '';
	if (!isset($aform_plz)) $aform_plz = isset($_SESSION['articles_order']['plz']) ? preg_replace('/[^0-9]/','',$_SESSION['articles_order']['plz']) : '';
	if (!isset($aform_city)) $aform_city = isset($_SESSION['articles_order']['city']) ? $_SESSION['articles_order']['city'] : '';
	if (!isset($aform_country)) $aform_country = isset($_SESSION['articles_order']['country']) ? $_SESSION['articles_order']['country'] : '';
	if (!isset($aform_agb)) $aform_agb = isset($_SESSION['articles_order']['agb']) ? (int)$_SESSION['articles_order']['agb'] : 0;
	if (!isset($aform_newsletter)) $aform_newsletter = isset($_SESSION['articles_order']['newsletter']) ? (int)$_SESSION['articles_order']['newsletter'] : 0;
	if (!isset($aform_notice)) $aform_notice = isset($_SESSION['articles_order']['notice']) ? $_SESSION['articles_order']['notice'] : '';
	if (!isset($aform_payment)) $aform_payment = isset($_SESSION['articles_order']['payment']) ? (int)$_SESSION['articles_order']['payment'] : 0;
	if (!isset($aform_bank)) $aform_bank = isset($_SESSION['articles_order']['bank']) ? $_SESSION['articles_order']['bank'] : '';
	if (!isset($aform_blz)) $aform_blz = isset($_SESSION['articles_order']['blz']) ? preg_replace('/[^0-9]/','',$_SESSION['articles_order']['blz']) : '';
	if (!isset($aform_konto)) $aform_konto = isset($_SESSION['articles_order']['konto']) ? preg_replace('/[^0-9]/','',$_SESSION['articles_order']['konto']) : '';
	if (!isset($aform_inhaber)) $aform_inhaber = isset($_SESSION['articles_order']['inhaber']) ? $_SESSION['articles_order']['inhaber'] : '';

	// Get cart
	$l_cart = isset($_SESSION['articles']) ? $_SESSION['articles'] : array();
	if (!$l_cart) {
		$smarty->assign('cart_articles',array());
		$smarty->assign('cart_count',0);
		$smarty->assign('cart_price','0.00');
		$smarty->assign('cart_vat','0.00');
		$smarty->assign('cart_shipping_costs','0.00');
	} else {
		$rowarray = array();
		$rowclass = 'row1';
		$sum = array ( 'amount'=>0, 'price'=>0, 'vat'=>0, 'shipping_costs'=>0 );
		$cartsum = "ID,Article,Amount,Price,Sum,VAT,Shipping Costs\n";
		foreach ($l_cart as $c_article=>$articleamount) {
			// Get article details
			$articleid = (int)str_replace('cart_','',$c_article);
			$articledetails = $db->GetRow('SELECT entry_id,title,short_desc,returnid,seo_url,image_url,price,vat,shipping_costs FROM '.cms_db_prefix().'module_articles_articles WHERE active=1 AND entry_id = ? LIMIT 1', array($articleid));
			if (count($articledetails)>0) {
				// Build a new row
				$row = new StdClass();
				$row->rowclass = $rowclass;
				// Get data in array
				$row->article = (int)$articledetails['entry_id'];
				$row->amount = (int)$articleamount;
				$row->title = $articledetails['title'];
				$row->summary = $articledetails['short_desc'];
				$row->price = number_format(str_replace(',','.',$articledetails['price']), 2, ',', '.').' '.$this->GetPreference('currency');
				$row->sum = number_format(str_replace(',','.',($articledetails['price'] * (int)$articleamount)), 2, ',', '.').' '.$this->GetPreference('currency');
				$row->vat = number_format(str_replace(',','.',($row->sum - ($row->sum / (1 + ($articledetails['vat'] / 100))))), 2, ',', '.').' '.$this->GetPreference('currency');
				$row->vatp = $articledetails['vat'];
				$row->shipping_costs = number_format(str_replace(',','.',$articledetails['shipping_costs']), 2, ',', '.').' '.$this->GetPreference('currency');
				$row->returnid = $articledetails['returnid'];
				if (!$row->returnid) $row->returnid = $returnid;
				$row->seo_url = $articledetails['seo_url'];
				$row->image = $articledetails['image_url'] ? $config['image_uploads_url'].'/Articles/articles/'.$ar['entry_id'].'/'.$ar['image_url'] : '';
				$row->articlelink = $this->CreateLink($id, 'default', $returnid, '', $category ? array('article'=>$row->article,'category'=>$category,'mode'=>$mode_article) : array('article'=>$row->article,'mode'=>'article'), '', true, false, '', false, $row->seo_url ? $row->seo_url : $this->PageID2Url($returnid).'/'.$returnid.'/'.$mode_article.'/'.$row->article.'/'.$this->UrlSeoDecode($row->title));
				// form fields
				$row->articleamount = $this->CreateInputText($id,'article_amount['.$row->article.']',$row->amount,3,3); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
				$row->articledelete = $this->CreateInputCheckbox($id, 'article_delete['.$row->article.']', 'true', 'false'); // $id, $name, $value='', $selectedvalue='', $addttext=''
				// increase price sum
				$sum['price']+= (float)$articledetails['price'] * (int)$articleamount;
				$sum['shipping_costs'] = $articledetails['shipping_costs']>$sum['shipping_costs'] ? $articledetails['shipping_costs'] : $sum['shipping_costs'];
				$sum['vat']+= $sum['price'] - ($sum['price'] / (1 + ($articledetails['vat'] / 100)));
				// Merge new domain to array
				array_push ($rowarray, $row);
				// Alternate row color
				($rowclass == 'row1' ? $rowclass = 'row2' : $rowclass = 'row1');
				// increase amount sum
				$sum['amount']+= $articleamount;
				// add cart value for logging
				$cartsum.= $row->article.','.$row->title.','.$row->amount.','.$row->price.','.$row->sum.','.$row->shipping_costs."\n";
			}
		}
		// assign global smarty vars
		$smarty->assign('cart_articles',$rowarray);
		$smarty->assign('cart_count',$sum['amount']);
		$smarty->assign('cart_price',number_format(str_replace(',','.',$sum['price']), 2, ',', '.').' '.$this->GetPreference('currency'));
		$smarty->assign('cart_vat',number_format(str_replace(',','.',$sum['vat']), 2, ',', '.').' '.$this->GetPreference('currency'));
		#$vat = (float)($sum['price'] - ($sum['price'] / 1.19));
		#$smarty->assign('cart_vat',number_format(str_replace(',','.',$vat), 2, ',', '.').' '.$this->GetPreference('currency'));
		$smarty->assign('cart_shipping_costs',number_format(str_replace(',','.',$sum['shipping_costs']), 2, ',', '.').' '.$this->GetPreference('currency'));
		$smarty->assign('cart_cash_on_delivery',number_format(str_replace(',','.',$this->GetPreference('cash_on_delivery')), 2, ',', '.').' '.$this->GetPreference('currency'));
		$sum_all = (float)str_replace(',','.',$sum['price']) + (float)str_replace(',','.',$sum['shipping_costs']);
		if ($aform_payment==1) $sum_all = (float)$sum_all + (float)str_replace(',','.',$this->GetPreference('cash_on_delivery'));
		$smarty->assign('cart_sum_all',number_format($sum_all, 2, ',', '.').' '.$this->GetPreference('currency'));
		$sum_all_5 = (float)str_replace(',','.',$sum['price']) + (float)str_replace(',','.',$sum['shipping_costs']) + 5;
		if ($aform_payment==1) $sum_all_5 = (float)$sum_all_5 + (float)str_replace(',','.',$this->GetPreference('cash_on_delivery'));
		$smarty->assign('cart_sum_all_5',number_format($sum_all_5, 2, ',', '.').' '.$this->GetPreference('currency'));
	}

	// Assign order form smarty vars
	$smarty->assign('formid',$id);
	$smarty->assign('formstart',$this->CreateFrontendFormStart($id,$returnid,'cart','post','multipart/form-data',$inline,'',array())); // $id,$returnid,$action='default',$method='post',$enctype='',$inline=true,$idsuffix='',$params=array()
	#$smarty->assign('formstart','<form id="'.$id.'moduleform_1" method="post" action="'.$_SERVER['REQUEST_URI'].'" class="cms_form" enctype="multipart/form-data">');
	$smarty->assign('formend', $this->CreateFormEnd());
	$smarty->assign('submit_cart', $this->CreateInputSubmit($id,'update_cart',$this->Lang('update_cart'))); // $id, $name, $value='', $addttext='', $image='', $confirmtext=''
	$smarty->assign('delete_cart', $this->CreateInputSubmit($id,'delete_cart',$this->Lang('delete_cart'))); // $id, $name, $value='', $addttext='', $image='', $confirmtext=''
	$smarty->assign('submit_order', $this->CreateInputSubmit($id,'send_cart',$this->Lang('send_cart'))); // $id, $name, $value='', $addttext='', $image='', $confirmtext=''
	$smarty->assign('submit_summary', $this->CreateInputSubmit($id,'send_summary',$this->Lang('send_summary'))); // $id, $name, $value='', $addttext='', $image='', $confirmtext=''
	$smarty->assign('submit_datachange', $this->CreateInputSubmit($id,'data_change',$this->Lang('data_change'))); // $id, $name, $value='', $addttext='', $image='', $confirmtext=''
	$smarty->assign('submit_toshop', $this->CreateInputSubmit($id,'to_shop',$this->Lang('to_shop'))); // $id, $name, $value='', $addttext='', $image='', $confirmtext=''
	$smarty->assign('mode', $this->CreateInputHidden($id,'mode',$mode)); // $id, $name, $value='', $addttext=''
	$smarty->assign('aprompt_salutation',$this->Lang('salutation'));
	$salutations = array ( $this->Lang('mr')=>0, $this->Lang('ms')=>1 );
	$smarty->assign('aform_salutation', $mode==$mode_cart ? $this->CreateInputDropdown($id,'aform_salutation',$salutations,0, $aform_salutation) : $aform_salutation); // $id, $name, $items, $selectedindex=-1, $selectedvalue='', $addttext=''
	$smarty->assign('aprompt_firstname',$this->Lang('firstname'));
	$smarty->assign('aform_firstname', $mode==$mode_cart ? $this->CreateInputText($id,'aform_firstname', $aform_firstname,20,100) : $aform_firstname); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_firstname_error',isset($formerrors['aform_firstname']) ? $formerrors['aform_firstname'] : '');
	$smarty->assign('aprompt_lastname',$this->Lang('lastname'));
	$smarty->assign('aform_lastname', $mode==$mode_cart ? $this->CreateInputText($id,'aform_lastname', $aform_lastname,20,100) : $aform_lastname); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_lastname_error',isset($formerrors['aform_lastname']) ? $formerrors['aform_lastname'] : '');
	$smarty->assign('aprompt_org',$this->Lang('org'));
	$smarty->assign('aform_org', $mode==$mode_cart ? $this->CreateInputText($id,'aform_org', $aform_org,20,100) : $aform_org); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_tel',$this->Lang('tel'));
	$smarty->assign('aform_tel', $mode==$mode_cart ? $this->CreateInputText($id,'aform_tel', $aform_tel,20,100) : $aform_tel); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_mail',$this->Lang('mail'));
	$smarty->assign('aform_mail', $mode==$mode_cart ? $this->CreateInputText($id,'aform_mail', $aform_mail,20,100) : $aform_mail); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_mail_error',isset($formerrors['aform_mail']) ? $formerrors['aform_mail'] : '');
	$smarty->assign('aprompt_street',$this->Lang('street'));
	$smarty->assign('aform_street', $mode==$mode_cart ? $this->CreateInputText($id,'aform_street', $aform_street,20,100) : $aform_street); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_street_error',isset($formerrors['aform_street']) ? $formerrors['aform_street'] : '');
	$smarty->assign('aprompt_plz_city',$this->Lang('plz_city'));
	$smarty->assign('aform_plz', $mode==$mode_cart ? $this->CreateInputText($id,'aform_plz', $aform_plz,5,5) : $aform_plz); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_plz_error',isset($formerrors['aform_plz']) ? $formerrors['aform_plz'] : '');
	$smarty->assign('aform_city', $mode==$mode_cart ? $this->CreateInputText($id,'aform_city', $aform_city,20,100) : $aform_city); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_city_error',isset($formerrors['aform_city']) ? $formerrors['aform_city'] : '');
	$smarty->assign('aprompt_country',$this->Lang('country'));
	$p_countries = trim($this->GetPreference('countries'));
	if ($p_countries) {
		$countries = array();
		$a_countries = explode(',',$p_countries);
		foreach ($a_countries as $a_country) { $countries[$a_country] = $a_country; }
	} else {
		$countries = array();
	}
	$smarty->assign('aform_country', $mode==$mode_cart ? $this->CreateInputDropdown($id,'aform_country',$countries,0, $aform_country) : $aform_country); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_country_error',isset($formerrors['aform_country']) ? $formerrors['aform_country'] : '');
	$smarty->assign('aprompt_payment',$this->Lang('payment'));
	$payment = array ( $this->Lang('payment_advance')=>0, $this->Lang('payment_cash_on_delivery')=>1, $this->Lang('payment_direct_debit')=>2 ); // , $this->Lang('payment_creadit_card')=>3
	$smarty->assign('aform_payment_value', $aform_payment); // $id, $name, $items (array), $selectedvalue='', $addttext='', $delimiter=''
	$smarty->assign('aform_payment', $mode==$mode_cart ? $this->CreateInputRadioGroup($id, 'aform_payment', $payment, $aform_payment) : $aform_payment); // $id, $name, $items (array), $selectedvalue='', $addttext='', $delimiter=''
	$smarty->assign('aprompt_payment_error',isset($formerrors['aform_payment']) ? $formerrors['aform_payment'] : '');
	$smarty->assign('aprompt_bank',$this->Lang('bank'));
	$smarty->assign('aform_bank', $mode==$mode_cart ? $this->CreateInputText($id,'aform_bank', $aform_bank,20,100) : $aform_bank); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_bank_error',isset($formerrors['aform_bank']) ? $formerrors['aform_bank'] : '');
	$smarty->assign('aprompt_blz',$this->Lang('blz'));
	$smarty->assign('aform_blz', $mode==$mode_cart ? $this->CreateInputText($id,'aform_blz', $aform_blz,20,100) : $aform_blz); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_blz_error',isset($formerrors['aform_blz']) ? $formerrors['aform_blz'] : '');
	$smarty->assign('aprompt_konto',$this->Lang('konto'));
	$smarty->assign('aform_konto', $mode==$mode_cart ? $this->CreateInputText($id,'aform_konto', $aform_konto,20,100) : $aform_konto); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_konto_error',isset($formerrors['aform_konto']) ? $formerrors['aform_konto'] : '');
	$smarty->assign('aprompt_inhaber',$this->Lang('inhaber'));
	$smarty->assign('aform_inhaber', $mode==$mode_cart ? $this->CreateInputText($id,'aform_inhaber', $aform_inhaber,20,100) : $aform_inhaber); // $id, $name, $value='', $size='10', $maxlength='255', $addttext=''
	$smarty->assign('aprompt_inhaber_error',isset($formerrors['aform_inhaber']) ? $formerrors['aform_inhaber'] : '');
	$smarty->assign('aprompt_agb',$this->Lang('agb'));
	$smarty->assign('aform_agb', $mode==$mode_summary ? $this->CreateInputCheckbox($id, 'aform_agb', 'true', $aform_agb>0 ? 'true' : 'false') : $aform_agb); // $id, $name, $value='', $selectedvalue='', $addttext=''
	$smarty->assign('aprompt_agb_error',isset($formerrors['aform_agb']) ? $formerrors['aform_agb'] : '');
	$smarty->assign('aprompt_newsletter',$this->Lang('newsletter'));
	$smarty->assign('aform_newsletter', $mode==$mode_cart ? $this->CreateInputCheckbox($id, 'aform_newsletter', 'true', $aform_newsletter>0 ? 'true' : 'false') : $aform_newsletter); // $id, $name, $value='', $selectedvalue='', $addttext=''
	$smarty->assign('aprompt_notice',$this->Lang('notice'));
	$smarty->assign('aform_notice', $mode==$mode_cart ? $this->CreateTextArea(false, $id, $aform_notice, 'aform_notice', 'pagesmalltextarea') : $aform_notice); // $enablewysiwyg, $id, $text, $name, $classname='', $htmlid='', $encoding='', $stylesheet='', $cols='80', $rows='15',$forcewysiwyg='',$wantedsyntax='',$addtext=''
	$smarty->assign('aform_errors_prompt',$this->Lang('form_errors'));
	$smarty->assign('aform_errors',count($formerrors)>0 ? $formerrors : array());
	$smarty->assign('aform_errors_count',count($formerrors)>0 ? count($formerrors) : 0);

}

// Show small cart (snippet)
else if ($mode==$mode_cartsnippet) {

	// Set template + ReturnID
	if (!$template) $template = $template_cartsnippet;
	if ($page_cartsnippet) $returnid = $page_cartsnippet;
	if (!$returnid) $returnid = $this->GetPreference('returnid');

	// Get cart
	$s_cart = isset($_SESSION['articles']) ? $_SESSION['articles'] : array();
	if (!$s_cart) {
		$smarty->assign('cart_count',0);
		$smarty->assign('cart_price',0);
		$smarty->assign('cart_shipping_costs',0);
	} else {
		$sum = array ( 'amount'=>0, 'price'=>0, 'shipping_costs'=>0 );
		foreach ($s_cart as $c_article=>$articleamount) {
			$sum['amount']+= $articleamount;
			$articleid = (int)str_replace('cart_','',$c_article);
			$articledetails = $db->GetRow('SELECT price,shipping_costs FROM '.cms_db_prefix().'module_articles_articles WHERE active=1 AND entry_id = ? LIMIT 1', array($articleid));
			if ($articledetails) {
				$sum['price']+= (float)$articledetails['price'] * (int)$articleamount;
				$sum['shipping_costs']+= $articledetails['shipping_costs'];
			}
		}
		$smarty->assign('cart_count',$sum['amount']);
		$smarty->assign('cart_price',$sum['price']);
		$smarty->assign('cart_shipping_costs',$sum['shipping_costs']);
	}
	// Set link to the full cart
	if (!$page_cart || (int)$page_cart<1) $page_cart = $returnid;
	$smarty->assign('cart_link',$this->CreateLink($id, 'cart', $page_cart, '', array('mode'=>$mode_cart), '', true, $inline, '', false, $this->PageID2Url($page_cart).'/'.$page_cart.'/'.$mode_cart));

}

else {
	echo '<div style="display:block;overflow:auto;background:#fff;color:#000;padding:5px;border:2px solid #f00;">ERROR: No such mode: <b>'.$mode.'</b></div>';
}

// Give Settings to Smarty
$smarty->assign('use_image', $this->GetPreference('image_use') ? true : false);
$smarty->assign('use_url', $this->GetPreference('url_use') ? true : false);
$smarty->assign('use_detailpage', $this->GetPreference('detailpage_use') ? true : false);
$smarty->assign('use_dates', $this->GetPreference('dates_use') ? true : false);
$smarty->assign('use_availibility', $this->GetPreference('availibility_use') ? true : false);
$smarty->assign('use_deliverytime', $this->GetPreference('deliverytime_use') ? true : false);
$smarty->assign('use_shippingcosts', $this->GetPreference('shippingcosts_use') ? true : false);
$smarty->assign('use_price', $this->GetPreference('price_use') ? true : false);
$smarty->assign('use_vat', $this->GetPreference('vat_use') ? true : false);
$smarty->assign('currency', $this->GetPreference('currency'));
$smarty->assign('use_cart', $this->GetPreference('cart_use') ? true : false);
$smarty->assign('use_seo', $this->GetPreference('seo_use') ? true : false);
$smarty->assign('orderdate', strftime(trim($this->GetPreference('date_format'))));

// Send order EMails
if ($mode==$mode_ack && $sum['amount']>0) {
#$mail_template = $this->GetTemplate($email_template_customer, $this->GetName());echo $this->ProcessTemplateFromData($mail_template); exit;

	// Prepare some values for logging
	$bank_account = $aform_payment==2 ? "Bank:".$aform_bank."\nBLZ:".$aform_blz."\nAccount:".$aform_konto."\nOwner:".$aform_inhaber : '';
	$address = $aform_street."\n".$aform_plz.' '.$aform_city;
	$salutation_modes = array ( 0=>$this->Lang('mr'), 1=>$this->Lang('ms') );
	$payment_modes = array ( 0=>$this->Lang('payment_advance'), 1=>$this->Lang('payment_cash_on_delivery'), 2=>$this->Lang('payment_direct_debit'), 3=>$this->Lang('payment_creadit_card') );
	// Log order to database
	$query = 'INSERT '.cms_db_prefix().'module_articles_orders (cart,price,delivery_costs,sum,payment,bank_account,salutation,firstname,lastname,city,org,tel,mail,address,newsletter,notice,ordertime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
	$db->Execute($query, array($cartsum,$sum['price'],$sum['shipping_costs'],$sum_all,$payment_modes[$aform_payment],$bank_account,$salutation_modes[$aform_salutation],$aform_firstname,$aform_lastname,$aform_city,$aform_org,$aform_tel,$aform_mail,$address,$aform_newsletter,$aform_notice,trim($db->DBTimeStamp(time()), "'")));
	// Get Order ID
	$order_id = $db->GetOne('SELECT entry_id FROM '.cms_db_prefix().'module_articles_orders WHERE firstname = ? AND lastname = ? ORDER BY entry_id DESC LIMIT 1', array($aform_firstname,$aform_lastname));
	if (!$order_id) $order_id = 0;
	$smarty->assign('order_id', $order_id);

	// Send EMail to shop owner
	if ($email_template_owner) {
		$mail_template = $this->GetTemplate($email_template_owner, $this->GetName());
		$cmsmailer = $this->GetModuleInstance('CMSMailer');
		$aemails = trim($this->GetPreference('emails'));
		$emails = $aemails ? explode(',',$aemails) : array();
		$subject = str_replace('%c',$aform_firstname.' '.$aform_lastname,trim($this->GetPreference('subject_owner')));
		$message = $this->ProcessTemplateFromData($mail_template);
		foreach ($emails as $email) { if (trim($email)) $cmsmailer->AddAddress(trim($email)); }
		$cmsmailer->SetBody($message);
		$cmsmailer->IsHTML(true);
		$cmsmailer->SetSubject($subject);
		$cmsmailer->Send();
		$cmsmailer->ClearAddresses();
		$cmsmailer->ClearAllRecipients();
	}

	// Send ack mail to customer
	if ($email_template_customer) {
		$mail_template = $this->GetTemplate($email_template_customer, $this->GetName());
		$subject = str_replace('%d',strftime(trim($this->GetPreference('date_format'))),trim($this->GetPreference('subject_customer')));
		$message = $this->ProcessTemplateFromData($mail_template);
		$cmsmailer = $this->GetModuleInstance('CMSMailer');
		$cmsmailer->AddAddress($aform_mail,$aform_firstname.' '.$aform_lastname);
		$cmsmailer->AddAttachment(getcwd()."/uploads/shop/agb.pdf", $name = 'AGB.pdf', $encoding = 'base64', $type = 'application/octent-stream' );
		$cmsmailer->SetBody($message);
		$cmsmailer->IsHTML(true);
		$cmsmailer->SetSubject($subject);
		$cmsmailer->Send();
	}

	// Destroy sessions
	unset($_SESSION['articles']);
	unset($_SESSION['articles_order']);

}

// Get template and process it
$template_content = $template ? $this->GetTemplate($template, $this->GetName()) : '';
#$items = Array();
if($template_content) {
	echo $this->ProcessTemplateFromData($template_content);
} else {
	echo '<div style="display:block;overflow:auto;background:#fff;color:#000;padding:5px;border:2px solid #f00;">ERROR: No such template: <b>'.$params['template'].'</b></div>';
}

// EOF