ACC SHELL

Path : /srv/www/vhosts/czerwenka/app/presenters/
File Upload :
Current File : //srv/www/vhosts/czerwenka/app/presenters/BasePresenter.php

<?php

/**
 * Base class for all application presenters.
 *
 * @author     John Doe
 * @package    MyApplication
 */
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
    /** @persistent */
    public $lang;
    
    public $translator;
    
    protected $xmlFile;
    protected $xmlCont;
    
    protected function startup()
    {
        parent::startup();
        
        if (!isset($this->lang))
        {
            $this->lang = $this->getHttpRequest()->detectLanguage(array('cs', 'en', 'de'));
        }
        
        $this->translator = new MyTranslator();
        $this->translator->setLang($this->lang);
        $this->template->setTranslator($this->translator);
        $this->template->lang = $this->lang;
    }
    
    protected function beforeRender()
    {
        parent::beforeRender();
        
        $this->template->content = $this->loadXMLContent($this->name);
    }
    
    protected function loadXMLContent($fileName)
    {
        
        $path = WWW_DIR . "/data/pages/" . $fileName . ".xml";
        if (($file = @fopen($path, "r")))
        {
            $contents = stream_get_contents($file);
            $xml = new DOMDocument();
            $xml->loadXML($contents);
            $xml->validate(); // without validation getElementById() doesn't work

            switch ($fileName)
            {
                case 'Homepage':
                    $this->xmlFile = $xml;
                    $top = $xml->getElementById($this->lang)->getElementsByTagName('mytop')->item(0);
                    $bottom = $xml->getElementById($this->lang)->getElementsByTagName('mybottom')->item(0);

                    $data = array();
                    $data['top'] = $this->xmlFile->saveXML($top);
                    $data['bottom'] = $this->xmlFile->saveXML($bottom);
                    break;
                
                case 'OurTeam':
                    $data = new SimpleXMLElement($contents);
                    break;
                
                case 'Infoservice':
                    $this->xmlFile = $xml;
                    $top = $xml->getElementById($this->lang)->getElementsByTagName('mytop')->item(0);
                    $bottom1 = $xml->getElementById($this->lang)->getElementsByTagName('bottom1')->item(0);
                    $bottom2 = $xml->getElementById($this->lang)->getElementsByTagName('bottom2')->item(0);

                    $data = array();
                    $data['mytop'] = $this->xmlFile->saveXML($top);
                    $data['bottom1'] = $this->xmlFile->saveXML($bottom1);
                    $data['bottom2'] = $this->xmlFile->saveXML($bottom2);
                    break;
                
                default:
                    $this->xmlFile = $xml;
                    $this->xmlCont = $xml->getElementById($this->lang);

                    $data = $this->xmlFile->saveXML($this->xmlCont);
                    $data = substr($data, strlen('<lang id="xx">'));
                    $data = substr($data, 0, strlen($data) - strlen('</lang>'));
            }
            
            fclose($file);
            return $data;
        }
        
    }
    
}

ACC SHELL 2018