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

define( 'PAGE_ID', 'article_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

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

$psize = getPageSize();

if ( $_GET['edit'] ) {

	if ($_SESSION['modified'] != '') {

		$data = $_SESSION['modified'];

		$_SESSION['modified'] = '';

	} else {
		$data =& $osDB->getRow( 'SELECT * from ! Where articleid = ?', array( ARTICLES_TABLE, $_GET['edit'] ) );
	}

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

	$t->assign( 'error_msg', get_lang('article_error', $_GET['errid'] ) );

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

	unset($data);

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

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

	exit;
}
if ( $_POST['deletearticle'] ) {

	$osDB->query( 'DELETE FROM ! Where articleid = ?', array( ARTICLES_TABLE, $_POST['deletearticle'] ) );

}

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

$page_size = $psize;

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

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

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

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

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

} else {

	$sort = findSortBy('articleid');

}

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

$t->assign( 'data', $osDB->getAll( 'SELECT * FROM ! ORDER BY ' . $sort . " LIMIT $upr,$page_size ", array( ARTICLES_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/managearticles.tpl'));

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

?>