<?php

	$actions = array('add', 'form', 'buy', 'sell', 'change', 'edit', 'update', 'delete', 'my');
	$protected_actions = array('add', 'edit', 'my', 'delete');
	
	(key_exists('action', $_GET) && in_array($_GET['action'], $actions)) ? $action = $_GET['action'] : $action = '';
	(key_exists('i', $_GET) && $_GET['i']>0) ? $i = $_GET['i'] : $i = 1;
	(key_exists('id', $_GET) && $_GET['id']>0) ? $id = $_GET['id'] : $id = 0;
	
	if(!session_is_registered('my_id') && in_array($action, $protected_actions)) $action = '';
	
	$nastranu = 10;
	
	if($action=='add' || $action=='update')
	{
		(key_exists('kat', $_POST) && $_POST['kat']<>'') ? $kat = $_POST['kat'] : $kat = 0;
		(key_exists('predmet', $_POST) && $_POST['predmet']<>'') ? $predmet = addslashes(htmlspecialchars($_POST['predmet'])) : $predmet = '';
		(key_exists('cena', $_POST) && $_POST['cena']<>'') ? $cena = addslashes(htmlspecialchars($_POST['cena'])) : $cena = '';
		(key_exists('kontakt', $_POST) && $_POST['kontakt']<>'') ? $kontakt = addslashes(htmlspecialchars($_POST['kontakt'])) : $kontakt = '';
		(key_exists('mesto', $_POST) && $_POST['mesto']<>'') ? $mesto = addslashes(htmlspecialchars($_POST['mesto'])) : $mesto = '';
		(key_exists('popis', $_POST) && $_POST['popis']<>'') ? $popis = addslashes(htmlspecialchars($_POST['popis'])) : $popis = '';
		(key_exists('iid', $_POST) && $_POST['iid']<>'') ? $iid = addslashes(htmlspecialchars($_POST['iid'])) : $iid = '';
		
		if($kat!=0 && $predmet!='' && $cena!='' && $kontakt!='')
		{
			$datum = date("j.m.Y H:i:s");
			$cas = time();
			if($action == 'add')
			{
				$query = "INSERT INTO mobile_bazar (kat, typ, popis, cena, kontakt, mesto, datum, pridal, cas) VALUES 
					('$kat', '$predmet', '$popis', '$cena', '$kontakt', '$mesto', '$datum', '".$_SESSION['my_id']."', '$cas')";
			}
			if($action == 'update' && $iid!=0)
			{
				$query = "UPDATE mobile_bazar SET kat=$kat, typ='$predmet', popis='$popis', cena='$cena', kontakt='$kontakt', mesto='$mesto', datum='$datum', cas='$cas' WHERE ID=$iid";
			}
			
			if($dba->ExecuteQuery($query))
			{
				$add_err = ($action=='add') ? 'Váš inzerát bol pridaný.' : 'Váš inzerát bol upravený.';
			}
			else $add_err = 'Mysql error, skúste to prosím znovu.';
			$action = 'edit';
			$id = $iid;
		}
		else
		{
			$add_err = '';
			if($kat==0) $add_err .= "Nevybrali ste si kategóriu!<br />";
			if($predmet=="") $add_err .= "Nezadali ste čo chcete kúpiť, predať, vymeniť!<br />";
			if($cena=="") $add_err .= "Nezadali ste cenu!<br />";
			if($kontakt=="") $add_err .= "Nezadali ste kontakt na Vás!<br />";
			$action = ($action=='add') ? 'form' : 'edit';
			$id = $iid;
		}
	}
		
	
	if($action == 'delete' && $id!=0)
	{
		$prid_id = $dba->GetOne2('mobile_bazar', 'pridal', 'ID', $id);
		if($prid_id==$_SESSION['my_id'])
		{
			$query = "DELETE FROM mobile_bazar WHERE ID=$id limit 1";
			$dba->ExecuteQuery($query);
			$add_err = "Inzerát zmazaný";
		}
		else echo 'error';
	}
	
	switch ($action):
		case 'buy' : $nadpis = '- kúpim'; break;
		case 'sell' : $nadpis = '- predám'; break;
		case 'change' : $nadpis = '- vymením'; break;
		case 'form' : $nadpis = '- pridanie inzerátu'; break;
		case 'my' : $nadpis = '- moje inzeráty'; break;
		default : $nadpis = '';
	endswitch;
?>
	<script language="JavaScript" type="text/javascript">
	function fnAlert01()
	{
		alert('Pridávať inzeráty môžu len zeregistrovaní a prihlásení používatelia!')
	}
	function fnConfirm01(link)
	{
		if (confirm('Naozaj chcete zmazať tento inzerát?')) window.location = link;
	}
	</script>
    <div id="register_form01">
	  <div id="menu01_head">:: bazár <?php echo $nadpis; ?></div>
	  <div id="melodie_01">
	   <?php if(isset($add_err)) echo "<span id=\"red\">$add_err</span>"; ?>
	   
	   <?php 
	   if($action=='edit' && $id!=0) 
	   { 
   			$query = "SELECT * FROM mobile_bazar WHERE ID=$id";
   			$result = $dba->GetResult($query);
   			if($result) $row = mysql_fetch_array($result);
   			else $row = null;
	   		if($row['pridal']==$_SESSION['my_id'])
	   		{	
	   	
	   	
	   	?>
		<form name="frm" style="padding-top: 10px;" action="index.php?module=bazar&amp;action=update" method="post">
		<table border="0" cellspacing="0" cellpadding="0" align="center">
		  <tr> 
		    <td align="right">Kategória:</td>
		    <td align="left"> 
		      <select name="kat" id="form_field01">
				  <option value="0">Vyberte si prosím kategóriu</option>
				  <option value="1"<?php if ($row['kat']==1) echo " selected=\"selected\"";?>>Kúpim</option>
				  <option value="2"<?php if ($row['kat']==2) echo " selected=\"selected\"";?>>Predám</option>
				  <option value="3"<?php if ($row['kat']==3) echo " selected=\"selected\"";?>>Vymením</option>
			  </select>
		    </td>
		  </tr>
		   <tr> 
		    <td align="right">Predmet:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="predmet" size="33" maxlength="20" value="<?php echo $row['typ']; ?>">
		    </td>
		  </tr>
		  <tr> 
		    <td align="right">Cena:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="cena" size="33" maxlength="10" value="<?php echo $row['cena']; ?>">
		    </td>
		  </tr>
		  <tr> 
		    <td align="right">Kontakt:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="kontakt" size="33" maxlength="50" value="<?php echo $row['kontakt']; ?>">
		    </td>
		  </tr>
		  <tr> 
		  <tr> 
		    <td align="right">Mesto:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="mesto" size="33" maxlength="30" value="<?php echo $row['mesto']; ?>">
		    </td>
		  </tr>
		  <tr> 
		    <td align="right" valign=top>Popis:</td>
		    <td align="left"> 
		      <textarea id="form_field01" rows="6" cols="32" name="popis"><?php echo $row['popis']; ?></textarea>
		    </td>
		  </tr>
		  <tr> 
		    <td></td>
		    <td align="left" valign="top"> 
		            <input id="form_field01" type="submit" name="Submit" value="Send" class="button2">
		            <input id="form_field01" type="reset" name="reset" value="Reset" class="button2">
		            <input type="hidden" name="iid" value="<?php echo $row['ID']; ?>">
		    </td>
		  </tr>
		</table></form>
		<?php
	   		}
	   		else echo "Namáte práva editovať tento inzerát!";
		}
	   
	   
	    if($action=='form') { ?>
		<form name="frm" style="padding-top: 10px;" action="index.php?module=bazar&amp;action=add" method="post">
		<table border="0" cellspacing="0" cellpadding="0" align="center">
		  <tr> 
		    <td align="right">Kategória:</td>
		    <td align="left"> 
		      <select name="kat" id="form_field01">
				  <option value="0">Vyberte si prosím kategóriu</option>
				  <option value="1">Kúpim</option>
				  <option value="2">Predám</option>
				  <option value="3">Vymením</option>
			  </select>
		    </td>
		  </tr>
		   <tr> 
		    <td align="right">Predmet:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="predmet" size="33" maxlength="20">
		    </td>
		  </tr>
		  <tr> 
		    <td align="right">Cena:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="cena" size="33" maxlength="10">
		    </td>
		  </tr>
		  <tr> 
		    <td align="right">Kontakt:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="kontakt" size="33" maxlength="50">
		    </td>
		  </tr>
		  <tr> 
		  <tr> 
		    <td align="right">Mesto:</td>
		    <td align="left"> 
		      <input id="form_field01" type="text" name="mesto" size="33" maxlength="30">
		    </td>
		  </tr>
		  <tr> 
		    <td align="right" valign=top>Popis:</td>
		    <td align="left"> 
		      <textarea id="form_field01" rows="6" cols="32" name="popis"></textarea>
		    </td>
		  </tr>
		  <tr> 
		    <td></td>
		    <td align="left" valign="top"> 
		            <input id="form_field01" type="submit" name="Submit" value="Send" class="button2">
		            <input id="form_field01" type="reset" name="reset" value="Reset" class="button2">
		    </td>
		  </tr>
		</table></form>
		<?php
		}
		else
		{
			switch ($action):
				case 'buy': $where = 'WHERE kat=1 AND mobile_bazar.pridal = mobile_users.ID'; break;
				case 'sell': $where = 'WHERE kat=2 AND mobile_bazar.pridal = mobile_users.ID'; break;
				case 'change': $where = 'WHERE kat=3 AND mobile_bazar.pridal = mobile_users.ID'; break;
				case 'my': $where = 'WHERE mobile_bazar.pridal = '.$_SESSION['my_id'].' AND mobile_bazar.pridal = mobile_users.ID'; break;
				default: $where = 'WHERE mobile_bazar.pridal = mobile_users.ID';
			endswitch;
			
			$query = "SELECT mobile_bazar.*, mobile_users.login as pridal_user FROM mobile_bazar, mobile_users $where ORDER BY ID DESC";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$max = $chunk[1];
			if($max==0 && $action='my') echo 'Ešte ste nepridali žiadne inzeráty.';
			if($max==0 && $action!='my') echo 'V tejto kategórii nie sú žiadne inzeráty.';
	    	$stranok = $chunk[2];
	    	$final_query = $query." limit $lim, $nastranu";
		
			$result = $dba->GetResult($final_query);
			fnNavi($i, $stranok, "module=bazar&amp;action=$action");
			fnWriteBazar($result);
			fnNavi($i, $stranok, "module=bazar&amp;action=$action");
		}
		
		?>
	  </div>
	</div>