<?php
/***********************************************
osDate Open-Source Dating and Matchmaking Script

(c) 2009 TUFaT.com

osDate was created by Darren Gates and Vijay Nair,
and can be downloaded freely from www.TUFaT.com.
It is distributed under the LGPL license.

osDate is free for commercial and non-commercial
uses. You may modify, re-sell, and re-distribute
osDate. Links back to TUFaT.com are appreciated.

This program is distributed in the hope that it
will be useful, but without any warranty, and
without even the implied warranty of merchantability
or fitness for a particular purpose. While strong
efforts have been taken to ensure the reliability,
security, and stability of osDate, all software
carries risk. Your use of osDate means that you
understand and accept the risks of using osDate.

For osDate documentation, change log, community
forum, latest updates, and project details,
please go to www.TUFaT.com  The osDate project is
supported through the sale of skins and add-ons,
which are entirely optional but help with the
development and design effort.
***********************************************/


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

define( 'PAGE_ID', 'section_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

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

if( isset($_GET['sort']) ){
	if ($_GET['sort'] == '' ) {

		$sort = 'displayorder asc';

	} else {

		$sort = findSortBy();
	}
} else {
	$sort = 'displayorder asc';
}

if ( isset($_GET['edit']) && $_GET['edit']!='' ) {

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

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

	if (isset($_GET['errid']) && $_GET['errid']!='') {
		$t->assign( 'error', get_lang('admin_error_msgs', $_GET['errid'] ) );
	}

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

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

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

	exit;
}


//For Deletion of sections
if ( isset($_POST['frm']) && $_POST['frm'] == 'frmDelQuestion'){

	$id = $_POST['txtid'];

	$section = $_POST['txtsectionid'];

	$osDB->query( 'DELETE FROM ! WHERE id = ? and section = ?', array( QUESTIONS_TABLE, $id, $section ) );

	$questions = $osDB->getAll('SELECT id FROM ! WHERE section = ? order by displayorder', array( QUESTIONS_TABLE, $section ) );

	$i=1;

	foreach ($questions as $qid ){

		$osDB->query( 'UPDATE ! SET displayorder = ? WHERE id = ? AND section = ?', array( QUESTIONS_TABLE, $i , $qid['id'], $section ));

		$i++;
	}

	unset($questions);
	$_GET['sectionid'] = $section;
}

//Move order of question 1 level up
if ( isset($_GET['moveup']) && $_GET['moveup'] !='' ) {

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

	if ( $nrowdispord != 1){

		$prow = $osDB->getRow( 'SELECT id, displayorder FROM ! WHERE displayorder = ? AND section = ?', array( QUESTIONS_TABLE, ($nrowdispord-1), $_GET['sectionid'] ) );

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

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

		header('location: sectionquestions.php?sectionid=' . $_GET['sectionid']);
		exit;

	}

	header('location: sectionquestions.php?sectionid=' . $_GET['sectionid'] . '&err='.QUESTION_ON_TOP );

	exit;

} elseif ( isset($_GET['movedown']) && $_GET['movedown']!='' ) {

//Move order of question 1 level down

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


	//get maximum order of sections
	$maxorder = $osDB->getOne( 'SELECT MAX(displayorder) as maxorder FROM ! WHERE section = ?', array( QUESTIONS_TABLE, $_GET['sectionid'] ) );

	if ( $nrowdispord !=  $maxorder['maxorder'] ){

		$prow = $osDB->getRow( 'SELECT id, displayorder FROM ! WHERE displayorder = ? AND section = ?', array( QUESTIONS_TABLE, ($nrowdispord+1), $_GET['sectionid'] ) );

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

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

		header( 'location: sectionquestions.php?sectionid=' . $_GET['sectionid'] );
		exit;
	}
	header('location: sectionquestions.php?sectionid=' . $_GET['sectionid'] . '&err='  . QUESTION_AT_BOTTOM );
	exit;
}


//Get Section id, name
$t->assign( 'lang', $lang );

if (isset($_GET['sectionid']) ) {
	$t->assign( 'sectionname', $osDB->getOne( 'SELECT section FROM ! WHERE id = ?', array( SECTIONS_TABLE, $_GET['sectionid'] ) ) );

	$t->assign( 'sectionid', $_GET['sectionid'] );

	$t->assign( 'data', $osDB->getAll( 'SELECT * from ! WHERE section = ? order by ' . $sort, array( QUESTIONS_TABLE, $_GET['sectionid'] ) ) );

}

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

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

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



?>