<?php

/**
 * @param int id_zasob
 * 
 */
function ZASOB_OBOWIAZKI() {

	$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;
	}

	$obProcesList = array();
	$sql = "select
			w.`ID_PROCES` as w__ID_PROCES
			, w.`OPIS_ZASOB` as w__OPIS_ZASOB
			, p.`ID` as p__ID
			, p.`DESC` as p__DESC
			, p.`OPIS` as p__OPIS
			, p_root.`ID` as p_root__ID
			, p_root.`DESC` as p_root__DESC
			, p_root.`OPIS` as p_root__OPIS
		from `CRM_WSKAZNIK` as w
			left join `CRM_PROCES` as p on (p.`ID`=w.`ID_PROCES`)
			left join `CRM_PROCES` as p_root on (p_root.`ID`=p.`PARENT_ID`)
		where
			w.`ID_ZASOB`={$zasobId}
			and w.`A_STATUS` in('WAITING','NORMAL')
			and p.`A_STATUS` in('WAITING','NORMAL')
	";
	$res = $db->query($sql);
	while ($r = $db->fetch($res)) {
		$obProcesList[$r->w__ID_PROCES][] = $r;
	}

	SE_Layout::menu();

	Lib::loadClass('ProcesHelper');
	$tblProcesyId = ProcesHelper::getZasobTableID('CRM_PROCES');

	?>
	<div class="container">

	<h3>Procesy 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>Nr procesu</th>
			<th>Nazwa / opis</th>
		</tr>
		</thead>
		<tbody>
		<?php $t = 0; foreach ($obProcesList as $k_id => $v_arr) : ?>
			<tr>
				<td>
					<?php echo $k_id; ?>
					<a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $tblProcesyId; ?>#EDIT/<?php echo $k_id; ?>" class="ico-pencil" title="Edytuj rekord"> </a>
					<?php
					//echo App::link($k_id, array('task'=>"CRM_PROCES", 'filtr_id'=>$k_id));
					//procesy5.php?task=PROCES_VIEW_LIST&id_proces=3888
					?>
				</td>
				<td>
				<?php foreach ($v_arr as $r) : ?>
					<?php echo $r->p__DESC; ?><br>
					<em class="muted"><?php echo $r->p__OPIS; ?></em>
					<?php if ($r->w__OPIS_ZASOB) : ?>
						<p class="muted"><?php echo $r->w__OPIS_ZASOB; ?></p>
					<?php endif; ?>
				<?php endforeach; ?>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>

	</div>
	<?php
}
