<?php
//Dialplan Positions ändern
@session_start();
if((isset($_SESSION['beroari_time']))&&(($_SESSION['beroari_time']+1200)<time())){
	@session_unset();
	@session_destroy();
	echo "<script>window.opener.location='../index.php'</script>";
	echo "<script>this.window.close();</script>";
}else{
	$_SESSION['beroari_time']=time();
	
	$posid=$_GET['posid'];
	$id=$_GET['id'];
	chdir("..");
	include './includes/variables.php';
	include './misc/database.php';
	$ba = new beroAri();
	$query=$ba->select("select posId from dialplan where id='".$id."'");
	$old_posid=sqlite_fetch_single($query);
	if ($old_posid>$posid){
		$query=$ba->select("SELECT * from dialplan where posId>=".$posid." order by posId asc");
		
		while(sqlite_has_more($query)){
			$tmp=sqlite_fetch_array($query);
			$nposid=$tmp['posId']+1;
			$ba->update("UPDATE dialplan set posId='".$nposid."' where id='".$tmp['id']."'");
		}
	}else if ($old_posid<$posid){
		$query=$ba->select("SELECT * from dialplan where posId<=".$posid." and posId>".$old_posid." order by posId asc");
		
		while(sqlite_has_more($query)){
			$tmp=sqlite_fetch_array($query);
			$nposid=$tmp['posId']-1;
			$ba->update("UPDATE dialplan set posId='".$nposid."' where id='".$tmp['id']."'");
		}
	}

	$query=$ba->update("UPDATE dialplan SET posId='".$posid."' where id=".$id);

	$query=$ba->select("select option from activate where id='activate'");
	while($entry = $ba->fetch_array($query)){
		if ($entry['option'] <= 1) $query =  $ba->update("UPDATE activate SET option=1 where id='activate'");
	}
	
	$div=floor(($posid-1)/$ba->get('span'));
	$start=$div*$ba->get('span');
		
	$query = $ba->select('SELECT * FROM dialplan');
	$record_count = sqlite_num_rows($query);
	if ($posid>$record_count){
		$div=floor(($record_count-1)/$ba->get('span'));
		$start=$div*$ba->get('span');
	}
			
		
	if (isset($_GET['first'])){
			$dir=$_GET['dir'];
			$search=$_GET['search'];
			echo "<script>window.opener.location='../index.php?m=dialplan';</script>";
			echo "<script>this.window.close();</script>";
	}else
	echo "<script>window.location='../index.php?m=dialplan&start=$start'</script>";
}
?>
