<?php


function task_CRM_EXT_IDS_TREE() {

	// fetch tree root
	$tree = array();
	$sql = "select z.`ID`, z.`DESC`
		from `CRM_LISTA_ZASOBOW` as z
		where
			z.`PARENT_ID`=0
	";
	$res = DB::query( $sql );
	while ($r = DB::fetch( $res )) {
		$tree[] = $r;
	}

	echo '<div id="tree-ext-ids">';
	foreach ($tree as $r) {
		echo '<div class="node">';
			echo $r->ID . ' ' . $r->DESC;
		echo '</div>';
	}
	echo '</div>';
	
	

	echo '<script type="text/javascript">' . "
//TODO: jQuery(document).ready(function(){
});
	" . '</script>';

}


function task_CRM_EXT_IDS() {

	echo'<p>'."F." . __FUNCTION__ . "..." . '</p>';

	$cnf = Config::getConfFile('external_ids');
	if (!$cnf) {
		echo'Config error (external_ids)';
		return;
	}

	if (empty($cnf)) {
		echo'Config empty (external_ids)';
		return;
	}


	if (($remote_id = V::get('remote_id', '', $_REQUEST, 'int')) > 0 && ($remote_table = V::get('remote_table', '', $_REQUEST)) != '') {
		echo'<p>';
			echo App::link("CRM_EXT_IDS", "?task=CRM_EXT_IDS");
			echo " - Remote {$remote_table}.{$remote_id} ...";
		echo'</p>';

		if (!array_key_exists($remote_table, $cnf)) {
			echo'<p class="box box-red">'."Table not exists in config".'</p>';
			return;
		}

		$r = DB::get_by_id( $remote_table, $remote_id );
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;display:none">';print_r($r);echo'</pre>';

		echo'<dl style="border:1px solid #000;">';
			echo'<dt style="border-bottom:1px solid #000;background:#eee;">';
						echo $remote_table.'.';
						echo $r->ID;
						echo ' <em>'.$r->T_TELBOX_TYPE.'</em>';
						echo ' <b>'.$r->T_TELBOX_NAME.'</b>';
						echo' <span style="font-size:small">('.$r->S_ADDRESS_STREET.')</span>';
			echo'</dt>';
			echo'<dd>';

		if ($r->CRM_LISTA_ZASOBOW_ID > 0) {
			echo'<p>'."Przypisany do zasobu ".$r->CRM_LISTA_ZASOBOW_ID.".".'</p>';

			$tree = new Tree( 'CRM_LISTA_ZASOBOW' );
			$tree->show_rec( $r->CRM_LISTA_ZASOBOW_ID );

		} else {
			echo'<p class="err">'."Brak przypisanego zasobu.".'</p>';
		}

			echo'</dd>';
		echo'</dl>';

		return;
	}

	$external_tbls = array();
	foreach ($cnf as $k_table_name => $v_cnf) {
		$cur_cnf = new stdClass();
		$cur_cnf->table_name = $k_table_name;
		$cur_cnf->id_col = V::get('id_col', 'ID', $v_cnf);
		$cur_cnf->allowed_cols = V::get('allowed_cols', '', $v_cnf);
		$cur_cnf->search_col = V::get('search_col', 'CRM_LISTA_ZASOBOW_ID', $v_cnf);
		$cur_cnf->search_col_regex = V::get('search_col_regex', '', $v_cnf);
		$sql_id_col = $cur_cnf->id_col;
		$cur_cnf->cols = array();
		$cur_cnf->sql = "";

		$cur_cnf->cols ['ID']= 'ID';
		$cur_cnf->cols [$cur_cnf->id_col]= $cur_cnf->id_col;
		$cur_cnf->cols [$cur_cnf->search_col]= $cur_cnf->search_col;
		if (!empty($cur_cnf->allowed_cols)) {
			$cols = explode(',', $cur_cnf->allowed_cols);
			foreach ($cols as $v_col) {
				$v_col = trim($v_col);
				$k_col = $v_col;
				if (strpos($v_col, ':') !== false) {
					list($k_col, $v_col) = explode(':', $v_col, 2);
				}
				$cur_cnf->cols [$k_col]= $v_col;
			}
		}

		$sql_where = "";
		if ($cur_cnf->search_col_regex) {
			$sql_where = "`".$cur_cnf->search_col."` like '".str_replace('$ID', '%', $cur_cnf->search_col_regex)."'";
		} else {
			$sql_where = "`".$cur_cnf->search_col."` is not null";
		}

		$sql_cols = array();
		foreach ($cur_cnf->cols as $k_col => $v_col) {
			$sql_cols[] = "t.`".$k_col."` as ".$v_col."";
		}
		$cur_cnf->sql = "select ".implode(",", $sql_cols)."
			from `".$cur_cnf->table_name."` as t
			where ".$sql_where."
		";

		$external_tbls[] = $cur_cnf;
	}

	foreach ($external_tbls as $cur_cnf) {
		echo'<p>'.$cur_cnf->table_name.'</p>';
		$res = DB::query( $cur_cnf->sql );
		echo'<table border=1>';
		echo'<thead>';
		echo'<tr>';
		foreach ($cur_cnf->cols as $k_col => $v_col) {
			echo'<th title="'.$k_col.'">'.$v_col.'</th>';
		}
		echo'</tr>';
		echo'</thead>';
		echo'<tbody>';
		while ($r = DB::fetch( $res )) {
			echo'<tr>';
			foreach (get_object_vars($r) as $k => $v) {
				echo'<td>';
					if ($k == 'ID') {
						echo App::link($v, "?task=CRM_EXT_IDS&remote_table=".$cur_cnf->table_name."&remote_id=".$v);
					} else {
						echo $v;
					}
				echo'</td>';
			}
			echo'</tr>';
		}
		echo'</tbody>';
		echo'</table>';

		echo'<hr />';
	}

	echo'<p>'."EOF F." . __FUNCTION__ . "..." . '</p>';

}
