<?php

/**
 * @param int id_zasob
 * 
 * procesy5.php?_ajax_request=ajax_zasob_search_external_ids&zasob_id=12613
 */
function ZASOB_EXTERNAL_IDS() {

	$zasobId = V::get('id_zasob', 0, $_GET, 'int');
	if (!$zasobId) {
		echo '<div class="alert alert-error">' . "Błąd: brak lub niepoprawny parametr id_zasob" . '</div>';
		return;
	}

	$db = DB::getDB();
	$zasob = $db->get_by_id('CRM_LISTA_ZASOBOW', $zasobId);
	if (!$zasob) {
		echo '<div class="alert alert-error">' . "Brak danych - zasób {$zasobId} nie istnieje." . '</div>';
		return;
	}

	$cnf = Config::getConfFile('external_ids');
	if (!$cnf) {
		echo '<div class="alert alert-error">' . "Brak pliku konfiguracyjnego dla 'external_ids'." . '</div>';
		return;
	}

	$extRows = array();
	$tblLabels = array();
	Lib::loadClass('ProcesHelper');
	$DBG = 0;
	if($DBG > 1){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">cnf (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($cnf);echo'</pre>'; }
	if (!empty($cnf)) {
		$external_ids = array();
		foreach ($cnf as $k_table_name => $v_cnf) {
			$vTblId = ProcesHelper::getZasobTableID($k_table_name);
			$cur_cnf = new stdClass();
			$cur_cnf->id_col = V::get('id_col', 'ID', $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;
			$sql_where = "";
			if ($cur_cnf->search_col_regex) {
				$sql_where = "`{$cur_cnf->search_col}` like '".str_replace('$ID', $zasob->ID, $cur_cnf->search_col_regex)."'";
			} else {
				$sql_where = "`{$cur_cnf->search_col}`='{$zasob->ID}'";
			}

			$sql = "select t.`ID`, {$sql_id_col} as id_col
				from `{$k_table_name}` as t
				where {$sql_where}
			";
			if($DBG > 2){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>'; }
			$res = $db->query($sql);
			while ($r = $db->fetch($res)) {
				$r->tblId = $vTblId;
				$extRows[] = $r;
				if ('ID' != $sql_id_col) {
					$external_ids []= $r->ID . "(" . $r->id_col . ")";
				} else {
					$external_ids []= "" . $r->id_col;
				}
			}
		}
	}

	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">extRows (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($extRows);echo'</pre>';}

	if (!empty($extRows)) {
		foreach ($extRows as $vExtRow) {
			$tblLabels[$vExtRow->tblId] = false;
		}
		$sqlTblsIds = array_keys($tblLabels);
		if (!empty($sqlTblsIds)) {
			$sqlTblsIds = implode(", ", $sqlTblsIds);
			$sql = "select z.`ID`, z.`DESC`, z.`DESC_PL`, z.`OPIS`
				from `CRM_LISTA_ZASOBOW` as z
				where
					z.`ID` in({$sqlTblsIds})
			";
			$res = $db->query($sql);
			while ($r = $db->fetch($res)) {
				$vLabel = $r->DESC_PL;
				$vTitle = htmlspecialchars($r->OPIS);
				if (empty($vLabel) && !empty($r->OPIS)) {
					$posLimit = 20;
					$vLabel = $r->OPIS;
					if (mb_strlen($r->OPIS) > $posLimit) {
						$pos = strpos($r->OPIS, ' - ');
						if ($pos > $posLimit || $pos < 5) {
							$pos = $posLimit;
							$vLabel = mb_substr($r->OPIS, 0, $posLimit, 'utf-8') . '...';
						} else {
							$vLabel = mb_substr($r->OPIS, 0, $pos, 'utf-8');
						}
					}
				}
				if (empty($vLabel)) {
					$vLabel = $r->DESC;
				}
				$vLabel = '<span title="' . "[{$r->ID}] {$vTitle}" . '">' . $vLabel. '</span>';
				$tblLabels[$r->ID] = $vLabel;
			}
		}
	}

	SE_Layout::menu();

	?>
	<div class="container">

	<h3>Rekordy powiązane z zasobem [<?php echo $zasob->ID; ?>]</h3>
	<blockquote><?php echo $zasob->TYPE; ?> <strong><?php echo $zasob->DESC; ?></strong><br>
		<span><?php echo $zasob->OPIS; ?></span>
	</blockquote>

	<table class="table table-bordered table-hover" style="width:auto">
		<thead>
		<tr>
			<th>Tabela</th>
			<th>Nr rekordu</th>
			<th>Nazwa / opis</th>
		</tr>
		</thead>
		<tbody>
		<?php $t = 0; foreach ($extRows as $vExtRow) : ?>
			<tr>
				<td>
					<?php if (!empty($tblLabels[$vExtRow->tblId])) : ?>
						<?php echo $tblLabels[$vExtRow->tblId]; ?>
					<?php else : ?>
						[<?php echo $vExtRow->tblId; ?>]
					<?php endif; ?>
				</td>
				<td>
					<?php echo $vExtRow->ID; ?>
					<a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $vExtRow->tblId; ?>#EDIT/<?php echo $vExtRow->ID; ?>" class="ico-pencil" title="Edytuj rekord"> </a>
				</td>
				<td>
					<?php echo $vExtRow->id_col; ?>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>

	</div>
	<?php
}
