FaZeBookSocialNetwork / www / src / Application / Application.php
Application.php
Raw
<?php
namespace Application;

require_once(__DIR__.'/Page/PageInterface.php');
require_once(__DIR__.'/Renderer.php');
require_once(__DIR__.'/NavigationItem.php');

use Application\Page\PageInterface;
use FaZeBook\Page\ProtectedPage;

/**
 * Objektmodell der Webanwendung. Verwaltet die Seiten und die Navigation.
 * @package Application
 */
class Application{

    const PAGE_PARAMETER = 'page';

    /** @var Renderer  */
    protected $renderer;
	/** @var PageInterface[] */
    protected $pages;
	/** @var NavigationItem[] */
    protected $navigation;

    /**
     * @param PageInterface[] $pages
     * @param NavigationItem[] $navigation
     */
    public function __construct($pages = [], $navigation = [])
    {
        $this->renderer = new Renderer();
        $this->pages = $pages;
        $this->navigation = $navigation;
    }

    /**
     * Fügt neue Seite mit zugehöriger Id hinzu. Existert die Id bereits, wird die gespeicherte Seite überschrieben
     * @param PageInterface $page Seitenklasse
     */
    public function addPage(PageInterface $page)
    {
        if (!$page instanceof ProtectedPage || $page->hasPermission()) {
            $this->pages[$page->getId()] = $page;
        }
    }

    /**
     * Fügt einen Navigationspunkt am Ende hinzu
     * @param NavigationItem $item
     */
    public function addNavigationItem(NavigationItem $item)
    {
        $this->navigation[] = $item;
    }

    public function generateNavigationItems() {
        foreach ($this->pages as $page) {
            if ($page->hasNavigationItem()) {
                $this->addNavigationItem(new NavigationItem($page->getTitle(), $page));
            }
        }
    }

    /**
     * Führt das Programm aus
     */
    public function run()
    {
        // Standardseite
        $pageId = array_keys($this->pages)[0];
        if(isset($_GET[self::PAGE_PARAMETER]) && !empty($this->pages[$_GET[self::PAGE_PARAMETER]])){
            // Wenn GET Parameter angegeben, nutze diesen
            $pageId = $_GET[self::PAGE_PARAMETER];
        }
        if(isset($this->pages[$pageId])){
            $page = $this->pages[$pageId];
        }
        $variables = array(
            'pageTitle' => $page->getTitle(),
            'pageContent' => $this->renderer->render($page),
            'activePageId' => $pageId,
            'navigation' => $this->navigation
        );
        $this->renderer->showViewScript($this->getLayoutViewScript(), $variables);
    }
    
    /**
     * Gibt das ViewScript für das layout zurück
     */
    public function getLayoutViewScript()
    {
    	return __DIR__.'/../../view/layout.phtml';
    }
}