<?php

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

namespace Apps\Courses\Module\Admin;

use Core\Control\Button\Button;
use Core\Controller\AdministratorController;
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\EntityIconColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;
use Core\Utility\Format\Format;

use Apps\Courses\Entity\CourseCour;
use Apps\Courses\Widget\CoursesCourForm\CoursesCourForm;

/*
 * 
 */
 class AdminController extends AdministratorController
 {
    /**
     * Constructeur
     */
    function _construct($core="")
    {
          $this->Core = $core;
    }

    /**
     * Create
     */
    function Create()
    {
    }

    /**
     * Init
     */
    function Init()
    {
    }

    /**
     * Render
     */
    function Show($all=true)
    {
         return $this->Index();
    }
    
    /*
    * Get the home page
    */
   function Index()
   {
       $view = new View(__DIR__."/View/index.tpl", $this->Core);

       $tabCourses = new TabStrip("tabCourses", "Courses"); 
       $tabCourses->AddTab($this->Core->GetCode("Courses.Config"), $this->GetTabConfig());
       $tabCourses->AddTab($this->Core->GetCode("Courses.Cours"), $this->GetTabCours(), true);
   
       $view->AddElement(new ElementView("tabCourses", $tabCourses->Render()));

       return $view->Render();
   }

   /***
    * Calendar Plugin Tab
    */
    function GetTabConfig(){
        $view = new View(__DIR__ . "/View/config.tpl", $this->Core);
  
        $plugins = \Apps\EeApp\Helper\AppHelper::GetPluginApp($this->Core, "Courses");
        
        $view->AddElement(new ElementView("Plugins", $plugins));
   
        $commentPlugin =  \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Courses", "Comment");
        $view->AddElement(new ElementView("PluginScript", $commentPlugin ? "<div style='display:none' id='pluginScript'>" .$commentPlugin->GetRegister() . "</div>" : ""));
      
        return $view->Render();
     }

    /***
     * Cour 
     */
    function GetTabCours(){
        $gdCour = new EntityGrid("gdCour", $this->Core);
        $gdCour->Entity = "Apps\Courses\Entity\CourseCour";
        $gdCour->App = "Courses";
        $gdCour->Action = "GetTabCour";
      
        $btnAdd = new Button(BUTTON, "btnAddCour");
        $btnAdd->Value = $this->Core->GetCode("Courses.AddCour");
  
        $gdCour->AddButton($btnAdd);
        $gdCour->AddColumn(new EntityColumn("Name", "Name"));
        $gdCour->AddColumn(new EntityColumn("ShortDescription", "ShortDescription"));
        
        $gdCour->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "Courses.EditCour", "Courses.EditCour"),
                                                  array("DeleteIcone", "Courses.DeleteCour", "Courses.DeleteCour")
                                                  )    
                          ));
  
        return $gdCour->Render();
    }
    
    /***
     * Save the cour
     */
    function SaveCour($data){

        $courForm = new CoursesCourForm($this->Core, $data);

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

            $cour = new CourseCour($this->Core);

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

            $cour->Code->Value = Format::ReplaceForUrl($data["Name"]);
       

            $courForm->Populate($cour);
            $cour->Save();

            return true;

        } else {
            return $courForm->Error();
        }
    }
    
    function DeleteCour($courId){

        $cour = new CourseCour($this->Core);
        $cour->GetById($data["Id"]);
        $cour->Delete();

        return true;
    }
          
          /*action*/
 }?>