<?php
include "head.php";
$meta2 = '<script type="text/javascript">
$(document).ready(function() {

  $("#jazyk_def").click(function() {
    if ($("#jazyk_def").is(":checked")) {
      $("#ac_jazyk").attr("disabled", true).css({"color" : "grey"});
    } else {
      $("#ac_jazyk").removeAttr("disabled").css({"color" : "#333333"});
    };
  });
  
  $("#vzhled_def").click(function() {
    if ($("#vzhled_def").is(":checked")) {
      $("#ac_vzhled").attr("disabled", true).css({"color" : "grey"});
    } else {
      $("#ac_vzhled").removeAttr("disabled").css({"color" : "#333333"});
    };
  });
  
  $("#tinymce_def").click(function() {
    if ($("#tinymce_def").is(":checked")) {
      $("#tinymce_1, #tinymce_2").attr("disabled", true);
      $("#tinymce_1_lab, #tinymce_2_lab").css({"color" : "grey"});
    } else {
      $("#tinymce_1, #tinymce_2").removeAttr("disabled");
      $("#tinymce_1_lab, #tinymce_2_lab").css({"color" : "#333333"});
    };
  });
  
';

$query_set = mysql_query("SELECT set_jazyk, set_jazyk2, set_vzhled, set_tinymce FROM jtcms_uzivatele WHERE id='".intval($_SESSION['userid'])."' LIMIT 1");
$result_set = mysql_fetch_array($query_set);

if (empty($result_set['set_jazyk']) or empty($result_set['set_jazyk2'])) {$meta2 .= '$("#jazyk_def").attr("checked", true);'."\n\r".'$("#ac_jazyk").attr("disabled", true).css({"color" : "grey"});'."\n\r";};
if (empty($result_set['set_vzhled'])) {$meta2 .= '$("#vzhled_def").attr("checked", true);'."\n\r".'$("#ac_vzhled").attr("disabled", true).css({"color" : "grey"});'."\n\r";};
if (empty($result_set['set_tinymce'])) {$meta2 .= '$("#tinymce_def").attr("checked", true);'."\n\r".'$("#tinymce_1, #tinymce_2").attr("disabled", true);'."\n\r".'$("#tinymce_1_lab, #tinymce_2_lab").css({"color" : "grey"});'."\n\r";};

$meta2 .= '  
});
</script>';
include_head($lang['my_account_title']);

if (!isset($_GET['action'])) {$_GET['action'] = "default";};
switch ($_GET['action']) {

case "change-data" :

if (!empty($_POST)) {
  $id = intval($_SESSION['userid']);

  $jmeno = $_POST['jmeno'];
  $mail = $_POST['mail'];
  $heslo_old = $_POST['heslo_old'];
  $heslo = $_POST['heslo'];
  $heslo2 = $_POST['heslo2'];
  
  $query = mysql_query("SELECT * FROM jtcms_uzivatele WHERE id='".$id."' LIMIT 1");
  $result = mysql_fetch_array($query);
  
  if (!empty($mail) && !empty($jmeno) && ((empty($heslo_old) && empty($heslo) && empty($heslo2)) or (!empty($heslo_old) && !empty($heslo) && !empty($heslo2) && sha1($heslo_old) == $result['heslo'] && $heslo == $heslo2))) {
  
    if ($prava['uzivatele'] == 1) {
      $jmeno = mysql_real_escape_string($jmeno);
      mysql_query("UPDATE jtcms_uzivatele SET jmeno='".$jmeno."' WHERE id='".$id."' LIMIT 1");
    };
    
    if (!empty($heslo_old) && !empty($heslo) && !empty($heslo2) && sha1($heslo_old) == $result['heslo'] && $heslo == $heslo2) {
      $heslo = mysql_real_escape_string(sha1($heslo));
      mysql_query("UPDATE jtcms_uzivatele SET heslo='".$heslo."' WHERE id='".$id."' LIMIT 1");
    };
    
    $mail = mysql_real_escape_string($mail);
    mysql_query("UPDATE jtcms_uzivatele SET mail='".$mail."' WHERE id='".$id."' LIMIT 1");
    
    Header("Location: my_account.php?work=edited-data");
    echo '<script type="text/javascript">location.replace("my_account.php?work=edited-data")</script>';    
  
  } else {
    echo '<br />';  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['my_account_error'].'</div>';
    if (empty($mail)) {echo '<p>'.$lang['my_account_er_1'].'</p>';};
    if (empty($jmeno)) {echo '<p>'.$lang['my_account_er_2'].'</p>';};
    if (!((empty($heslo_old) && empty($heslo) && empty($heslo2)) or (!empty($heslo_old) && !empty($heslo) && !empty($heslo2) && sha1($heslo_old) == $result['heslo'] && $heslo == $heslo2))) {echo '<p>'.$lang['my_account_er_3'].'</p>';};
    echo '</div>';
  };
} else {
  Header("Location: my_account.php");
  echo '<script type="text/javascript">location.replace("my_account.php")</script>';
};

break;

case "change-info" :

if (!empty($_POST)) {
  $info = $_POST['info'];
  $narozeniny1 = $_POST['narozeniny1'];
  $narozeniny2 = $_POST['narozeniny2'];
  $narozeniny3 = $_POST['narozeniny3'];
  $pohlavi = $_POST['pohlavi'];
  
  if (!empty($info) or (!empty($narozeniny1) && !empty($narozeniny2) && !empty($narozeniny3)) or !empty($pohlavi)) {
  
    if (!empty($narozeniny1) && !empty($narozeniny2) && !empty($narozeniny3)) {
      $narozeniny = $narozeniny3.'-'.$narozeniny2.'-'.$narozeniny1.' 00:00:00';
      mysql_query("UPDATE jtcms_uzivatele SET info2='".$narozeniny."' WHERE id='".intval($_SESSION['userid'])."' LIMIT 1");
    };
    
        if ($pohlavi == 1) {$pohlavi = $lang['my_account_info3_1'];}
    elseif ($pohlavi == 2) {$pohlavi = $lang['my_account_info3_2'];};
    mysql_query("UPDATE jtcms_uzivatele SET info3='".mysql_real_escape_string($pohlavi)."' WHERE id='".intval($_SESSION['userid'])."' LIMIT 1");

    ksort($info);
      echo '<br>';
    print_r($info);
      echo '<br>';
    
    $dotaz = "";
    foreach ($info as $key => $val) {
      $key = preg_replace("/^'/", "", $key);
      $key = preg_replace("/'$/", "", $key);
      if (!empty($val)) {
        $dotaz .= 'info'.$key.'=\''.mysql_real_escape_string($val).'\', '; 
      } else {
        $dotaz .= 'info'.$key.'=\'\', ';
      };
    };
    
    $dotaz = preg_replace("/, $/", "", $dotaz);

    mysql_query("UPDATE jtcms_uzivatele SET ".$dotaz." WHERE id='".intval($_SESSION['userid'])."' LIMIT 1");

    Header("Location: my_account.php?work=edited-info");
    echo '<script type="text/javascript">location.replace("my_account.php?work=edited-info")</script>'; 
     
  } else {
    Header("Location: my_account.php");
    echo '<script type="text/javascript">location.replace("my_account.php")</script>';  
  };
} else {
  Header("Location: my_account.php");
  echo '<script type="text/javascript">location.replace("my_account.php")</script>';
};

break;

case "change-set" :

if (!empty($_POST)) {
  if (isset($_POST['jazyk_def']) && $_POST['jazyk_def'] == 1) {$jazyk_def = TRUE;} else {$jazyk_def = FALSE;};
  if (isset($_POST['vzhled_def']) && $_POST['vzhled_def'] == 1) {$vzhled_def = TRUE;} else {$vzhled_def = FALSE;};
  if (isset($_POST['tinymce_def']) && $_POST['tinymce_def'] == 1) {$tinymce_def = TRUE;} else {$tinymce_def = FALSE;};
  
  $jazyk = $_POST['jazyk'];
  $jazyk2 = preg_replace("/_(.*)$/", "", $jazyk);
  $vzhled = $_POST['vzhled'];
  $tinymce = $_POST['tinymce'];
  
  $id = intval($_SESSION['userid']);
  
  if (($jazyk_def or !empty($jazyk)) && ($vzhled_def or !empty($vzhled)) && ($tinymce_def or !empty($tinymce))) {
  
    if ($jazyk_def) {
      mysql_query("UPDATE jtcms_uzivatele SET set_jazyk='', set_jazyk2='' WHERE id='".$id."' LIMIT 1");
    } else {
      mysql_query("UPDATE jtcms_uzivatele SET set_jazyk='".mysql_real_escape_string($jazyk)."', set_jazyk2='".mysql_real_escape_string($jazyk2)."' WHERE id='".$id."' LIMIT 1");
    };
    
    if ($vzhled_def) {
      mysql_query("UPDATE jtcms_uzivatele SET set_vzhled='' WHERE id='".$id."' LIMIT 1");
    } else {
      mysql_query("UPDATE jtcms_uzivatele SET set_vzhled='".mysql_real_escape_string($vzhled)."' WHERE id='".$id."' LIMIT 1");
    };
    
    if ($tinymce_def) {
      mysql_query("UPDATE jtcms_uzivatele SET set_tinymce='' WHERE id='".$id."' LIMIT 1");
    } else {
      mysql_query("UPDATE jtcms_uzivatele SET set_tinymce='".mysql_real_escape_string($tinymce)."' WHERE id='".$id."' LIMIT 1");
    };

    Header("Location: my_account.php?work=edited-set");
    echo '<script type="text/javascript">location.replace("my_account.php?work=edited-set")</script>'; 
        
  } else {
    echo '<br />';
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['my_account_error'].'</div>';
    if (!$jazyk_def && empty($jazyk)) {echo '<li>'.$lang['my_account_er_4'].'</li>';};
    if (!$vzhled_def && empty($vzhled)) {echo '<li>'.$lang['my_account_er_5'].'</li>';};
    if (!$tinymce_def && empty($tinymce)) {echo '<li>'.$lang['my_account_er_6'].'</li>';};
    echo '</div>';
  };
};

break;

default :

$info_img = array(1 => '<img src="stuff/img/icons/user.png" alt="" />',
                  2 => '<img src="stuff/img/icons/favourites.png" alt="" />',
                  3 => '<img src="stuff/img/icons/users.png" alt="" />',
                  4 => '<img src="stuff/img/icons/home.png" alt="" />',
                  5 => '<img src="stuff/img/icons/book.png" alt="" />',
                  12 => '<img src="stuff/img/icons/online.png" alt="" />',
                  13 => '<img src="stuff/img/icons/phone.png" alt="" />'
                 );
                 
$id = intval($_SESSION['userid']); 

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_kategorie WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$kategorii = $result['pocet'];

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_galerie WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$galerii = $result['pocet'];

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_soubory_slozky WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$slozek = $result['pocet'];

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_soubory WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$souboru = $result['pocet'];

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_stitky WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$stitku = $result['pocet'];

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_clanky WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$clanku = $result['pocet'];

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_stranky WHERE pridal='".$id."'");
$result = mysql_fetch_array($query);
$stranek = $result['pocet'];

$query = mysql_query("SELECT * FROM jtcms_galerie WHERE pridal='".$id."'");

  $num = 0;
  while ($result = mysql_fetch_array($query)) {
    $adresar = opendir("./data/gal/".$result['id']);
    while ($soubor = readdir($adresar)){
      $num++;
    };
    $num = $num-2;
  };
         
  $fotek = $num;

$query = mysql_query("SELECT * FROM jtcms_uzivatele WHERE id='".$_SESSION['userid']."' LIMIT 1");
$result = mysql_fetch_array($query);
$jmeno = $result['jmeno'];
$mail = $result['mail'];

if ($result['info2'] == "0000-00-00 00:00:00") {
  $day = "00";
  $month = "00";
  $year = "0000";
} else {
  $day = StrFTime("%d", strtotime($result['info2']));
  $month = StrFTime("%m", strtotime($result['info2']));
  $year = StrFTime("%Y", strtotime($result['info2']));
};
  
?>

<div class="filtr_but" onclick="$('#my_account_form_change_data').slideToggle();"><img src="stuff/img/icons/shield.png" alt="" /><?php echo $lang['my_account_change_data']; ?></div>
<div class="filtr_but" onclick="$('#my_account_form_change_info').slideToggle();"><img src="stuff/img/icons/clien list.png" alt="" /><?php echo $lang['my_account_change_info']; ?></div>
<div class="filtr_but" onclick="$('#my_account_form_change_set').slideToggle();"><img src="stuff/img/icons/gear.png" alt="" /><?php echo $lang['my_account_change_settings']; ?></div>

<h1 class="tal"><?php echo $lang['my_account_account'].' '.$jmeno; ?></h1>
<br />

<?php 
if (!isset($_GET['work'])) {$_GET['work'] = "default";};
switch ($_GET['work']) {
case "edited-info" :
    echo '<div class="oznameni_div">';
    echo '<p>'.$lang['my_account_info_was_edited'].'</p>';
    echo '</div>';
break;     

case "edited-data" :
    echo '<div class="oznameni_div">';
    echo '<p>'.$lang['my_account_data_was_edited'].'</p>';
    echo '</div>';
break;

case "edited-set" :
    echo '<div class="oznameni_div">';
    echo '<p>'.$lang['my_account_set_was_edited'].'</p>';
    echo '</div>';
break;
};
?>

  <form method="post" action="my_account.php?action=change-set" class="my_account_form" id="my_account_form_change_set">
    <fieldset class="fieldset5">
      <legend class="legend5"><?php echo $lang['my_account_settings']; ?></legend>
      
      <label for="ac_jazyk"><?php echo $lang['my_account_form_6']; ?></label>
      <input type="checkbox" name="jazyk_def" id="jazyk_def" title="<?php echo $lang['my_account_use_def']; ?>" value="1" />
      
  <select name="jazyk" size="1" id="ac_jazyk" class="input">
<?php
  $adresar = opendir("./stuff/lang");

    while ($soubor = readdir($adresar)){
      if ($soubor != "." && $soubor != ".." && is_file("./stuff/lang/".$soubor)) {
        $soubor = str_replace(".php", "", $soubor);
        echo '<option value="'.$soubor.'"';
        if ($soubor == $result['hodnota2']) {echo ' selected';};
        echo ' /> '.$soubor."\n";
      };
    };
?>
  </select>
  <br />
  
      <label for="ac_vzhled"><?php echo $lang['my_account_form_7']; ?></label>
      <input type="checkbox" name="vzhled_def" id="vzhled_def" title="<?php echo $lang['my_account_use_def']; ?>" value="1" />
  <select name="vzhled" size="1" id="ac_vzhled" class="input">
<?php
  $adresar = opendir("./stuff/themes");

    while ($soubor = readdir($adresar)){
      if ($soubor != "." && $soubor != ".." && is_dir("./stuff/themes/".$soubor) && file_exists("./stuff/themes/".$soubor."/style.css")) {
        echo '<option value="'.$soubor.'"';
        if ($soubor == $result['hodnota']) {echo ' selected';};
        echo ' />'.$soubor."\n";
      };
    };
?>
  </select>
  <br />
  
      <?php 
      $set_tinymce = mysql_query("SELECT set_tinymce FROM jtcms_uzivatele WHERE id='".$id."' LIMIT 1");
      $set_tinymce = mysql_fetch_array($set_tinymce);
      $set_tinymce = $set_tinymce['set_tinymce'];
      ?>
      <label for="tinymce_1"><?php echo $lang['my_account_form_8']; ?></label>
      <div class="type_radio">
        <input type="checkbox" class="checkbox" name="tinymce_def" id="tinymce_def" title="<?php echo $lang['my_account_use_def']; ?>" value="1" />
        <input type="radio" name="tinymce" value="1" id="tinymce_1"<?php if (!empty($set_tinymce) && $set_tinymce == 1) {echo ' checked';} elseif (empty($set_tinymce)) {echo ' checked';}; ?> /><label class="no_lab_set" for="tinymce_1" id="tinymce_1_lab"><?php echo $lang['my_account_form_9']; ?></label>
        <input type="radio" name="tinymce" value="2" id="tinymce_2"<?php if (!empty($set_tinymce) && $set_tinymce == 2) {echo ' checked';}; ?> /><label class="no_lab_set" for="tinymce_2" id="tinymce_2_lab"><?php echo $lang['my_account_form_10']; ?></label>
      </div>
    </fieldset>
    <br />
    <div class="tac">
      <input type="submit" name="submit" class="submit" value="<?php echo $lang['my_account_form_submit4']; ?>" />
    </div>
  </form>
  
  <form method="post" action="my_account.php?action=change-data" class="my_account_form" id="my_account_form_change_data">
    <fieldset class="fieldset4">
      <legend class="legend4"><?php echo $lang['my_account_data']; ?></legend>
      <label for="ac_jmeno"><?php echo $lang['my_account_form_5']; ?></label><input type="text" name="jmeno" id="ac_jmeno" class="input" value="<?php echo $result['jmeno']; ?>"<?php if ($prava['uzivatele'] != 1) {echo ' disabled';}; ?> /><br />
      <label for="ac_mail"><?php echo $lang['my_account_form_4']; ?></label><input type="text" name="mail" id="ac_mail" class="input" value="<?php echo $result['mail']; ?>" /><br />
      <label for="ac_heslo_old"><?php echo $lang['my_account_form_1']; ?></label><input type="password" name="heslo_old" id="ac_heslo_old" class="input" /><br />
      <label for="ac_heslo"><?php echo $lang['my_account_form_2']; ?></label><input type="password" name="heslo" id="ac_heslo" class="input" /><br />
      <label for="ac_heslo2"><?php echo $lang['my_account_form_3']; ?></label><input type="password" name="heslo2" id="ac_heslo2" class="input" /><br />
    </fieldset>
    <br />
    <div class="tac">
      <input type="submit" name="submit" value="<?php echo $lang['my_account_form_submit2']; ?>" class="submit" />
    </div>
  </form>
  
  <form method="post" action="my_account.php?action=change-info" class="my_account_form" id="my_account_form_change_info">
    <fieldset class="fieldset1">
      <legend class="legend1"><?php echo $lang['my_account_personal_info']; ?></legend>
      
      <label for="info1"><?php echo $info_img['1'].' '.$lang['my_account_info1']; ?></label>
        <input type="text" name="info['1']" id="info1" class="input" value="<?php echo my_acc_info_form('1'); ?>" />
      <br />
      
      <div class="nf"></div>
        
      <label for="info2_1"><?php echo $info_img['2'].' '.$lang['my_account_info2']; ?></label>
        <select name="narozeniny1" id="info2_1" class="input select_short">
          <option value="00" /><?php echo $lang['my_account_info2_1'];
          for ($c = 1; $c < 32; $c++) {
            echo '<option value="'.$c.'"';
              if ((empty($_POST['narozeniny1']) && $day == $c) or (!empty($_POST['narozeniny1']) && $_POST['narozeniny1'] == $c)) {
                echo ' selected';
              };
            echo ' />'.$c;
          };
          ?>
        </select>
        <select name="narozeniny2" id="info2_2" class="input select_short">
          <option value="00" /><?php echo $lang['my_account_info2_2'];
          for ($c = 1; $c < 13; $c++) {
            echo '<option value="'.$c.'"';
              if ((empty($_POST['narozeniny2']) && $month == $c) or (!empty($_POST['narozeniny2']) && $_POST['narozeniny2'] == $c)) {
                echo ' selected';
              };
            echo ' />'.$c;
          };
          ?>
        </select>
        <select name="narozeniny3" id="info2_3" class="input select_short">
          <option value="0000" /><?php echo $lang['my_account_info2_3'];
          for ($c = 1932; $c < 2013; $c++) {
            echo '<option value="'.$c.'"';
              if ((empty($_POST['narozeniny3']) && $year == $c) or (!empty($_POST['narozeniny3']) && $_POST['narozeniny3'] == $c)) {
                echo ' selected';
              };
            echo ' />'.$c;
          };
          ?>
        </select>          
      <br />
      
      <div class="nf"></div>
      
      <label for="info3"><?php echo $info_img['3'].' '.$lang['my_account_info3']; ?></label>
        <select name="pohlavi" id="info5" class="input select_short">
          <option value="" />
          <option value="1"<?php if ((empty($_POST['pohlavi']) && $result['info3'] == $lang['my_account_info3_1']) or (!empty($_POST['pohlavi']) && $_POST['pohlavi'] == $lang['my_account_info3_1'])) {echo ' selected';}; ?> /><?php echo $lang['my_account_info3_1']; ?>
          <option value="2"<?php if ((empty($_POST['pohlavi']) && $result['info3'] == $lang['my_account_info3_2']) or (!empty($_POST['pohlavi']) && $_POST['pohlavi'] == $lang['my_account_info3_2'])) {echo ' selected';}; ?> /><?php echo $lang['my_account_info3_2']; ?>
        </select>
      <br /> 
      
      <div class="nf"></div>
      
      <label for="info4"><?php echo $info_img['4'].' '.$lang['my_account_info4']; ?></label>
        <input type="text" name="info['4']" id="info4" class="input" value="<?php echo my_acc_info_form('4'); ?>" />
      <br /> 
      
      <div class="nf"></div>
        
    </fieldset>
    <br />
    <fieldset class="fieldset2">
      <legend class="legend2"><?php echo $lang['my_account_contact_info']; ?></legend>
      
  <?php
  
    for ($c = 12; $c < 19; $c++) {
      echo '<label for="info'.$c.'">'.$info_img[$c].' '.$lang['my_account_info'.$c].'</label>';
        echo '<input type="text" name="info[\''.$c.'\']" id="info'.$c.'" class="input" value="'.my_acc_info_form($c).'" />';
      echo '<br />';
      echo '<div class="nf"></div>';
      echo "\n\r";
    };
  ?> 
            
    </fieldset>
    <br />
    <fieldset class="fieldset3">
      <legend class="legend3"><?php echo $lang['my_account_other_info']; ?></legend>

      <label for="info5"><?php echo $info_img['5'].' '.$lang['my_account_info5']; ?><br /><span class="popis"><?php echo $lang['my_account_info5_1']; ?></span></label>
        <textarea wrap="hard" name="info['5']" id="info5" class="input"><?php echo my_acc_info_form('5'); ?></textarea>
      <br />
      
    </fieldset>
    <br />
    <div class="tac">
      <input type="submit" name="submit" value="<?php echo $lang['my_account_form_submit3']; ?>" class="submit" />
    </div>
  </form>
  
<table class="my_account_table">
<tr class="prvni_tr"><td class="first_a_last" colspan="2"><?php echo $lang['my_account_from_this_account']; ?></td></tr>
<?php
echo '<tr><td>'.$kategorii.'</td><td>'.$lang['my_account_num_3'].'</td></tr>';
echo '<tr><td>'.$galerii.'</td><td>'.$lang['my_account_num_4'].'</td></tr>';
echo '<tr><td>'.$fotek.'</td><td>'.$lang['my_account_num_8'].'</td></tr>';
echo '<tr><td>'.$slozek.'</td><td>'.$lang['my_account_num_7'].'</td></tr>';
echo '<tr><td>'.$souboru.'</td><td>'.$lang['my_account_num_6'].'</td></tr>';
echo '<tr><td>'.$stitku.'</td><td>'.$lang['my_account_num_5'].'</td></tr>';
echo '<tr><td>'.$clanku.'</td><td>'.$lang['my_account_num_1'].'</td></tr>';
echo '<tr><td>'.$stranek.'</td><td>'.$lang['my_account_num_2'].'</td></tr>';
?>
</table>

<br />
<?php
};
include_end();
?>