<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'cntry_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;
}

$page_size = getPageSize();

$sql = 'select name from ! where code = ?';

$t->assign('countryname', getCountryName( $_REQUEST['countrycode'] ));

$t->assign('countrycode', $_REQUEST['countrycode'] );

$t->assign('statename', getStateName( $_REQUEST['countrycode'], $_REQUEST['statecode'] ) );

$t->assign('statecode', $_REQUEST['statecode'] );

$t->assign('countyname', getCountyName( $_REQUEST['countrycode'], $_REQUEST['statecode'], $_REQUEST['countycode'] ) );

$t->assign('countycode', $_REQUEST['countycode'] );

$t->assign('cityname', getCityName( $_REQUEST['countrycode'], $_REQUEST['statecode'], $_REQUEST['citycode'], $_REQUEST['countycode']  ) );

$t->assign('citycode', $_REQUEST['citycode'] );

$t->assign('lang',$lang);


if ($_REQUEST['action'] == 'add') {

	$t->assign('todo','add');

	$t->assign('rendered_page', $t->fetch('admin/managezips.tpl'));

	$t->display( 'admin/index.tpl' );

	exit;

} elseif ($_REQUEST['action'] == 'edit') {

	$t->assign('data', $osDB->getRow( 'select * from ! where id = ?', array( ZIPCODES_TABLE, $_REQUEST['id'] ) ) );

	$t->assign('todo','edit');

	$t->assign('rendered_page', $t->fetch('admin/managezips.tpl'));

	$t->display( 'admin/index.tpl' );

	exit;

} elseif ( $_REQUEST['action'] == 'added' ) {

	$foundid = $osDB->getOne( 'select id from ! where countrycode = ? and statecode = ? and code = ? ', array( ZIPCODES_TABLE, $_REQUEST['countrycode'], $_REQUEST['statecode'],  $_REQUEST['code'] ) ) ;

	if ($foundid > 0 ) {

		$t->assign('errmsg', ZIPCODE_INUSE);

		$t->assign("error_message", get_lang('errormsgs', ZIPCODE_INUSE) );

		$data['code'] = $_REQUEST['code'];

		$data['name'] = $_REQUEST['name'];

		$t->assign('data', $data);

		unset($data);

		$t->assign('todo','add');

		$t->assign('rendered_page', $t->fetch('admin/managezips.tpl'));

		$t->display( 'admin/index.tpl' );

		exit;

	} else {

		$osDB->query('insert into !(countrycode, statecode, countycode, citycode, code, latitude, longitude) values (?, ?, ?, ?, ?, ?, ?)', array( ZIPCODES_TABLE, $_REQUEST['countrycode'], $_REQUEST['statecode'], $_REQUEST['countycode'], $_REQUEST['citycode'], $_REQUEST['code'], $_REQUEST['latitude'], $_REQUEST['longitude'] ) );

		$errmsg = ZIP_ADDED;

	}

} elseif ($_REQUEST['action'] == 'edited' ) {

	$foundid = $osDB->getOne( 'select id from ! where countrycode = ? and statecode = ? and countycode = ? and citycode = ? and code = ? and id <> ?', array( ZIPCODES_TABLE, $_REQUEST['countrycode'], $_REQUEST['statecode'], $_REQUEST['countycode'],  $_REQUEST['citycode'], $_REQUEST['code'], $_REQUEST['id'] ) ) ;

	if ($foundid > 0 ) {

		$t->assign('errmsg', ZIPCODE_INUSE);

		$t->assign("error_message", get_lang('errormsgs', ZIPCODE_INUSE) );

		$data['code'] = $_REQUEST['code'];

		$data['name'] = $_REQUEST['name'];

		$data['latitude'] = $_REQUEST['latitude'];

		$data['longitude'] = $_REQUEST['longitude'];

		$data['id'] = $_REQUEST['id'];

		$t->assign('data', $data);

		unset($data);

		$t->assign('todo','edit');

		$t->assign('rendered_page', $t->fetch('admin/managezips.tpl'));

		$t->display( 'admin/index.tpl' );

		exit;

	} else {

		$osDB->query('update ! set code = ?, latitude=?, longitude=? where id = ?', array( ZIPCODES_TABLE, $_REQUEST['code'], $_REQUEST['latitude'], $_REQUEST['longitude'], $_REQUEST['id'] ) );

		$errmsg = ZIP_MODIFIED;
	}
}

if ( $_REQUEST['action'] == 'delete' ) {

	$osDB->query( 'DELETE FROM ! where id = ?', array( ZIPCODES_TABLE, $_REQUEST['id'] ) );

	$errmsg = ZIP_DELETED;

} elseif ( $_REQUEST['groupaction'] == get_lang('delete_selected') && count($_REQUEST['txtcheck']) > 0 ) {
/* Group delete */

	$del_list = " id in (";
	foreach ($_REQUEST['txtcheck'] as $k => $val) {

		if ($k > 0) { $del_list.= ','; }

		$del_list .= "'".$val."'";

	}

	$del_list .= ')';

	$osDB->query( 'DELETE FROM ! where ! ', array( ZIPCODES_TABLE, $del_list ) );

	unset($del_list);

	$errmsg = ZIP_DELETED;

}

$where='';

if ($_REQUEST['searchme'] == get_lang('show') ) {

	if ($_REQUEST['zipcode'] != '') {

		$where = " and upper(code) like upper('%".$_REQUEST['zipcode']."%') ";

	}

	$t->assign('zipcode', $_REQUEST['zipcode']);
}

$page = (int)$_GET['offset'];

if( $page == 0 ) { $page = 1; }

$_GET['offset'] = $page;

$upr = ($page)*$page_size - $page_size;

$t->assign( 'zipslist',$osDB->getAll( 'SELECT * FROM ! where countrycode = ? and statecode = ? and countycode = ? and citycode = ? ' . $where . ' ' . $sort . ' ORDER BY code LIMIT !,! ', array( ZIPCODES_TABLE, $_REQUEST['countrycode'], $_REQUEST['statecode'], $_REQUEST['countycode'], $_REQUEST['citycode'], $upr, (int)$page_size ) ) );

$t->assign('total_recs', $osDB->getOne('select count(*) from ! where countrycode = ? and statecode = ? and countycode = ? and citycode = ? ', array( ZIPCODES_TABLE, $_REQUEST['countrycode'], $_REQUEST['statecode'], $_REQUEST['countycode'], $_REQUEST['citycode'] ) ) );

unset($where, $sort);

$t->assign( 'sort_type', checkSortType( $_GET['type'] ) );

$t->assign('countrycode', $_REQUEST['countrycode']);

$t->assign('statecode', $_REQUEST['statecode']);

$t->assign('countycode', $_REQUEST['countycode']);

$t->assign('citycode', $_REQUEST['citycode']);

$t->assign( 'upr', $upr );

$t->assign('page_size', $page_size);

$t->assign('errmsg', $errmsg);

$t->assign("error_message", get_lang('errormsgs', $errmsg) );

$t->assign('rendered_page', $t->fetch('admin/managezips.tpl'));

$t->display( 'admin/index.tpl' );

?>
