<?php

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

namespace Apps\Calendar\Module\Front;

use Core\Control\Button\Button;
use Core\Controller\Controller;
use Core\View\View;
use Core\View\ElementView;

use Apps\Calendar\Entity\CalendarEvent;
use Apps\Calendar\Entity\CalendarEventUser;
use Apps\Calendar\Widget\CalendarEventUserForm\CalendarEventUserForm;

/*
 * 
 */
 class FrontController 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()
   {
      $this->Core->MasterView->Set("Title", $this->Core->GetCode("Events"));
    
       $view = new View(__DIR__."/View/index.tpl", $this->Core);

       $event = new CalendarEvent($this->Core);
       $events = $event->Find("1=1 Order by DateStart Desc");
       $view->AddElement(new ElementView("Events", $events));
    
       return $view->Render();
   }

   /***
    * Detail of a event
    */
   function Event($eventCode){
      $event = new CalendarEvent($this->Core);
      $event = $event->GetByCode($eventCode);

      if($event){
        $view = new View(__DIR__."/View/event.tpl", $this->Core);
   
        $this->Core->MasterView->Set("Title", $this->Core->GetCode("Events") . " | " .$event->Name->Value);
        $this->Core->MasterView->Set("Description", $event->Name->Value);

        $view->AddElement(new ElementView("Event", $event));

        $commentPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Calendar", "Comment");

        $view->AddElement(new ElementView("hasCommentPlugin", $commentPlugin != null));
         
        $view->AddElement(new ElementView("commentPluginScript",  $commentPlugin ? $commentPlugin->GetRegisterPlugin() : ""));
        $view->AddElement(new ElementView("commentPlugin",  $commentPlugin ? $commentPlugin->Render("CalendarEvent", $event->IdEntite) : ""));

        if($event->NumberPlace->Value != "") {
            $view->AddElement(new ElementView("ShowRegistration", true));
            
            $eventUser = new CalendarEventUser($this->Core);
            $eventUser->EventId->Value =  $event->IdEntite;
            
            $eventUserForm = new CalendarEventUserForm($this->Core, $eventUser);
            $eventUserForm->Load($eventUser);
    
            $view->AddElement(new ElementView("eventUserForm", $eventUserForm->Render()));
       
        } else {
            $view->AddElement(new ElementView("ShowRegistration", false));
        } 

      } else {
        $this->Core->MasterView->Set("Title", $this->Core->GetCode("Calendar"));
    
        $view = new View(__DIR__."/View/noevent.tpl", $this->Core);
      }

      return $view->Render();
   }
          
    /*action*/
 }?>