<?php


function DODAJ_WYNIK_ANKIETY_FUNC() {
//numer zasobu funkcji [137] Narzedzie do wprowadzania wynikow badan http://se.dev.webone.pl/budynki.php?DODAJ_WYNIK_ANKIETY=1

SE_Layout::menu();
echo '<div class="container">';

$zasobID = '23';// Table USERS2_MARKETING

$userAcl = User::getAcl();
$userAcl->fetchGroups();
//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;display:none;">$userAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userAcl);echo'</pre>';

if (!$userAcl->hasTableAcl($zasobID)) {
	die("Brak uprawnień do tabeli ID={$zasobID}");
}

$tblAcl = $userAcl->getTableAcl($zasobID);
//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblAcl);echo'</pre>';

$forceTblAclInit = ('1' == V::get('_force', '', $_GET));
$tblAcl->init($forceTblAclInit);

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

Lib::loadClass('Data_Source');
$dataSource = new Data_Source($tblAcl->getDB());
$dataSource->setTable($tblAcl->getName());
$dataSource->setCols($tblAcl->getRealFieldList());
$dataSource->setVirtualCols($tblAcl->getVirtualFieldList());

$cols = array();

$ankietaFrm = new stdClass();
$ankietaFrm->S_ADDRESS_STREET = V::get('S_ADDRESS_STREET', '', $_GET);
//$ankietaFrm->T_WORKPOINTS_USER = V::get('T_WORKPOINTS_USER', '', $_GET);
$ankietaFrm->row = null;
$ankietaFrm->rowID = null;

$rows = array();
if (!empty($ankietaFrm->S_ADDRESS_STREET)) {
	$params = array();
	$params['f_S_ADDRESS_STREET'] = '=' . $ankietaFrm->S_ADDRESS_STREET;
	$rows = $dataSource->getItems($params);
}

if (1 != count($rows) && isset($_GET['S_ADDRESS_STREET'])) {
	?>
		<div class="alert alert-error">
			<h4>Error!</h4>
			<?php if (empty($rows)) : ?>
				Nie odnaleziono mieszkania.
			<?php else : ?>
				Odnaleziono <?php echo count($rows); ?> mieszkań.
			<?php endif; ?>
		</div>
	<?php
}

?>
	<form action="" method="get">
		<fieldset>
			<legend>DODAWANIE WYNIKOW ANKIETY / BADANIA nazwa PAWLAK200603</legend>

			<table class="table table-bordered table-striped table-hover">
				<tfoot>
					<tr>
						<td>
							<input type="hidden" name="MENU_INIT" value="DODAJ_WYNIK_ANKIETY_FUNC">
							<button type="submit" class="btn btn-primary">Szukaj</button>
						</td>
					</tr>
				</tfoot>
				<tbody>
					<tr>
						<td>
							<label for="S_ADDRESS_STREET">
								<code>[1121] S_ADDRESS_STREET</code>
								<em>SKROT ULICY z Nr Budynku (np CI50/24 dla CIESZYNSKIEGO 50/24 )</em>
							</label>
							<input type="text" name="S_ADDRESS_STREET" value="<?php echo $ankietaFrm->S_ADDRESS_STREET; ?>" class="span4">
							<?php /* echo $tblAcl->showFormItem('R', 1121, "S_ADDRESS_STREET", $ankietaFrm->S_ADDRESS_STREET); */ ?>
						</td>
					</tr>
<!--
					<tr>
						<td>
							<label for="T_WORKPOINTS_USER">
								<code></code>
								<em>Autor Badań: (Inicjały Handlowca)</em>
							</label>
							<input type="text" name="T_WORKPOINTS_USER" value="<?php echo $ankietaFrm->T_WORKPOINTS_USER; ?>" class="span4">
						</td>
					</tr>
-->
				</tbody>
			</table>
		</fieldset>
	</form>
<?php

if (1 != count($rows)) {
	return;
}

$ankietaFrm->row = reset($rows);
$ankietaFrm->rowID = $ankietaFrm->row->ID;

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

$fieldsAnkieta = array();
$fieldsAnkieta[] = 'S_OTHER_INFO';
$fieldsAnkieta[] = 'I_BAD_DATE';
$fieldsAnkieta[] = 'I_OPER';
$fieldsAnkieta[] = 'I_OPER_ZAD';
$fieldsAnkieta[] = 'I_OPER_FIN';
$fieldsAnkieta[] = 'I_OPER_ZAINT';
$fieldsAnkieta[] = 'T_BAD_DATE';
$fieldsAnkieta[] = 'T_OPER';
$fieldsAnkieta[] = 'T_OPER_ZAD';
$fieldsAnkieta[] = 'T_OPER_FIN';
$fieldsAnkieta[] = 'T_OPER_ZAINT';
$fieldsAnkieta[] = 'TV_BAD_DATE';
$fieldsAnkieta[] = 'TV_OPER';
$fieldsAnkieta[] = 'TV_OPER_ZAD';
$fieldsAnkieta[] = 'TV_OPER_FIN';
$fieldsAnkieta[] = 'TV_OPER_ZAINT';
$fieldsAnkieta[] = 'P_NAME';
$fieldsAnkieta[] = 'P_PHONE';
$fieldsAnkieta[] = 'T_WORKPOINTS_USER';
$fieldsAnkieta[] = 'T_WORKPOINTS_TYPE';
$fieldsAnkieta[] = 'T_WORKPOINTS_DATE';
$fieldsAnkieta[] = 'P_FLOOR_CONTROL';

$fieldsList = array();
$fieldsListAll = $tblAcl->getFields();
foreach ($fieldsAnkieta as $vColName) {
	$vColID = $tblAcl->getFieldIdByName($vColName);
	if (isset($fieldsListAll[$vColID])) {
		$fieldsList[$vColID] = $fieldsListAll[$vColID];

		$cols[$vColID] = '';
		//$cols[$kID] = V::get($vCol['name'], '', $row);
		$cols[$vColID] = V::get("f{$vColID}", $cols[$vColID], $_POST);
	}
	else {
		?>
			<div class="alert alert-error">
				<h4>Error!</h4>
				Brak uprawnień do pola <?php echo $vColName; ?>
			</div>
		<?php
	}
}

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

if ('1' == V::get('ankietaSave', '', $_POST)) {
	$DBG = V::get('DBG', '', $_GET) == 1;
	$id = $ankietaFrm->rowID;
	$args = $_POST;

	$dbID = $tblAcl->getDB();
	$db = DB::getDB($dbID);
	if (!$db) {
		echo '<div class="alert alert-error">' . "Database Error" . '</div>';
		return;
	}

	$tblName = $tblAcl->getName();

	$sqlObj = new stdClass();
	$fields = $tblAcl->getFields();
	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fields (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fields);echo'</pre>';}
	foreach ($fields as $kID => $vField) {
		if (!$tblAcl->isAllowed($kID, 'W', $ankietaFrm->row)) {
			continue;
		}
		if (array_key_exists("f{$kID}", $args)) {
			$sqlObj->{$vField['name']} = $args["f{$kID}"];

			if (empty($args["f{$kID}"])) {// fix bug in input type date and value="0000-00-00"
				$type = $tblAcl->getFieldTypeById($kID);
				if ($type['type'] == 'date') {
					$sqlObj->{$vField['name']} = $type['default'];
				}
			}
		}
	}
	$sqlObj->ID = $id;
	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}

	$ret = $db->UPDATE_OBJ($tblName, $sqlObj);

	if ($ret > 0) {
		echo '<div class="alert alert-success">';
			echo "Rekord zapisany pomyślnie";//"Record saved successfully";
		echo '</div>';
	} else if ($ret == 0) {
		echo '<div class="alert alert-info">';
			echo "Nie wprowadzono żadnych zmian";
		echo '</div>';
	} else {
		echo '<div class="alert alert-error">';
			echo "Database Error";
		echo '</div>';
	}
}

?>
	<form action="" method="post">
		<fieldset>
			<legend>Wynik ankiety dla mieszkania <?php echo $ankietaFrm->S_ADDRESS_STREET; ?></legend>

			<table class="table table-bordered table-striped table-hover">
				<tfoot>
					<tr>
						<td>
							<input type="hidden" name="ankietaSave" value="1">
							<button type="submit" class="btn btn-primary">Zapisz</button>
						</td>
					</tr>
				</tfoot>
				<tbody>
			<?php foreach ($fieldsList as $kID => $vCol) : ?>
				<?php if (!$tblAcl->isAllowed($kID, 'W', $ankietaFrm->row)) continue; ?>
					<tr>
						<td>
							<label for="<?php echo "f{$kID}"; ?>">
								<code>[<?php echo $kID; ?>] <?php echo $vCol['name']; ?></code>
								<?php if (!empty($vCol['opis'])) : ?>
									<em><?php echo $vCol['opis']; ?></em>
								<?php endif; ?>
							</label>
							<?php echo $tblAcl->showFormItem('W', $kID, "f{$kID}", $cols[$kID], array(), $ankietaFrm->row); ?>
						</td>
					</tr>
			<?php endforeach; ?>
				</tbody>
			</table>
		</fieldset>
	</form>
<?php

echo '</div>';// .container
}
