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

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'news_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;
}
$psize = getPageSize();

if ( $_GET['edit'] ) {

	if ($_GET['errid'] != '') {
		$data['date'] = $_SESSION['txtdate'];
		$data['header'] = $_SESSION['txttitle'];
		$data['text'] = $_SESSION['txttext'];
		$data['newsid'] = $_GET['edit'];
	} else{

		$data =& $osDB->getRow( 'SELECT * from ! Where newsid = ?', array(NEWS_TABLE, $_GET['edit']) );
	}
	$t->assign( 'lang', $lang );

	if ($_GET['errid'] != '') {
		$t->assign( 'error_msg', get_lang('news_error', $_GET['errid'] ) );
	}
	$t->assign( 'news', $data );

	unset($data);

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

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

	exit;
}

if ( $_POST['deletenews'] ) {

	$osDB->query( 'DELETE FROM ! Where newsid = ?', array(NEWS_TABLE, $_POST['deletenews']) );

}


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

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

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

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

if( $_GET['sort'] == get_lang('col_head_sendtime') ) {

	$sort = 'date '. checkSortType ( $_GET['type'] );

} else {

	$sort = findSortBy('newsid');
}

$t->assign ( 'total_recs',  $osDB->getOne( 'SELECT count(*) as num FROM !', array(NEWS_TABLE) ) );

$t->assign( 'data', $osDB->getAll( 'SELECT * FROM ! ORDER BY ' . $sort . " LIMIT $upr,$psize ", array(NEWS_TABLE) ) );

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

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

$t->assign( 'querystring', 'sort=' . $_GET['sort'] . '&type=' . $_GET['type']);

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

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

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


?>