<?php

include('includes/hlava.php');
title('Klub priateľov fotografie', '');

if ($link) {
  if ($db_selected) {
  
include('includes/vedla.php');
odkaz('');
include('includes/menu.php');
menu('','');
?>      

        <div class="obsah">   
          <h2>Na<em>stav</em>iť</h2>
          
<?php
  
  $sql = "SELECT A.*
          FROM kpf_author A
          WHERE A.aid='".$_SESSION['aid']."';";
    $result = mysql_query($sql, $link);
    if ($result) {
    $row = mysql_fetch_assoc($result);
    $cl = $row['rss_clanok'];
    $bo = $row['rss_body'];
    $fo = $row['rss_fotka'];
    $em = $row['mail'];
    $hes = $row['heslo'];
    }  
  
		$zobraz_formular = false;
    if (isset($_POST["submit"])){
    $sql = "SELECT A.heslo FROM kpf_author A WHERE A.aid='".$_SESSION['aid']."';";
    $result = mysql_query($sql, $link);
    if ($result) {
    $row = mysql_fetch_assoc($result);
    $hes = $row['heslo'];
    } 
        if (isset($_POST["meno"]) && ($_POST["meno"] != "") OR (isset($_POST["priezvisko"]) && ($_POST["priezvisko"] != "")) OR (isset($_POST["mail"]) && ($_POST["mail"] != ""))){
        
            if (isset($_POST['rss_fotky'])) $fotka = '1'; else $fotka = '0';
            if (isset($_POST['rss_body'])) $body = '1'; else $body = '0';
            if (isset($_POST['rss_clanok'])) $clanok = '1'; else $clanok = '0';
        
            $sql = "UPDATE kpf_author SET meno='".$_POST["meno"]."',
                                          priezvisko='".$_POST["priezvisko"]."',
                                          mail='".$_POST["mail"]."',
                                          rss_clanok='".$clanok."',
                                          rss_body='".$body."',
                                          rss_fotka='".$fotka."'
                                          WHERE aid='".$_SESSION['aid']."';";
            $result = mysql_query($sql, $link);
            if ($result){
                header("Location: index.php");
                $_SESSION['meno'] = $_POST['meno'];
                $_SESSION['priezvisko'] = $_POST['priezvisko'];
            }
        }
        
        if (isset($_POST["old"]) && ($_POST["old"] != "") &&
            (md5($_POST["old"]) == $hes) &&
            isset($_POST["new"]) && ($_POST["new"] != "") &&
            isset($_POST["new2"]) && ($_POST["new2"] != "") &&
            ($_POST["new"] == $_POST["new2"])){

                $sql = "UPDATE kpf_author SET heslo='" . md5($_POST["new"]) . "'
                                          WHERE aid='".$_SESSION['aid']."';";
                $result = mysql_query($sql, $link);
                if ($result) {
                header("Location: index.php");
                } else {
                  echo '<p>Pri zapise problem.</p>';                  
                  $zobraz_formular = true;
                }
  
        } else {
          if (isset($_POST["old"]) && ($_POST["old"] == "") OR
              isset($_POST["new"]) && ($_POST["new"] == "") OR
              isset($_POST["new2"]) && ($_POST["new2"] == "")){
            echo '<p>NEzadali ste vsetky polozky!</p>';             
            $zobraz_formular = true;    
          }
          else              
            $zobraz_formular = true;
        }
      } else {
        if ($_SESSION['aid'] == 0){
        $zobraz_formular = false;
        }
        else {
        $zobraz_formular = true;
        }}   
      if ($zobraz_formular) :        
      
      ?>
          
                <form class="login" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
                  <fieldset>
                  <legend>Osobné údaje</legend>
                    <p class="form"><em>Meno</em> <input name="meno" id="meno" size="15" type="text" value="<?php echo $_SESSION['meno'] ?>"/><br />
                    <em>Priezvisko</em> <input name="priezvisko" id="priezvisko" size="15" type="text" value="<?php echo $_SESSION['priezvisko'] ?>"/><br />
                    <em>E-mail</em> <input name="mail" id="mail" size="25" type="text" value="<?php echo $em ?>"/></p>
                  </fieldset>
                  <fieldset>
                  <legend>Heslo</legend>
                    <p class="form"><em>Staré heslo</em> <input name="old" id="old" size="15" type="password"/><br />
                    <em>Nové heslo</em> <input name="new" id="new" size="15" type="password"/><br />
                    <em>Potvrdenie hesla</em> <input name="new2" id="new2" size="15" type="password"/></p>
                  </fieldset>
                  <fieldset>
                  <legend>Notifikáciu</legend>
                  <p class="form">
                  <label class="rss">fotky</label><input class="check" type="checkbox" name="rss_fotky" value="fotky" <?php if ($fo == '1') echo 'checked="checked"'?> />
                  <label class="rss">body</label><input class="check" type="checkbox" name="rss_body" value="body" <?php if ($bo == '1') echo 'checked="checked"'?> />
                  <label class="rss">clanky</label><input class="check" type="checkbox" name="rss_clanok" value="clanok" <?php if ($cl == '1') echo 'checked="checked"'?> /><br /></p>
                  </fieldset>
                  <input class="ok" name="submit" type="submit" id="submit" value="zmeniť"/>
              </form>
      <?php
        else :
        if ($_SESSION['aid'] == 0){
          echo '<p>Nie si prihlaseny. Nemôžeš meniť nastavenia!</p>';
        }
      endif; // kontrola SESSION
	}
	mysql_close($link);
}
include('includes/pata.php'); 
?>
