<?php
define("ADMIN_AUTHORIZATION",1);
include("../../../includes/common.php");


/*PAGE TITLE*/
$page_title = "Список рекламных материалов - баннеров";

$header = "Список баннеров";

$table = "banners";
/* pager start */
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$pager = GetPagerParams($params);
/* pager end */

$group_funcs_options=("<option >Выберите действие<option value='./action.php?action=delete'> - Удалить</option>
                      <option value='./action.php?action=activate'> - Активировать</option>
                      <option value='./action.php?action=unactivate'> - Деактивировать</option>");

$table_headers = Array(
	"itter"=>Array("№","width=20 align=center"),
	"name"=>Array("<a href='".$pager['new_sort']."$table".".name' title='Sort'>Название</a>",""),
	"html_code"=>Array("<a href='".$pager['new_sort']."$table".".html_code' title='Sort'>HTML Код</a>",""),
	"status"=>Array("<a href='".$pager['new_sort']."$table".".status' title='Sort'>Статус</a>","width=55px"),
	"funcs"=>Array("Операции","width=5% align=center"),
	"group_funcs"=>Array("<select name=act_sel onChange='SubmitForm();'><option>Выберите действие<option value='./action.php?action=delete'>-Удалить<option value='./action.php?action=activate'>-Активировать<option value='./action.php?action=unactivate'>-Деактивировать</select>"));

$query_select = "
select 
ex_currencies.*, currencies.name as curr,currencies.code as curr_code, count(pincodes.id) as pincount, 
metals.name as metal, pincode_collections.name as collection_name, pincode_collections.sell_price, pincurrencies.name as pincode_curr, pincurrencies.code as pincode_currcode
from 
(ex_currencies left outer join currencies on currencies.id = ex_currencies.currency_id) 
left outer join metals on metals.id = ex_currencies.metal_id 
left join pincode_collections on pincode_collections.id=ex_currencies.pincode_collection_id 
left join currencies as pincurrencies on pincurrencies.id=pincode_collections.currency_id 
left join pincodes on (pincodes.collection_id=ex_currencies.pincode_collection_id and pincodes.status='valid') 
WHERE ex_currencies.status=1 
GROUP BY ex_currencies.id ORDER BY id";

$res_select = mysql_query($query_select);

$select_out = "";
$select_in = "";
$itter = 0;
while ($row = mysql_fetch_object($res_select)){
	if($itter == 0){
		$select_out .= "<option name='".$row->id."' selected>".$row->name."</option>";
		$select_in .= "<option name='".$row->id."'>".$row->name."</option>";
	}elseif($itter == 1){
		$select_out .= "<option name='".$row->id."'>".$row->name."</option>";
		$select_in .= "<option name='".$row->id."' selected>".$row->name."</option>";
	}else{
		$select_out .= "<option name='".$row->id."'>".$row->name."</option>";
		$select_in .= "<option name='".$row->id."'>".$row->name."</option>";
	}
	$itter++;
}

$query = "
	SELECT *
	FROM $table
	".
	($search_string?" where $search_string":"")
	." ORDER BY ".$pager['sort_by']." ".$pager['sort_ord']
	." LIMIT ".$SETTINGS['admin_items_per_page']." OFFSET ".($pager['start_item']-1);
  

$res = mysql_query($query);
$table_items=Array();
$itter =$pager['start_item'];
$i=0;

while ($row = mysql_fetch_object($res)){
	switch($row->status){
		case 0:
			$s= $ADMIN_ICONS[status_off];
		break;
		case 1:
			$s= $ADMIN_ICONS[status_on];
		break;
	}

	$html = stripslashes($row->html_code);
	$html = str_replace("[[SITE_URL]]", $SETTINGS['site_url'], $html);
	$html = str_replace("[[SITE_NAME]]", $SETTINGS['site_name'], $html);
	$html = str_replace("[[SELECT_OUT]]", $select_out, $html);
	$html = str_replace("[[SELECT_IN]]", $select_in, $html);
	$html = str_replace("[[PARTNER_ID]]", $partner_id, $html);

	$sql_amount = "SELECT amount FROM ex_currencies WHERE id=1";
	$row_amount = mysql_fetch_object(mysql_query($sql_amount));
	$html = str_replace("[[CURR_OUT_AMOUNT]]", round($row_amount->amount, 2), $html);
	$sql_amount = "SELECT amount FROM ex_currencies WHERE id=2";
	$row_amount = mysql_fetch_object(mysql_query($sql_amount));
	$html = str_replace("[[CURR_IN_AMOUNT]]", round($row_amount->amount, 2), $html);

	$table_items[$itter] = Array(
		"id"=>Array($row->id,"align=center"),
		"itter"=>array($itter,"align=center"),
		"name"=>Array(stripslashes($row->name)),
		"html_code"=>Array($html, ""),
		"status"=>array($s, "align=center"),
		"funcs"=>Array("<nobr><a href='./b_add.php?id=$row->id' title='Редактировать'>$ADMIN_ICONS[edit]</a>&nbsp;<a href='#' onClick=\"if(confirm('Удалить??')){document.location.href='./action.php?action=delete&id=$row->id';}return false;\" title='Удалить'>$ADMIN_ICONS[del]</a></nobr>","align=center")
		);
	$itter++;
}

$smarty->assign('page_title', $page_title);

$smarty->assign('error',$error);

/* table template */
$smarty->assign('pager',$pager);
$smarty->assign('table_headers',$table_headers);
$smarty->assign('table_items',$table_items);
$smarty->assign('table_width',"100%");
$smarty->assign('top_menu_tpl',"admin_ads_menu.tpl");
$smarty->assign('main_tpl',"admin_list_table.tpl");
$smarty->assign('use_group_funcs',array_key_exists("group_funcs",$table_headers));

/* display content */
$smarty->display('admin_index.tpl');
?>