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

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'section_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

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

//Default Sorting
if( $_GET['sort'] == '' ) {

	$sort = 'displayorder asc ';

} else if( $_GET['sort'] == get_lang('col_head_name') ) {

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

} else {

	$sort = findSortBy();

}

//For Editing Sections
if ( $_GET['edit'] ) {

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

	$t->assign( 'error', get_lang('admin_error_msgs', $_GET['errid']) );

	$t->assign( 'data', $osDB->getRow( 'SELECT id, section, enabled from ! Where id = ?', array( SECTIONS_TABLE, $_GET['edit'] )) );

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

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

//For Deletion of sections
if ( $_POST['frm'] == 'frmDelSection' && $_POST['delaction'] == 'Yes') {

	$id = $_POST['txtid'];

	$displayorder = $osDB->getOne( 'SELECT displayorder FROM ! WHERE id = ?', array( SECTIONS_TABLE, $id ) );

	$maxorder = $osDB->getOne( 'SELECT MAX(displayorder) as maxorder FROM ! ', array( SECTIONS_TABLE ) );

	//move the records below this record up
	$i=$displayorder + 1;

	while ($i <= $maxorder['maxorder'] ){

		$osDB->query( 'UPDATE ! SET displayorder = ? WHERE displayorder = ? ', array( SECTIONS_TABLE,  $i-1 , $i ));

		$i++;
	}

	//now delete the record
	$osDB->query( 'DELETE FROM ! WHERE id = ?', array( SECTIONS_TABLE, $id ) );

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

//Insert in Section with max displayorder
if ( $_POST['frm'] == 'frmAddSection') {

	$maxorder = $osDB->getOne( 'SELECT MAX(displayorder)+1 as orderno FROM ! ', array( SECTIONS_TABLE ) );

	if ($maxorder <=0) $maxorder = 1;

	$osDB->query( 'INSERT INTO ! (section, enabled , displayorder) VALUES (?, ?, ? )', array( SECTIONS_TABLE, trim( $_POST['txtsection'] ), trim( $_POST['txtenabled'] ),  $maxorder ) );

	header('location: section.php');
	exit;
}//End of if

if ( $_GET['moveup'] ) {

	$nrowdispord = $osDB->getOne( 'SELECT displayorder FROM ! WHERE id = ?', array( SECTIONS_TABLE, $_GET['moveup'] ) );

	//to check whether it is at the highest order
	//if not then move up
	if ( $nrow['displayorder'] != 1){

		$prow =& $osDB->getRow( 'SELECT id, displayorder FROM ! WHERE displayorder = ?', array( SECTIONS_TABLE, ($nrowdispord-1) ) );

		$osDB->query( 'UPDATE ! SET displayorder = ? WHERE displayorder = ? AND id = ?', array( SECTIONS_TABLE, $nrowdispord, $prow['displayorder'], $prow['id'] ));

		$osDB->query( 'UPDATE ! SET displayorder = ? WHERE displayorder = ? AND id = ?', array( SECTIONS_TABLE, $nrowdispord-1, $nrowdispord, $_GET['moveup'] ));

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

	header('location: section.php?msg=Section is already at the top');

	exit;
}

if ( $_GET['movedown'] ) {

	$nrowdispord = $osDB->getOne( 'SELECT displayorder FROM ! WHERE id = ?', array( SECTIONS_TABLE,$_GET['movedown'] ) );

	//get maximum order of sections
	$maxorder = $osDB->getOne( 'SELECT MAX(displayorder) as maxorder FROM !', array( SECTIONS_TABLE ) );

	//to check whether it is at the lowest order
	//if not then move down
	if ( $nrowdispord !=  $maxorder['maxorder'] ){

		$prow =& $osDB->getRow( 'SELECT id, displayorder FROM ! WHERE displayorder = ?', array( SECTIONS_TABLE,($nrowdispord+1) ) );

		$osDB->query( 'UPDATE ! SET displayorder = ? WHERE displayorder = ? AND
			id = ?' , array( SECTIONS_TABLE, ($nrowdispord+1), $nrowdispord, $_GET['movedown'] ));

		$osDB->query( 'UPDATE ! SET displayorder = ? WHERE displayorder = ? AND
			id = ?' , array( SECTIONS_TABLE, $nrowdispord, $prow['displayorder'], $prow['id'] ));

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

	header('location: section.php?msg=Section is already at the bottom');

	exit;
}
//Insert new section
elseif ( $_GET['insert'] == 'question') {

	$lang['display_control_type'] = get_lang_values('display_control_type');

	$t->assign('sectionname', $osDB->getOne('SELECT section from ! Where id = ?', array( SECTIONS_TABLE, $_GET['sectionid'] ) ));

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

	$t->assign( 'error', get_lang('admin_error_msgs', $_GET['errid'] ) );

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

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

	exit;
}

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

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

$t->assign( 'data', $osDB->getAll( 'SELECT id, section, displayorder, enabled from ! order by ' . $sort, array(SECTIONS_TABLE) ));

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

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


?>