<?php

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

namespace Apps\DownloadManager\Module\Admin;

use Core\Controller\Controller;
use Core\View\View;
use Core\View\ElementView;
use Core\Control\TabStrip\TabStrip;
use Core\Control\EntityGrid\EntityGrid;
use Core\Control\EntityGrid\EntityColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;
use Core\Control\EntityGrid\EntityIconColumn;
use Core\Control\Button\Button;
use Core\Utility\Format\Format;
use Apps\DownloadManager\Widget\DocumentForm\DocumentForm;
use Apps\DownloadManager\Entity\DmCategory;
use Apps\DownloadManager\Widget\DownloadManagerCategoryForm\DownloadManagerCategoryForm;

class AdminController extends Controller {

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

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

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

    /**
     * Page principale de l'administration
     */
    function Index() {

        $view = new View(__DIR__ . "/View/index.tpl", $this->Core);

        $tabAdmin = new TabStrip("tabAdmin", "DownloadManager");
        $tabAdmin->AddTab($this->Core->GetCode("DownloadManager.Categories"), $this->GetTabCategory());
        $tabAdmin->AddTab($this->Core->GetCode("DownloadManager.Documents"),  $this->GetTabDocument(), true);
        $tabAdmin->AddTab("Statistiques",                                      $this->GetTabStats());

        $view->AddElement(new ElementView("tabAdmin", $tabAdmin->Render()));

        return $view->Render();
    }

    /**
     * Onglet liste des documents
     */
    function GetTabDocument() {

        $grid = new EntityGrid("gdDocument", $this->Core);
        $grid->Entity = "Apps\\DownloadManager\\Entity\\DmDocument";
        
        
        $btnAdd = new Button(BUTTON, "btnAddDocument");
        $btnAdd->Value = $this->Core->GetCode("DownloadManager.AddDocument");

        $grid->AddButton($btnAdd);
        

        $grid->AddColumn(new EntityColumn("Name",        "Name"));
        $grid->AddColumn(new EntityColumn("Description", "Description"));
        $grid->AddColumn(new EntityColumn("FileType",    "FileType"));
        $grid->AddColumn(new EntityColumn("FileSize",    "FileSize"));
        $grid->AddColumn(new EntityColumn("DateCreated", "DateCreated"));
      
            
        $grid->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "DownloadManager.EditDocument", "DownloadManager.EditDocument"),
                                                  array("DeleteIcone", "DownloadManager.DeleteDocument", "DownloadManager.DeleteDocument")
                                                  )    
                          ));
  
        return $grid->Render();
    }

    /**
     * Onglet catégories
     */
    function GetTabCategory() {

        $grid = new EntityGrid("gdCategory", $this->Core);
        $grid->Entity = "Apps\\DownloadManager\\Entity\\DmCategory";

        $btnAdd = new Button(BUTTON, "btnAddCategory");
        $btnAdd->Value = $this->Core->GetCode("DownloadManager.AddCategory");

        $grid->AddButton($btnAdd);
        
        $grid->AddColumn(new EntityColumn("Name",        "Name"));
        $grid->AddColumn(new EntityColumn("Description", "Description"));

     
        $grid->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "DownloadManager.EditCategory", "DownloadManager.EditCategory"),
                                                  array("DeleteIcone", "DownloadManager.DeleteCategory", "DownloadManager.DeleteCategory")
                                                  )    
                          ));
  
        
        return $grid->Render();
    }

    function SaveCategory($data){
        
        $categoryForm = new DownloadManagerCategoryForm($this->Core, $data);

        if($categoryForm->Validate($data)){

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

            if ($data["Id"] != "") {
                $category->GetById($data["Id"]);
            } 

            $category->Code->Value = Format::ReplaceForUrl($data["Name"]);
       
            $categoryForm->Populate($category);
            $category->Save();

            return true;

        } else {
            return $categoryForm->Error();
        }
        
    }
    
    /**
     * Onglet statistiques
     */
    function GetTabStats() {
      
        $grid = new EntityGrid("gdDwonload", $this->Core);
        $grid->Entity = "Apps\\DownloadManager\\Entity\\DmDownload";
        $grid->AddOrder("Id desc");
        
        $grid->AddColumn(new EntityColumn("DocumentId",        "DocumentId"));
        $grid->AddColumn(new EntityColumn("Email", "Email"));
        $grid->AddColumn(new EntityColumn("DateDownload", "DateDownload"));
        
        return $grid->Render();
        
    }
}

?>
