<?php



/**
 * Klasse Sip
 * Zeigt alle Einträge von der Tabelle sip der Datenbank
 */

class Sip {

	/*
	 * Funktion action
	 * wird aufgerufen um einen neuen Eintrag in der Datenbank zu schreiben
	 */
	function action($args) {

		// args
		$m = $_GET['m'];
		$a = $_GET['a'];
		$start = $_GET['start'];
		if ($a = 'add') {
			echo "<script type=\"text/javascript\">new_window(\"./misc/sip_popup.php?start=".$start."\");</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'];
		$order = $_GET['order'];
		$sort = $_GET['sort'];

		$ba = new beroAri();

		$span = 15;
		$query = $ba->select('SELECT * FROM sip');
		$record_count = sqlite_num_rows($query);
		$test=$record_count/$span;
		$test=(int)$test;
		if ($record_count==0){
			$next_add=0;
		}else{
			$next_add=($test*$span);
		}

		$start = $start == '' ? 0 : $start;
		$order = $order;
		$sort = $sort;
		include('./includes/header.php');
		$sip_table = $this->dynamic_table();

		// build page content
		$ret .= "
				 <script>
					function del(table,option,name){
						conf = confirm('".SIP_DELETE_MESS1_DIS." '+name+'? ".SIP_DELETE_MESS2_DIS."');
						if (conf==false)
							return false;
						else
							window.location='./misc/delete.php?id='+option+'&table='+table+'&name='+name;
					}
				 </script>
				 <form class='beroari_form' name='sip' action='' method='GET'>
				 <input type=hidden name=m value=" . $m . ">
				 <input type=hidden name=f value='action'>
				 <input type=hidden name=q value=" . $q . ">
				 <input type=hidden name=start value=" . $next_add . ">
				 <input type=hidden name=span value=" . $span . ">
				 <input type=hidden name=order value=" . $order . ">
				 <input type=hidden name=sort value=" . $sort . ">
				 <input type=hidden name=a value='add'>
				 <br>
				 " . $sip_table . "
				 <br>
				 <input name='submit' type='submit' value='".BUTTON_ADD."' onmouseover=\"Tip('" . MOD_SIP_ADD . "')\" onmouseout='UnTip()'>
				 </form>";

		//ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();

		//Erzeugung der Navigationsleiste.
		$query = $ba->select('SELECT * FROM sip');
		$record_count = sqlite_num_rows($query);
		$url_opts = array ();
		$url_opts['sort'] = $sort;
		$url_opts['order'] = $order;
		$ret .= $functions->displayNavigationBlock($m, $q, $url_opts, $start, $span, $record_count);

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

	/*
	 * Funktion dynamic_table
	 */
	function dynamic_table() {
		//ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();
		require_once("./includes/lang.php");
		$start = $_GET['start'];

		//Das Header der Tabelle sind die Spaltennamen von dialplan
		$columns = $ba->column_type('sip');
		$i = 0;
		foreach ($columns as $column => $type) {
			if (($column != 'id') && ($column != 'secret') && ($column != 'def') && ($column != 'config') && ($column != 'register_option') && ($column != 'regrefresh') && ($column != 'stunserver') && ($column != 'externip') && ($column != 'hard_reregister')) {
				$cols_name[$i] = $column;
				if ($column == "name")
					$column = SIP_NAME_DIS;
				if ($column == "server_address")
					$column = SIP_SERVER_ADDRESS_DIS;
				if ($column == "user")
					$column = SIP_USER_DIS;
				if ($column == "type")
					$column = SIP_REGISTER_DIS;
$cols .= "<th>" . $column . "</th>";
				$i++;
			}
		}

		/*Die Einträge werden von der Tabelle sip gelesen, formatiert und in einer
		  Tabelle geschrieben, zusätzlich werden 2 Knöpfe eingebaut: modify und delete*/
		$page = (($start / 15) + 1);
		if (!$start)
			$start = 0;
		$id = 0;
		for ($j = 1; $j <= $page; $j++) {
			$query = $ba->select('SELECT * FROM sip ORDER BY id asc limit 15 offset ' . $start);
			$rows = '';
			while ($entry = $ba->fetch_array($query)) {
				$rows .= "<tr>";
				$count = sqlite_num_fields($query);
				for ($i = 0; $i < ($count - 9); $i++) {
					$col_name = $cols_name[$i];
					$rows .= "<td>" . $entry[$col_name] . "</td>";

				}
				$id = $entry['id'];
				$rows .= "<td class='buttons' bgcolor='#DACECE'>
				<a class='popup_download' onclick=\"javascript:new_window('./misc/sip_popup.php?id=".$entry['id']."');\" >
					<img src='./includes/images/modify.png' onmouseover=\"Tip('" . MOD_SIP_MODIFY . "')\" onmouseout='UnTip()'>
				</a>&nbsp;
				<a class='popup_download' onclick=\"del('sip','".$id."','".$entry['name']."');\">
            	    <img src='./includes/images/delete.png' onmouseover=\"Tip('" .
					MOD_SIP_DELETE . "')\" onmouseout='UnTip()'>
				</a>
				</td></tr>";
			}
		}

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

}
?>
