<?php

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

namespace Apps\DownloadManager\Module\DialogAdminDownloadManager;

use Core\Controller\Controller;
use Apps\DownloadManager\Entity\DmDocument;
use Apps\DownloadManager\Entity\DmCategory;
use Apps\DownloadManager\Widget\DownloadManagerCategoryForm\DownloadManagerCategoryForm;
use Apps\DownloadManager\Widget\DownloadManagerDocumentForm\DownloadManagerDocumentForm;
use Apps\DownloadManager\Widget\DownloadManagerEmailDownloadForm\DownloadManagerEmailDownloadForm;

class DialogAdminDownloadManagerController extends Controller {

    /**
     * Ajout ou édition d'un document
     */
    function AddDocument($documentId) {

        $document = new DmDocument($this->Core);

        if ($documentId != "") {
            $document->GetById($documentId);
        }

        $data       = ["Id" => $documentId];
        $documentForm = new DownloadManagerDocumentForm($this->Core, $data);
        $documentForm->Load($document);
        $documentForm->form->LoadImage($document);
        $documentForm->form->LoadDocument($document);

        return $documentForm->Render();
    }

    /**
     * Ajout ou édition d'une catégorie
     */
    function AddCategory($categoryId) {

        $category = new DmCategory($this->Core);

        if ($categoryId != "") {
            $category->GetById($categoryId);
        }

        $data       = ["Id" => $documentId];
        $categoryForm = new DownloadManagerCategoryForm($this->Core, $data);
        $categoryForm->Load($category);
     
        return $categoryForm->Render();
    }
    
    function DownLoadDocument($documentId){
        
            $widget = new DownloadManagerEmailDownloadForm($this->Core, base64_encode($documentId));
                $widget->Load(["Token" => base64_encode($documentId)]);
                return $widget->Render();
    }
}

?>
