<?php


/**
 * Klasse Dialplan
 * Zeigt alle Einträge von der Tabelle dialplan der Datenbank
 */

class Dialplan {
	//Suche die Dialplan Einträge, die dem "search" Kriterium entsprechen
	function filter($ba,$span,$start){
		if ((!isset($_GET['dir']) || $_GET['dir']=='all' || $_GET['dir']=="") && $_GET['search']==""){
				$query = $ba->select(' SELECT * from dialplan ORDER BY posId, id asc limit ' . $span .' offset ' . $start);
			}else if ((!isset($_GET['dir']) || $_GET['dir']=='all' || $_GET['dir']=="") && $_GET['search']!=""){
				$query = $ba->select(' SELECT * from dialplan
										WHERE fromId like ("%'.$_GET["search"].'%")
											or toId like ("%'.$_GET["search"].'%")
											or dest like ("%'.$_GET["search"].'%")
											or newDest like ("%'.$_GET["search"].'%")
											or src like ("%'.$_GET["search"].'%")
											or newSrc like ("%'.$_GET["search"].'%")
										ORDER BY posId, id asc limit ' . $span .' offset ' . $start);
			}else if (isset($_GET['dir']) && $_GET['dir']!='all' && $_GET['dir']!="" && $_GET['search']!=""){
				$dir=explode("-",$_GET['dir']);
				$query = $ba->select(' SELECT * from dialplan
										WHERE (fromId like ("%'.$_GET["search"].'%")
											or toId like ("%'.$_GET["search"].'%")
											or dest like ("%'.$_GET["search"].'%")
											or newDest like ("%'.$_GET["search"].'%")
											or src like ("%'.$_GET["search"].'%")
											or newSrc like ("%'.$_GET["search"].'%"))
											and (toDirection="'.$dir[1].'" and fromDirection="'.$dir[0].'")
										ORDER BY posId, id asc limit ' . $span .' offset ' . $start);
			}else{
				$dir=explode("-",$_GET['dir']);
				$query = $ba->select(' SELECT * from dialplan WHERE toDirection="'.$dir[1].'" and fromDirection="'.$dir[0].'" ORDER BY posId, id asc limit ' . $span .' offset ' . $start);
			}
			return $query;
	}

	//Suche die Dialplan Einträge, die dem "search" Kriterium entsprechen(wird für die Navigationsleiste benutzt)
	function filter_navi($ba,$span,$start){
		if ((!isset($_GET['dir']) || $_GET['dir']=='all' || $_GET['dir']=="") && $_GET['search']==""){
				$query = $ba->select(' SELECT * from dialplan ORDER BY posId, id asc');
			}else if ((!isset($_GET['dir']) || $_GET['dir']=='all' || $_GET['dir']=="") && $_GET['search']!=""){
				$query = $ba->select(' SELECT * from dialplan
										WHERE fromId like ("%'.$_GET["search"].'%")
											or toId like ("%'.$_GET["search"].'%")
											or dest like ("%'.$_GET["search"].'%")
											or newDest like ("%'.$_GET["search"].'%")
											or src like ("%'.$_GET["search"].'%")
											or newSrc like ("%'.$_GET["search"].'%")
										ORDER BY posId, id asc');
			}else if (isset($_GET['dir']) && $_GET['dir']!='all' && $_GET['dir']!="" && $_GET['search']!=""){
				$dir=explode("-",$_GET['dir']);
				$query = $ba->select(' SELECT * from dialplan
										WHERE (fromId like ("%'.$_GET["search"].'%")
											or toId like ("%'.$_GET["search"].'%")
											or dest like ("%'.$_GET["search"].'%")
											or newDest like ("%'.$_GET["search"].'%")
											or src like ("%'.$_GET["search"].'%")
											or newSrc like ("%'.$_GET["search"].'%"))
											and (toDirection="'.$dir[1].'" and fromDirection="'.$dir[0].'")
										ORDER BY posId, id asc');
			}else{
				$dir=explode("-",$_GET['dir']);
				$query = $ba->select(' SELECT * from dialplan WHERE toDirection="'.$dir[1].'" and fromDirection="'.$dir[0].'" ORDER BY posId, id asc');
			}
			return $query;
	}
	/*
	 * Funktion action
	 * wird aufgerufen um einen neuen Eintrag in der Datenbank zu schreiben
	 * Popup fenster wird geöffnet
	 */
	function action($args) {

		// args
		$m = $_GET['m'];
		$a = $_GET['a'];
		$start = $_GET['start'];

		if ($a = 'add') {
			echo "<script type=\"text/javascript\">new_window(\"./misc/dialplan_popup.php?logingroup=" . $_SESSION['beroari_group'] . "&start=" . $start . "&dir=" . $_GET['dir'] . "&search=" . $_GET['search'] . "\");</script>";
		}
	}

	/*
	 * Funktion display erzeugt den Inhalt von der HTML-Seite
	 */
	function display($args) {

		include "./includes/functions.php";
		//Das Objekt funktions wird erzeugt um später die Navigationsleiste zu erzeugen.
		$functions = new functions();


		// args
		$m = $_GET['m'];
		$q = $_GET['q'];
		$start = $_GET['start'];
		$next_del=0;
		$next_add=0;
		//ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();

		$query = $ba->select('SELECT * FROM dialplan');
		$record_count = sqlite_num_rows($query);
		$start = $start == '' ? 0 : $start;
		if (isset($_GET['ent'])) $ba->set('span',$_GET['ent']);
		if (!$ba->get('span')) $ba->set('span','15');
		$span=$ba->get('span');

		$next_del=$start;
		$test=$record_count/$span;
		$test=(int)$test;
		if ($record_count==0){
			$next_add=0;
		}else{
			$next_add=($test*$span);
		}

		if ($start+$span<$record_count){
			$next_del=$start;
		}else{
			$rest=$record_count%$span;
			if ($record_count%$span==1){
				$next_del=$start-$span;
				if ($next_del<0)$next_del=0;
			}else{
				$next_del=$start;
			}
		}
		/*
		echo "START " .  $start . "<br>";
		echo "SPAN " .  $span . "<br>";
		echo "TEST " .  $test . "<br>";
		echo "REST " .  $rest . "<br>";
		echo "ADD " .  $next_add . " rc " . $record_count . "<br>";
		echo "DEL " .  $next_del . " rc " . $record_count . "<br>";
		*/

		include('./includes/header.php');
		$dialplan_table = $this->dynamic_table($next_del,$span,$next_add);
		// build page content
		$ret .= "
			<script>
				function del(table,option, from, to,op,pos){


					conf = confirm('".DIALPLAN_DELETE_MESSAGE_DIS." ".DIALPLAN_FROM_ID_DIS." '+from+', ".DIALPLAN_TO_ID_DIS." '+to+'?');
					if (conf==false)
						return false;
					else
						if(op=='isdnid') window.location='./misc/delete.php?isdnid='+option+'&table='+table+'&start='+pos;
						if(op=='id') window.location='./misc/delete.php?id='+option+'&table='+table+'&start='+pos;
				}
			</script>

			<br>
			<div id='content'>
			" . $dialplan_table . "
			<br>
			<form class='extensions_form' name='dialplan' action='' method='GET'>
			<input type=hidden name=m value=" . $m . ">
			<input type=hidden name=f value='action'>
			<input type=hidden name=a value='add'>
			<input type=hidden name=start value=" . $next_add . ">
			<input name='submit' type='submit' value='".BUTTON_ADD."' onmouseover=\"Tip('" . DIALPLAN_MOD_INBOUND_ADD . "')\" onmouseout='UnTip()'><br>";


		//Erzeugung der Navigationsleiste.


		$url_opts = array ();
		$url_opts['number'] = $_GET['number'];
		$url_opts['search'] = $_GET['search'];
		$url_opts['dir'] = $_GET['dir'];
		$query=$this->filter_navi($ba,$span,$start);
		$record_count = sqlite_num_rows($query);
		$ret .=$functions->displayNavigationBlock($m, $q, $url_opts, $start, $span, $record_count)."</div></form>";;

		//Rückgabe: Inhalt der HTML-Seite
		return $ret;
	}



	/*
	 * Funktion dynamic_table
	 */
	function dynamic_table($next_del,$span,$next_add) {
		//ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();
		require_once("./includes/lang.php");
		$filter="<th colspan='12'>
		<form class='extensions_form'>
		".DIALPLAN_DIRECTION_DIS.": <select name='dir' id='dir' onchange=\"window.location='index.php?m=dialplan&search=".$_GET['search']."&dir='+getElementById('dir').options[getElementById('dir').selectedIndex].value;\">";
		if ($_GET['dir']=='all')	$filter.="<option value='all' selected>".DIALPLAN_DIRECTION_ALL_DIS."</option>";
		else $filter.="<option value='all'>".DIALPLAN_DIRECTION_ALL_DIS."</option>";
		if ($_GET['dir']=='sip-isdn')	$filter.="<option value='sip-isdn' selected>sip-isdn</option>";
		else $filter.="<option value='sip-isdn'>sip-isdn</option>";
		if ($_GET['dir']=='sip-analog')	$filter.="<option value='sip-analog' selected>sip-analog</option>";
		else $filter.="<option value='sip-analog'>sip-analog</option>";
		if ($_GET['dir']=='sip-gsm')	$filter.="<option value='sip-gsm' selected>sip-gsm</option>";
		else $filter.="<option value='sip-gsm'>sip-gsm</option>";
		if ($_GET['dir']=='isdn-isdn')	$filter.="<option value='isdn-isdn' selected>isdn-isdn</option>";
		else $filter.="<option value='isdn-isdn'>isdn-isdn</option>";
		if ($_GET['dir']=='isdn-sip')	$filter.="<option value='isdn-sip' selected>isdn-sip</option>";
		else $filter.="<option value='isdn-sip'>isdn-sip</option>";
		if ($_GET['dir']=='isdn-analog')	$filter.="<option value='isdn-analog' selected>isdn-analog</option>";
		else $filter.="<option value='isdn-analog'>isdn-analog</option>";
		if ($_GET['dir']=='isdn-gsm')	$filter.="<option value='isdn-gsm' selected>isdn-gsm</option>";
		else $filter.="<option value='isdn-gsm'>isdn-gsm</option>";
		if ($_GET['dir']=='analog-analog')	$filter.="<option value='analog-analog' selected>analog-analog</option>";
		else $filter.="<option value='analog-analog'>analog-analog</option>";
		if ($_GET['dir']=='analog-sip')	$filter.="<option value='analog-sip' selected>analog-sip</option>";
		else $filter.="<option value='analog-sip'>analog-sip</option>";
		if ($_GET['dir']=='analog-isdn')	$filter.="<option value='analog-isdn' selected>analog-isdn</option>";
		else $filter.="<option value='analog-isdn'>analog-isdn</option>";
		if ($_GET['dir']=='analog-gsm')	$filter.="<option value='analog-gsm' selected>analog-gsm</option>";
		else $filter.="<option value='analog-gsm'>analog-gsm</option>";
		if ($_GET['dir']=='gsm-sip')	$filter.="<option value='gsm-sip' selected>gsm-sip</option>";
		else $filter.="<option value='gsm-sip'>gsm-sip</option>";
		if ($_GET['dir']=='gsm-isdn')	$filter.="<option value='gsm-isdn' selected>gsm-isdn</option>";
		else $filter.="<option value='gsm-isdn'>gsm-isdn</option>";
		if ($_GET['dir']=='gsm-analog')	$filter.="<option value='gsm-analog' selected>gsm-analog</option>";
		else $filter.="<option value='gsm-analog'>gsm-analog</option>";
		if ($_GET['dir']=='gsm-gsm')	$filter.="<option value='gsm-gsm' selected>gsm-gsm</option>";
		else $filter.="<option value='gsm-gsm'>gsm-gsm</option>";
		$filter.="</select>";

		$filter.="&nbsp;&nbsp;".DIALPLAN_SEARCH_DIS.": <input name='search' value='".$_GET['search']."' onkeydown=\"if (event.keyCode == 13){ window.location='index.php?m=dialplan&dir=".$_GET['dir']."&search='+search.value; return false;}\" >";
		$filter.="&nbsp;&nbsp;
		".DIALPLAN_ENTRIES_DIS.": <select name='ent' id='ent' onchange=\"window.location='index.php?m=dialplan&ent='+getElementById('ent').options[getElementById('ent').selectedIndex].value;\">";
		if ($ba->get('span')=='10')	$filter.="<option value='10' selected>10</option>";
		else $filter.="<option value='10'>10</option>";
		if ($ba->get('span')=='15')	$filter.="<option value='15' selected>15</option>";
		else $filter.="<option value='15'>15</option>";
		if ($ba->get('span')=='20')	$filter.="<option value='20' selected>20</option>";
		else $filter.="<option value='20'>20</option>";
		if ($ba->get('span')=='25')	$filter.="<option value='25' selected>25</option>";
		else $filter.="<option value='25'>25</option>";
		if ($ba->get('span')=='30')	$filter.="<option value='30' selected>30</option>";
		else $filter.="<option value='30'>30</option>";
		if ($ba->get('span')=='35')	$filter.="<option value='35' selected>35</option>";
		else $filter.="<option value='35'>35</option>";
		if ($ba->get('span')=='40')	$filter.="<option value='40' selected>40</option>";
		else $filter.="<option value='40'>40</option>";
		if ($ba->get('span')=='45')	$filter.="<option value='45' selected>45</option>";
		else $filter.="<option value='45'>45</option>";
		if ($ba->get('span')=='50')	$filter.="<option value='50' selected>50</option>";
		else $filter.="<option value='50'>50</option>";
		$filter.="</select>
		</form>
		</th>";




		$query = $ba->select(' SELECT * from dialplan ORDER BY posId, id asc');
		$pi = 1;

		while ($entry = $ba->fetch_array($query)) {
			$ba->update('UPDATE dialplan set posId="'.$pi++.'" WHERE id="'.$entry['id'].'"');
		}


		$start = $_GET['start'];
		$columns = $ba->column_type('dialplan');
		$i = 0;

		//Das Header der Tabelle sind die Spaltennamen von dialplan
		$cols.= "<th>".DIALPLAN_DIRECTION_DIS."</th>";
		foreach ($columns as $column => $type) {
			if (($column != 'id') && ($column != 'config') && ($column != 'isdnid') && ($column != 'activ') && ($column != 'fromDirection') && ($column != 'toDirection') && ($column != 'posId') && ($column != 'comment')) {
				$cols_name[$i] = $column;
				if ($column == "fromId")
					$column = DIALPLAN_FROM_ID_DIS;
				if ($column == "toId")
					$column = DIALPLAN_TO_ID_DIS;
				if ($column == "dest")
					$column = DIALPLAN_DESTINATION_DIS;
				if ($column == "newDest")
					$column = DIALPLAN_NEW_DESTINATION_DIS;
				if ($column == "src")
					$column = DIALPLAN_SOURCE_DIS;
				if ($column == "newSrc")
					$column = DIALPLAN_NEW_SOURCE_DIS;
				$cols .= "<th>" . $column . "</th>";
				$i++;
			}
		}
		$i = 0;
		$cols .= "<th>".DIALPLAN_POSITION_DIS."</th>";

		/*Die Einträge werden von der Tabelle dialplan gelesen, formatiert und in einer
		  Tabelle geschrieben, zusätzlich werden 3 Knöpfe eingebaut: modify, copy und delete*/
		$page = (($start / $span) + 1);
		if (!$start)
			$start = 0;
		$id = 0;

		for ($j = $page; $j <= $page; $j++) {
			$query=$this->filter($ba,$span,$start);
			$rows = '';

			while ($entry = $ba->fetch_array($query)) {


					($entry['activ']==1) ? $rows .= "<tr>" : $rows .= "<tr style='color:grey'>";
					$rows.="<td>".$entry['fromDirection']."-".$entry['toDirection']."</td>";
					$count = sqlite_num_fields($query);
					for ($i = 0; $i < ($count - 8); $i++) {
						$col_name = $cols_name[$i];
						$entry['comment']=str_replace("\r\n","<br>",$entry['comment']);
						if ($entry['comment']!="") $rows .= "<td onmouseover=\"Tip('" . $entry['comment'] . "')\" onmouseout='UnTip()'>";
						else $rows .= "<td >";
						if (strlen($entry[$col_name])>15) $rows.=substr($entry[$col_name],0,15)."...";
						else $rows.=$entry[$col_name];
						$rows.="</td>";
					}

					if ((!isset($_GET['dir']) || $_GET['dir']=='all' || $_GET['dir']=="") && $_GET['search']=="")
					$rows.="<td>
								<a href='misc/up.php?posid=".$entry['posId']."'>
									<img src='./includes/images/up.png' onmouseover=\"Tip('" .DIALPLAN_UP . "')\" onmouseout='UnTip()'>
								</a>
								&nbsp;&nbsp;
								<a href='misc/down.php?posid=".$entry['posId']."'>
									<img src='./includes/images/down.png' onmouseover=\"Tip('" .DIALPLAN_DOWN . "')\" onmouseout='UnTip()'>
								</a>
								&nbsp;
								<form class='extensions_form'>
									<input type='text' name='pos".$entry['id']."' size='3' value='".$entry['posId']."' onmouseover=\"Tip('" . DIALPLAN_MOVE_POS_DIS . "')\" onmouseout='UnTip()' onkeydown=\"if (event.keyCode == 13 && verify_number(pos".$entry['id'].".value, 'Position')){ window.location='./misc/change_pos.php?posid='+pos".$entry['id'].".value+'&id=".$entry['id']."'; return false;}\">
								</form>
							</td>";
					else
					$rows.="<td>
								<img src='./includes/images/up.png'>
								&nbsp;&nbsp;
								<img src='./includes/images/down.png'>
								&nbsp;
								<form class='extensions_form'>
									<input disabled type='text' name='pos".$entry['id']."' size='3' value='".$entry['posId']."' onmouseover=\"Tip('" . DIALPLAN_MOVE_POS_DIS . "')\" onmouseout='UnTip()' onkeydown=\"if (event.keyCode == 13 && verify_number(pos".$entry['id'].".value, 'Position')){ window.location='./misc/change_pos.php?posid='+pos".$entry['id'].".value+'&id=".$entry['id']."'; return false;}\">
								</form>
							</td>";

					$id = $entry['id'];
					$rows .= "<td class='buttons' bgcolor='#DACECE'><font size='3'><form class='extensions_form'>
					<a class='popup_download' onclick=\"javascript:new_window('./misc/dialplan_popup.php?id=" .
					$entry['id'] . "&start=" . $start . "&dir=" . $_GET['dir'] . "&search=" . $_GET['search'] . "')\" ><img src='./includes/images/modify.png' onmouseover=\"Tip('" .
					DIALPLAN_MOD_INBOUND_MODIFY . "')\" onmouseout='UnTip()'></a>" .
					"&nbsp;<a class='popup_download' onclick=\"javascript:new_window('./misc/dialplan_popup.php?copy=" .
    	            $entry['id'] . "&start=" . $next_add . "&dir=" . $_GET['dir'] . "&search=" . $_GET['search'] . "')\" ><img src='./includes/images/copy.png' value='".BUTTON_COPY."' onmouseover=\"Tip('" .
        	        DIALPLAN_MOD_INBOUND_COPY . "')\" onmouseout='UnTip()'></a>" .
            	    "&nbsp;<a class='popup_download' onclick=\"del('dialplan','" . $id . "','".$entry['fromId']."','".$entry['toId']."','id','" .  $next_del . "');\">
            	    <img src='./includes/images/delete.png' onmouseover=\"Tip('" .
					DIALPLAN_MOD_INBOUND_DELETE . "')\" onmouseout='UnTip()'></a>" .
					"<a class='popup_download' onclick=\"javascript:new_window('./misc/more.php?id=" .
					$entry['id'] . "&start=" . $start . "&dir=" . $_GET['dir'] . "&search=" . $_GET['search'] . "')\" >
					<img src='./includes/images/more.png' alt='more' onmouseover=\"Tip('" .
					DIALPLAN_MORE_OPTIONS . "')\" onmouseout='UnTip()'></a></form></font></td></tr>";
			}

		}
		//Die Tabelle mit allen Einträgen
		$table = "<table class='contenttoc' id='table'><tr>".$filter."</tr><tr>" . $cols . "<th></th></tr>" . $rows . "</table>";
		//Rückgabe: Erzeugte Tabelle mit den Einträgen der Tabelle dialplan der Datenbank
		return $table;
	}

}
?>
