<?php
	class cSettingsPage extends cAdminPage
	{
    private $menuItem;

    function __construct( $smarty, $db, $action, $post, $get  )
    {
      parent::__construct( $smarty, $db, $action, $post, $get  );
      $this->menuItem = $this->menu->GetItemByLabel("Nastavenia");
      $this->menuItem->bSelected = true;
      $this->menuItem->bExpanded = true;
    }
    		
		private function ChangePassword( &$error )
		{
      if ( empty( $this->postParams["oldP"] ) )
      {
        $this->smarty->assign( "error", "Musíte zadať staré heslo!" );        
      }          
      else if ( empty( $this->postParams["newP"] ) )
      {
        $this->smarty->assign( "error", "Musíte zadať nové heslo!" );        
      }          
      else if ( $this->postParams["newP"] != $this->postParams["retypeP"] )
      {
        $this->smarty->assign( "error", "Zle zopakované heslo!" );        
      }
      else
      { 
        $query = "SELECT password, passPrefix FROM tUser WHERE id=".$_SESSION["userID"].";";
        $result = $this->db->QueryToRow($query);
        if ( $this->db->IsError() )
        {
          $error = $this->db->GetError();
          return false;
        }
        
        if ( $result == 0 || md5($result["passPrefix"].$this->postParams["oldP"]) == $result["password"] )
        {
          $query = "UPDATE tUser SET password=MD5(CONCAT(passPrefix,'".$this->postParams["newP"]."')) ".
                   "WHERE id=".$_SESSION["userID"].";";
        
          $result = $this->db->Query($query);
          if ( $result )
          {            
            $this->smarty->assign( "actionResult", "Heslo bolo úspešne zmenené." );
            return true;
          }            
          $error = $this->db->GetError();
        }
        else
        {              
          $this->smarty->assign( "error", "Nesprávne zadané heslo!" );
        }
      }
      return false; 
		}
		
		private function ChangeImport( &$error )
		{
      if ( empty( $this->postParams["url"] ) || empty( $this->postParams["key"] ) || empty( $this->postParams["mod"] ) )
      {
        $this->smarty->assign( "error", "Musíte vyplniť všetky údaje!" );        
      }          
      else
      {
        if ( !empty($this->postParams["id"]) )
        {
          $idPrefix = $this->GenerateRandomString(6);
          $id = ", id='".md5($idPrefix.$this->postParams["id"])."', idPrefix='".$idPrefix."'";
        }
        else
          $id = "";
        $query = "UPDATE tImportData SET url='".$this->postParams["url"]."'".$id.
                 ", pubKey='".$this->postParams["key"]."', pubMod='".$this->postParams["mod"]."';";
        $result = $this->db->Query($query);
        if ( $result )
        {            
          $this->smarty->assign( "actionResult", "Údaje boli úspešne zmenené." );
          return true;
        }            
        $error = $this->db->GetError();
		  }
		  $this->smarty->assign("urlV", $this->postParams["url"]);
		  $this->smarty->assign("idV", $this->postParams["id"]);
		  $this->smarty->assign("keyV", $this->postParams["key"]);
		  $this->smarty->assign("modV", $this->postParams["mod"]);
		  return false;
		}
		
		private function GetImportData( &$error )
		{
		  $query = "SELECT * FROM tImportData";
		  $result = $this->db->QueryToRow($query);
		  $error = $this->db->GetError();
      if ( $result )
      {
        $this->smarty->assign("urlV", $result["url"]);
        $this->smarty->assign("keyV", $result["pubKey"]);
        $this->smarty->assign("modV", $result["pubMod"]);
      }       
		}
		
    function Process()
    {
      switch ( $this->action )
      {
        case "change_i":
          $subMItem = $this->menuItem->GetItemByLabel("Importovanie");
          $this->GetImportData( $this->error );
          if ( !empty( $this->error ) )
            return false;
          $this->bPageSet = true;
        break; 
        
        case "do_change_i":
          if ( !$this->ChangeImport( $this->error ) )
          {
            if ( !empty( $this->error ) )
              return false;
            $subMItem = $this->menuItem->GetItemByLabel("Importovanie");
            $this->action = "change_i";
            $this->bPageSet = true;
          }
        break;

        case "change_p":
          $subMItem = $this->menuItem->GetItemByLabel("Zmena hesla");
          $this->bPageSet = true;
        break;
      
        case "do_change_p":
          if ( !$this->ChangePassword( $this->error ) )
          {
            if ( !empty( $this->error ) )
              return false;
            $subMItem = $this->menuItem->GetItemByLabel("Zmena hesla");
            $this->action = "change_p";
            $this->bPageSet = true;
          }
        break;
      } 
      if ( $subMItem )
      {
        $this->menuItem->bSelected = false;
        $subMItem->bSelected = true;
      }
      return true;
    }
    
    function Draw()
    {
      if ( !$this->bPageSet )
      {
        $this->smarty->assign("content", "settingsMain.tpl");        
        $this->smarty->assign("title", "Nastavenia");
      }
      else
      {
        switch ( $this->action )
        {
          case "change_p":
            $this->smarty->assign("content", "changePassword.tpl");
            $this->smarty->assign("title", "Zmena hesla");
          break;
            
          case "change_i":
            $this->smarty->assign("content", "changeImport.tpl");
            $this->smarty->assign("title", "Zmena importovacích údajov");
          break;
        }
      }
      parent::Draw();
    }
	}
?>
