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

// Check authorisation
if (!isset($gCms)) exit;
if ( !$this->CheckPermission('Articles: show orders') ) exit;
$errors = array();
#print_r($params);exit;
// get parameters
$tabto = preg_replace('/[^0-9a-zA-Z_]/','',$params['tabto']);
$order = isset($params['order']) ? (int)$params['order'] : 0;
$newsletter = $params['newsletter'] ? 1 : 0;
$cart = isset($params['cart']) ? $params['cart'] : '';
$price = isset($params['price']) ? $params['price'] : 0;
$delivery_costs = isset($params['delivery_costs']) ? $params['delivery_costs'] : 0;
$sum = isset($params['sum']) ? $params['sum'] : 0;
$payment_mode = isset($params['payment']) ? $params['payment'] : '';
$bank_account = isset($params['bank_account']) ? $params['bank_account'] : '';
$salutation = isset($params['salutation']) ? $params['salutation'] : '';
$firstname = isset($params['firstname']) ? $params['firstname'] : '';
$lastname = isset($params['lastname']) ? $params['lastname'] : '';
$city = isset($params['city']) ? $params['city'] : '';
$org = isset($params['org']) ? $params['org'] : '';
$tel = isset($params['tel']) ? $params['tel'] : '';
$mail = isset($params['mail']) ? $params['mail'] : '';
$address = isset($params['address']) ? $params['address'] : '';
$notice = isset($params['notice']) ? $params['notice'] : '';

// Save order changes
if (isset($params['submit']) || isset($params['cache']) || isset($params['add']) || isset($params['cacheadd']) || isset($params['apply'])) {
	// Edit order
	if ($order) {
		$sql = 'UPDATE '.cms_db_prefix().'module_articles_orders SET cart=?,price=?,delivery_costs=?,sum=?,payment=?,bank_account=?,salutation=?,firstname=?,lastname=?,city=?,org=?,tel=?,mail=?,address=?,newsletter=?,notice=? WHERE entry_id=?';
		$res = $db->Execute($sql,array($cart,$price,$delivery_costs,$sum,$payment_mode,$bank_account,$salutation,$firstname,$lastname,$city,$org,$tel,$mail,$address,$newsletter,$notice,$order));
		if(!$res) { $errors[] = 'SQL ERROR: ' . $db->ErrorMsg() . '(with ' . $db->sql . ')'; }
	// Insert new order
	} else {
		// Insert article
		$sql = 'INSERT INTO '.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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
		$res = $db->Execute($sql,array($cart,$price,$delivery_costs,$sum,$payment_mode,$bank_account,$salutation,$firstname,$lastname,$city,$org,$tel,$mail,$address,$newsletter,$notice,trim($db->DBTimeStamp(time()), "'")));
		$order = $db->GetOne('SELECT entry_id FROM '.cms_db_prefix().'module_articles_orders WHERE person = ? ORDER BY entry_id DESC LIMIT 1', array($person));
	}
}

// Redirect
if (isset($params['submit']) || isset($params['add'])) $this->Redirect($id, 'defaultadmin', $returnid, array('tabto'=>$tabto, 'errors' => implode(',',str_replace(',',';',$errors)), 'message' => $this->Lang('changessaved')));
elseif (isset($params['cancel'])) $this->Redirect($id, 'defaultadmin', $returnid, array('tabto'=>$tabto));
else $this->Redirect($id, 'defaultadmin', $returnid, array( 'tabto'=>$tabto, 'order'=>$order ));

// EOF