<?php
class strankaController extends baseController {

  private $stranka;

  function __construct() {
    $this->controller_name = 'stranka';
    parent::__construct();
  }

  public function uvodni() {
    $result = dibi::query('SELECT S.ID, S.ID_SUB, S.NAZEV, S.REWRITE_LINK, S.META_TITLE, S.META_DESCRIPTION,
    	S.META_KEYWORDS, S.BODY, S.STAV, T.SLOZKA AS T_SLOZKA, TSUB.SLOZKA AS SUB_SLOZKA, 
    	SUB.ID AS SUB_ID, SUB.NAZEV AS SUB_NAZEV, SUB.REWRITE_URL AS SUB_REWRITE_URL, SUB.ROBOTS AS SUB_ROBOTS,
	SUB.SERVER_TITLE AS SUB_SERVER_TITLE, SUB.SERVER_KEYWORDS AS SUB_SERVER_KEYWORDS, SUB.SERVER_DESCRIPTION AS 	
	SUB_SERVER_DESCRIPTION
		FROM STRANKA S 
		INNER JOIN SABLONA AS T ON T.ID = S.ID_SABL 
		INNER JOIN SUBSITE AS SUB ON SUB.ID = S.ID_SUB
		INNER JOIN SABLONA AS TSUB ON TSUB.ID = SUB.ID 
		WHERE S.ID = %i', $this->registry->nastaveni['default_page']); // dopsat omezení na homepage
    $this->zobrazit($result);
  }

  public function index($args) {
    $url_rewrite = $args[0];
    $result = dibi::query('SELECT S.ID, S.ID_SUB, S.NAZEV, S.REWRITE_LINK, S.META_TITLE, S.META_DESCRIPTION,
    	S.META_KEYWORDS, S.BODY, S.STAV, T.SLOZKA AS T_SLOZKA, TSUB.SLOZKA AS SUB_SLOZKA, 
    	SUB.ID AS SUB_ID, SUB.NAZEV AS SUB_NAZEV, SUB.REWRITE_URL AS SUB_REWRITE_URL, SUB.ROBOTS AS SUB_ROBOTS,
	SUB.SERVER_TITLE AS SUB_SERVER_TITLE, SUB.SERVER_KEYWORDS AS SUB_SERVER_KEYWORDS, SUB.SERVER_DESCRIPTION AS 	
	SUB_SERVER_DESCRIPTION
		FROM STRANKA S 
		INNER JOIN SABLONA AS T ON T.ID = S.ID_SABL 
		INNER JOIN SUBSITE AS SUB ON SUB.ID = S.ID_SUB
		INNER JOIN SABLONA AS TSUB ON TSUB.ID = SUB.ID 
		WHERE `REWRITE_LINK` = %s', $url_rewrite);
    $this->zobrazit($result);
  }

  public function zobrazit($result) {
    if ( $result->count() !== 1 ) {
      $this->notFound();
    }
    else {
      $res = $result->fetchAll();
      $this->stranka = $res[0];
      switch ($this->stranka['STAV']) {
        case 2:
          $this->setPage();
          //$this->application->setTemplatePath();
          if ( $this->registry->cache_type != 2 ) {
            $this->setView();
          }

          break;
        default:
          $this->notFound();
      }
    }
  }

  public function setPage() {

    $this->registry->meta_title        = $this->stranka['META_TITLE'];
    $this->registry->meta_description  = $this->stranka['META_DESCRIPTION'];
    $this->registry->meta_keywords     = $this->stranka['META_KEYWORDS'];


    $this->subsite['id']              = $this->stranka['SUB_ID'];
    $this->subsite['nazev']           = $this->stranka['SUB_NAZEV'];
    $this->subsite['robots']          = $this->stranka['SUB_ROBOTS'];
    $this->subsite['tslozka']         = $this->stranka['SUB_SLOZKA'];
    $this->subsite['rewrite_url']     = $this->stranka['SUB_REWRITE_URL'];
    $this->subsite['server_title']    = $this->stranka['SUB_SERVER_TITLE'];
    $this->subsite['server_keywords'] = $this->stranka['SUB_SERVER_KEYWORDS'];
    $this->subsite['server_description'] = $this->stranka['SUB_SERVER_DESCRIPTION'];
    $this->subsite['loaded']       = true;

    $this->registry->subsite = $this->subsite;
    $this->registry->aktual_id     = $this->stranka['ID'];
    $this->registry->aktual_nazev  = $this->stranka['NAZEV'];
    $this->registry->aktual_link   = $this->stranka['REWRITE_LINK'];
    $this->subsite['id']           = $this->stranka['ID_SUB'];
    if (empty($this->stranka['T_SLOZKA'])) {
      $this->registry->aktual_sablona_cesta = $this->stranka['T_SLOZKA'];
    }
    else {
      $this->registry->aktual_sablona_cesta = $this->stranka['SUB_SLOZKA'];
    }
    Debug::addPoint('setPage');
  }

  public function setView() {

    $this->template->setHTML('page_html', $this->stranka['BODY']);
    $this->registry->html_view = $this->template->fetch(TEMPLATE.DIRSEP.$this->registry->aktual_sablona_cesta.DIRSEP.'page.tpl', false, false);
    $this->checkSub();
    Debug::addPoint('setView');

  }

  public function checkSub() {

    do {
      $pokracuj = 1;
      if ( eregi('<sub:([a-z0-9_]*) />', $this->registry->html_view, $regs) !== false ) {
        // zpracování subcontrolleru

        $this->registry->html_view = str_replace($regs[0], 'Nahrazeno '.$regs[1], $this->registry->html_view);
      }
      else {
        $pokracuj = 0;
      }
    }
    while($pokracuj);

    // nalezení a nahrazení subControllerů
  }

  public function notFound() {
    $err_name = $this->registry->application->loadController(ERROR_CONTROLLER);
    $err_controller = new $err_name();
    $err_action = ERROR_CONTROLLER_ACTION;
    $this->registry->toCache      = false;
    $log_id = Logger::add('Požadovaná stránka neexistuje');
    $err_controller->$err_action(array(404,$log_id));
  }



}
?>