<?php


/**
 * Klasse General
 * Zeigt algemeine Einstellungen von der isgw.conf Datei
 */

class Isgw_causes {

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

		// args
		$m = $_GET['m'];
		$q = $_GET['q'];
		$start = $_GET['start'];
		$order = $_GET['order'];
		$sort = $_GET['sort'];
		include "./includes/functions.php";
		$functions = new functions();
		include('./includes/header.php');

		// build page content
		if(isset($_POST['rest'])){
			$ba=new beroAri();
			$query = $ba->delete('delete FROM isgw_causes');
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','901','503','No channel processing resources');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','902','503','No free ISDN channels');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','903','503','No resources');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','904','404','Number not in dialplan');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','905','603','Invalid number');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','906','503','No free ISDN channels (ports are down)');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','907','503','No free ANALOG ports');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','908','503','No free ANALOG ports (ports are down)');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISGW','910','503','No free GSM ports');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISDN','1','404','Unallocated number');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISDN','17','486','User busy');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISDN','21','603','Call rejected');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('SIP-ISDN','ANY','486','Busy here');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-ISGW','901','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-ISGW','902','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-ISGW','903','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-ISGW','904','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-ISGW','905','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-SIP','404','1','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-SIP','486','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-SIP','603','17','');");
			$query = $ba->insert_("INSERT into isgw_causes (direction, to_cause, from_cause, sip_response_message) VALUES('ISDN-SIP','ANY','17','');");
			$query=$ba->select("select option from activate where id='activate'");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 2) $query =  $ba->update("UPDATE activate SET option=2 where id='activate'");
			}
			$ret.= "<script>aktivieren(1,'causes','2','".ACTIVATE."');</script>";
		}

		$causes_table = $this->dynamic_table();
		$ret .= "<script>
					function del(table,id){
						conf = confirm('".ISGW_CAUSES_DELETE_MESSAGE_DIS."?');
						if (conf==false)
							return false;
						else
							window.location='./misc/delete.php?id='+id+'&table='+table;
					}
				 </script>
					<form class='extensions_form' name='isgw_causes' action='' method='POST' >
					<input type=hidden name=m value=" . $m . ">
					<input type=hidden name=a value='save'>
					<br>
					" . $causes_table.
					"<br><input name='submit' type='submit' value='".BUTTON_ADD."' onclick='javascript:new_window(\"./misc/causes_popup.php\");'>
					<input name='rest' type='submit' value='".BUTTON_LOAD_DEF."' onclick='javascript:return confirm(\"Are you sure?\");'></form>";

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

		//Erzeugung der Navigationsleiste.
		$query = $ba->select('SELECT * FROM isgw_causes');
		$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() {
		//Wenn man Änderugen von der Daten macht werden die in der Tabelle der Datenbank
		//aktualisiert
		$ba = new beroAri();

		if (isset($_POST['save']) && isset($_POST['causes'])) {
			$ba = new beroAri();
			$ba->set("causes_active",$_POST['causes']);
			$query=$ba->select("select option from activate");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 2){
					$query =  $ba->update("UPDATE activate SET option=2");
					$table.= "<script>aktivieren(1,'causes','2','".ACTIVATE."');</script>";
				}
			}
		}

		require_once("./includes/lang.php");

		$columns = $ba->column_type('isgw_causes');
		$i = 0;
		foreach ($columns as $column => $type) {
			if (($column != 'id') && ($column != 'sip_response_message')) {
				$cols_name[$i] = $column;
				if ($column == "direction")
					$column = ISGW_CAUSES_CALL_DIRECTION_DIS;
				if ($column == "to_cause")
					$column = ISGW_CAUSES_TO_CAUSE_DIS;
				if ($column == "from_cause")
					$column = ISGW_CAUSES_FROM_CAUSE_DIS;
$cols .= "<th class='contenttoc'>" . $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*/
		$start = $_GET['start'];
		$page = (($start / 15) + 1);
		if (!$start)
			$start = 0;
		$id = 0;
		for ($j = 1; $j <= $page; $j++) {
			$query = $ba->select('SELECT * FROM isgw_causes ORDER BY id asc limit 15 offset ' . $start);
			$rows = '';
			while ($entry = $ba->fetch_array($query)) {
				$rows .= "<tr>";
				$count = sqlite_num_fields($query);

				$entry['sip_response_message']=str_replace("\r\n","<br>",$entry['sip_response_message']);
				$dir=preg_split("/-/",$entry['direction']);
				if ($entry['sip_response_message']!=""){
					$rows .= "<td onmouseover=\"Tip('" . $entry['sip_response_message'] . "')\" onmouseout='UnTip()'>" . $entry['direction'] . "</td>";
					$rows .= "<td onmouseover=\"Tip('" . $entry['sip_response_message'] . "')\" onmouseout='UnTip()'>" . $entry['from_cause'] . " (".$dir[0].")</td>";
					$rows .= "<td onmouseover=\"Tip('" . $entry['sip_response_message'] . "')\" onmouseout='UnTip()'>" . $entry['to_cause'] . " (".$dir[1].")</td>";
				}else{
					$rows .= "<td>" . $entry['direction'] . "</td>";
					$rows .= "<td>" . $entry['from_cause'] . " (".$dir[0].")</td>";
					$rows .= "<td>" . $entry['to_cause'] . " (".$dir[1].")</td>";
				}
				$id = $entry['id'];
				$rows .= "<td class='buttons' bgcolor='#DACECE'>
							<a class='popup_download' onclick=\"javascript:new_window('./misc/causes_popup.php?id=" .
				$entry['id'] ."');\">
					<img src='./includes/images/modify.png' onmouseover=\"Tip('" . MOD_ISGW_CAUSES_MODIFY . "')\" onmouseout='UnTip()'>
				</a>&nbsp;
				<a class='popup_download' onclick=\"del('isgw_causes','".$id."');\">
            	    <img src='./includes/images/delete.png' onmouseover=\"Tip('" .
					MOD_ISGW_CAUSES_DELETE . "')\" onmouseout='UnTip()'>
				</a>

				</td></tr>";
			}
		}
		$table = "<table class='contenttoc' id='table'><tr>" . $cols . "<th></th></tr>" . $rows . "</table>";

/*
		$table .= "<table class='contenttoc' id='table'>
					<tr>
					<th >".ISGW_CAUSES_TABLE_HEADER_DIS."</th>
					</tr>
					<tr>
					<td>";
					if (isset($_POST['load_def'])) $table.=$this->get_default();
					else  $table.=$this->get_causes();
					$table.="</td>
					</tr>
					<tr>
					<td><input name='save' type='submit' value='".BUTTON_SAVE."' onmouseover=\"Tip('" . GENERAL_SAVE . "')\" onmouseout='UnTip()'>
					<input name='load_def' type='submit' value='".BUTTON_LOAD_DEF."' onmouseover=\"Tip('" . ISGW_CAUSES_LOAD_DEF . "')\" onmouseout='UnTip()'>
					</td>
					</tr>
					</table>
					<br>
					</form>";
		//Rückgabe: Erzeugte Tabelle mit den änderbaren Feldern von den Einstellungen von isgw.conf
*/
		return $table;
	}

}
?>
