<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Apps\DownloadManager\Module\Front;

use Core\Controller\Controller;
use Core\View\View;
use Core\View\ElementView;
use Apps\DownloadManager\Helper\DocumentHelper;
use Apps\DownloadManager\Entity\DmCategory;

class FrontController extends Controller {

    function _construct($core = "") {
        $this->Core = $core;
    }

    function Create() {}
    function Init()   {}

    function Show($all = true) {
        return $this->Index();
    }

    /**
     * Page d'accueil : liste de tous les documents
     */
    function Index() {
        $view = new View(__DIR__ . "/View/index.tpl", $this->Core);

        $documents = DocumentHelper::GetAll($this->Core);
        $view->AddElement(new ElementView("Documents", $documents));

        $category = new DmCategory($this->Core);
        $view->AddElement(new ElementView("Categories", $category->Find("1=1")));

        return $view->Render();
    }

    /**
     * Page de détail d'un document
     */
    function Detail($documentCode) {
        $view = new View(__DIR__ . "/View/detail.tpl", $this->Core);

        $document = new \Apps\DownloadManager\Entity\DmDocument($this->Core);
        $results  = $document->Find("Code='" . $documentCode . "' AND Status=1");

        if (count($results) > 0) {
            $doc = $results[0];
            $view->AddElement(new ElementView("Document", $doc));
            $view->AddElement(new ElementView("Token", base64_encode($doc->IdEntite)));
        }

        return $view->Render();
    }
}

?>
