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

// Check authorisation
if (!isset($gCms)) exit;
if ( !$this->CheckPermission('Articles: show orders') || $this->GetPreference('cart_use')<1 ) exit;

// Set Tab name
$tabto = 'orders';

// Get Order ID
$orderid = isset($params['order']) ? (int)$params['order'] : 0;

// Set vars and images
$admintheme = cms_utils::get_theme_object();
$imageview = $admintheme->DisplayImage('icons/system/view.gif',$this->Lang('view'),'','','systemicon');
$imagedelete = $admintheme->DisplayImage('icons/system/delete.gif',$this->Lang('delete'),'','','systemicon');
$imagenew = $admintheme->DisplayImage('icons/system/newobject.gif',$this->Lang('newtemplate'),'','','systemicon');


// Display form for editing a order
if (isset($params['order'])) {

	// Get category info
	$orderinfo = $db->GetRow('SELECT * FROM '.cms_db_prefix().'module_articles_orders WHERE entry_id = ?', array($orderid));
	if (!$orderinfo) $orderinfo = array( 'entry_id'=>0,'cart'=>'','price'=>0,'delivery_costs'=>0,'sum'=>0,'payment'=>'','bank_account'=>'','salutation'=>'','firstname'=>'','lastname'=>'','city'=>'','org'=>'','tel'=>'','mail'=>'','address'=>'','newsletter'=>0,'notice'=>'','ordertime'=>trim($db->DBTimeStamp(time()), "'") );

	// Assign Smarty vars
	$smarty->assign('formid',$id);
	$smarty->assign('formstart',$this->CreateFormStart($id,'admin_vieworder',$returnid,'post','multipart/form-data',true,array()));
    $smarty->assign('formend', $this->CreateFormEnd());
    $smarty->assign('submit', $this->CreateInputSubmit($id,$order ? 'submit': 'add',$this->Lang('submit')));
    $smarty->assign('cache', $this->CreateInputSubmit($id,$category ? 'cache' : 'cacheadd',$this->Lang('cache')));
    $smarty->assign('reset', $this->CreateInputReset($id,'reset',$this->Lang('reset')));
    $smarty->assign('cancel', $this->CreateInputSubmit($id,'cancel',$this->Lang('cancel')));
	$smarty->assign('tabto', $this->CreateInputHidden($id,'tabto',$tabto));
    $smarty->assign('order', $this->CreateInputHidden($id,'order',$orderid));

	$smarty->assign('prompt_cart', $this->Lang('cart'));
	$smarty->assign('input_cart', $this->CreateTextArea(false, $id, $orderinfo['cart'], 'cart', 'pagesmalltextarea'));
	$smarty->assign('prompt_price', $this->Lang('price'));
	$smarty->assign('input_price', $this->CreateInputText($id,'price',$orderinfo['price'],20,20));
	$smarty->assign('prompt_delivery_costs', $this->Lang('delivery_costs'));
	$smarty->assign('input_delivery_costs', $this->CreateInputText($id,'delivery_costs',$orderinfo['delivery_costs'],20,20));
	$smarty->assign('prompt_sum', $this->Lang('sum'));
	$smarty->assign('input_sum', $this->CreateInputText($id,'sum',$orderinfo['sum'],20,20));
	$smarty->assign('prompt_payment', $this->Lang('payment_mode'));
	$smarty->assign('input_payment', $this->CreateInputText($id,'payment',$orderinfo['payment'],20,20));
	$smarty->assign('prompt_bank_account', $this->Lang('bank_account'));
	$smarty->assign('input_bank_account', $this->CreateTextArea(false, $id, $orderinfo['bank_account'], 'bank_account', 'pagesmalltextarea'));
	$smarty->assign('prompt_salutation', $this->Lang('salutation'));
	$smarty->assign('input_salutation', $this->CreateInputText($id,'salutation',$orderinfo['salutation'],20,50));
	$smarty->assign('prompt_person', $this->Lang('person'));
	$smarty->assign('prompt_firstname', $this->Lang('firstname'));
	$smarty->assign('input_firstname', $this->CreateInputText($id,'firstname',$orderinfo['firstname'],10,100));
	$smarty->assign('prompt_lastname', $this->Lang('lastname'));
	$smarty->assign('input_lastname', $this->CreateInputText($id,'lastname',$orderinfo['lastname'],10,100));
	$smarty->assign('prompt_city', $this->Lang('city'));
	$smarty->assign('input_city', $this->CreateInputText($id,'city',$orderinfo['city'],20,100));
	$smarty->assign('prompt_org', $this->Lang('org'));
	$smarty->assign('input_org', $this->CreateInputText($id,'org',$orderinfo['org'],20,100));
	$smarty->assign('prompt_tel', $this->Lang('tel'));
	$smarty->assign('input_tel', $this->CreateInputText($id,'tel',$orderinfo['tel'],20,50));
	$smarty->assign('prompt_mail', $this->Lang('mail'));
	$smarty->assign('input_mail', $this->CreateInputText($id,'mail',$orderinfo['mail'],20,100));
	$smarty->assign('prompt_address', $this->Lang('address'));
	$smarty->assign('input_address', $this->CreateTextArea(false, $id, $orderinfo['address'], 'address', 'pagesmalltextarea'));
	$smarty->assign('prompt_newsletter', $this->Lang('newsletter'));
	$smarty->assign('input_newsletter', $this->CreateInputCheckbox($id, 'newsletter', 'true', $orderinfo['newsletter'] ? 'true' : 'false'));
	$smarty->assign('prompt_notice', $this->Lang('notice'));
	$smarty->assign('input_notice', $this->CreateTextArea(false, $id, $orderinfo['notice'], 'notice', 'pagesmalltextarea'));
	$smarty->assign('prompt_ordertime', $this->Lang('ordertime'));
	$smarty->assign('input_ordertime', $orderinfo['ordertime']);
	
    // Display Template
    echo $this->ProcessTemplate('vieworder.tpl');

}

// Display a list with available orders
else {

	// Get orders
	$allorders = $db->GetAll('SELECT * FROM '.cms_db_prefix().'module_articles_orders');
	if (!is_array($allorders)) $allorders = array();

	// Show orders
	$rowarray = array();
	$rowclass = 'row1';
	foreach ($allorders as $order) {
		// Build a new row
		$row = new StdClass();
		$row->rowclass = $rowclass;
		// ID
		$row->id = $order['entry_id'];
		// Cart
		$row->cart = $order['cart'];
		// Price
		$row->price = $this->FormatPrice($order['price']);
		// Delivery Costs
		$row->delivery_costs = $this->FormatPrice($order['delivery_costs']);
		// Sum (price including all costs like shipping, ...)
		$row->sum = $this->FormatPrice($order['sum']);
		// Payment Mode
		$row->payment = $order['payment'];
		// Bank Account
		$row->bank_account = $order['bank_account'];
		// Company/Organisation
		$row->org = trim($order['org']);
		// Salutation
		$row->salutation = $order['salutation'];
		// First Name
		$row->firstname = trim($order['firstname']);
		// Last Name
		$row->lastname = trim($order['lastname']);
		// Full Name of Person
		$row->person = $order['firstname'].' '.$order['lastname'];
		// Full Name with Salutation
		$row->person_full = $row->org ? $row->salutation.' '.$row->person.' ('.$row->org.')' : $row->salutation.' '.$row->person;
		// City
		$row->city = $order['city'];
		// Telephone
		$row->tel = $order['tel'];
		// EMail
		$row->mail = $order['mail'];
		// Address
		$row->address = $order['address'];
		// Newsletter
		$row->newsletter = $order['newsletter']>0 ? $this->Lang('yes') : $this->Lang('no');
		// Notice
		$row->notice = $order['notice'];
		// Order Time
		$row->ordertime = $order['ordertime'];
		// Title
		#$row->title = $this->CreateLink( $id, 'admin_editcategory', $returnid, $order['title'], array ( 'tabto'=>$tabto, 'category'=>$order['entry_id'] ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// Build edit icon
		#$row->editlink = $this->CreateLink( $id, 'admin_editcategory', $returnid, $imageedit, array ( 'tabto'=>$tabto, 'category'=>$order['entry_id'] ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// Build view icon
		$row->viewlink = $this->CreateLink( $id, 'admin_vieworder', $returnid, $imageview, array ( 'tabto'=>$tabto, 'order'=>$order['entry_id'] ));
		// Build delete icon
		$row->deletelink = $this->CreateLink( $id, 'admin_deleteorder', $returnid, $imagedelete, array ( 'tabto'=>$tabto, 'order'=>$order['entry_id'] ), $this->Lang('reallydelete'), false, false, ' title="'.$this->Lang('delete').'"');
		// Merge new domain to array
		array_push ($rowarray, $row);
		// Alternate row color
		($rowclass == 'row1' ? $rowclass = 'row2' : $rowclass = 'row1');
	}

	// Assign smarty vars
	$smarty->assign('items', $rowarray );
	$smarty->assign('id', $this->Lang('id'));
	$smarty->assign('person', $this->Lang('person'));
	$smarty->assign('city', $this->Lang('city'));
	$smarty->assign('ordertime', $this->Lang('ordertime'));
	$smarty->assign('price', $this->Lang('price'));
	$smarty->assign('delivery_costs', $this->Lang('delivery_costs'));
	$smarty->assign('deletealllink', $this->CreateLink( $id, 'admin_deleteorder', $returnid, $imagedelete.' '.$this->Lang('deleteall'), array ( 'tabto'=>$tabto, 'all'=>'true' ), $this->Lang('reallydelete'), false, false, ' title="'.$this->Lang('deleteall').'"'));
	echo $this->ProcessTemplate('listorders.tpl');

}

// EOF