<?php

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

namespace Apps\Calendar\Module\Admin;

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

use Apps\Calendar\Entity\CalendarEvent;
use Apps\Calendar\Widget\CalendarEventForm\CalendarEventForm;

/*
 * 
 */
 class AdminController extends Controller
 {
    /**
     * 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);
   
       $tabCalendar = new TabStrip("tabCalendar", "Calendar"); 
       $tabCalendar->AddTab($this->Core->GetCode("Calendar.Config"), $this->GetTabConfig());
       $tabCalendar->AddTab($this->Core->GetCode("Calendar.Events"), $this->GetTabEvent(), true);
      
       $view->AddElement(new ElementView("tabCalendar", $tabCalendar->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, "Calendar");
      
      $view->AddElement(new ElementView("Plugins", $plugins));
 
      $commentPlugin =  \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Calendar","Comment");
      $view->AddElement(new ElementView("PluginScript", $commentPlugin ? "<div style='display:none' id='pluginScript'>" .$commentPlugin->GetRegister() . "</div>" : ""));
    
      return $view->Render();
   }

   function GetTabEvent(){

      $gdEvent = new EntityGrid("gdEvent", $this->Core);
      $gdEvent->Entity = "Apps\Calendar\Entity\CalendarEvent";
      $gdEvent->App = "Calendar";
      $gdEvent->Action = "GetTabCategory";
      
      $gdEvent->AddOrder("Id desc");

      $btnAdd = new Button(BUTTON, "btnAddEvent");
      $btnAdd->Value = $this->Core->GetCode("Calendar.AddEvent");

      $gdEvent->AddButton($btnAdd);
      $gdEvent->AddColumn(new EntityColumn("Name", "Name"));
      $gdEvent->AddColumn(new EntityColumn("DateStart", "DateStart"));
      $gdEvent->AddColumn(new EntityColumn("HourStart", "HourStart"));
      $gdEvent->AddColumn(new EntityColumn("DateEnd", "DateEnd"));
      $gdEvent->AddColumn(new EntityColumn("HourEnd", "HourEnd"));
      $gdEvent->AddColumn(new EntityColumn("Address", "Address"));
      $gdEvent->AddColumn(new EntityColumn("NumberPlace", "NumberPlace"));
      $gdEvent->AddPluginColumn("Calendar", "Comment", "Comments", "GetComments");
   
      
      $gdEvent->AddColumn(new EntityIconColumn("Action", 
                                                array(array("EditIcone", "Calendar.EditEvent", "Calendar.EditEvent"),
                                                array("UserIcone", "Calendar.EditEventUsers", "Calendar.EditEventUsers"),
                                                array("DeleteIcone", "Calendar.DeleteEvent", "Calendar.DeleteEvent")
                                                )    
                        ));

      return $gdEvent->Render();

   }

   /***
    * Save a event
    */
   function SaveEvent($data){

      $calendarForm = new CalendarEventForm($this->Core, $data);
      if($calendarForm->Validate($data)){

         $calendarEvent = new CalendarEvent($this->Core);

         if($data["Id"]){
            $calendarEvent->GetById($data["Id"]);
         } else{
            $calendarEvent->UserId->Value = $this->Core->User->IdEntite;
         }
      
         $calendarEvent->Code->Value = Format::ReplaceForUrl($data["Name"]);
         $calendarForm->Populate($calendarEvent);

         $calendarEvent->Save();
         return true;

      } else {

         return $calendarForm->Error();
      }
   }

   /***
    * Delete a event
    */
   function DeleteEvent($eventId){
      $calendarEvent = new CalendarEvent($this->Core);
      $calendarEvent->GetById($data["Id"]);
      $calendarEvent->Delete();
      return true;
   }
          
          /*action*/
 }?>