<?php

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

namespace Apps\Calendar\Entity;

use Core\Entity\Entity\Entity;
use Core\Entity\Entity\Property;
use Core\Entity\Entity\EntityProperty;
use Core\Utility\Format\Format;

use Apps\Calendar\Entity\CalendarEventUser;

class CalendarEvent extends Entity {

    //Constructeur
    function __construct($core) {
        //Version
        $this->Version = "2.0.0.0";

        //Nom de la table 
        $this->Core = $core;
        $this->TableName = "CalendarEvent";
        $this->Alias = "CalendarEvent";

        $this->Name = new Property("Name", "Name", TEXTBOX, false, $this->Alias);
        $this->Code = new Property("Code", "Code", TEXTBOX, false, $this->Alias);
        $this->Description = new Property("Description", "Description", TEXTAREA, false, $this->Alias);
        $this->DateStart = new Property("DateStart", "DateStart", DATEBOX, true, $this->Alias);
        $this->HourStart = new Property("HourStart", "HourStart", DATETIMEBOX, false, $this->Alias);
        $this->DateEnd = new Property("DateEnd", "DateEnd", DATEBOX, false, $this->Alias);
        $this->HourEnd = new Property("HourEnd", "HourEnd", DATETIMEBOX, false, $this->Alias);

        $this->Address = new Property("Address", "Address", TEXTAREA, false, $this->Alias);
        $this->NumberPlace = new Property("NumberPlace", "NumberPlace", TEXTAREA, false, $this->Alias);

        $this->UserId = new Property("UserId", "UserId", NUMERICBOX, false, $this->Alias);

        //Partage entre application 
        $this->AddSharedProperty();

        //Creation de l entité 
        $this->Create();
    }

    /*
     * Get A small Description
     */

    function GetSmallDescription() {
        return Format::Tronquer($this->Description->Value, 200);
    }
    
    /****
     * Vérify disponibility
     */
    function IsDispo() {
        return (count($this->GetUsers()) < $this->NumberPlace->Value);
    }
    
    /**
     * Get thje uiser
     * @return type
     */
    function GetUsers(){
       $eventUser = new CalendarEventUser($this->Core);
       
       return $eventUser->Find("EventId=" . $this->IdEntite);
    }
    
    /***
     * Vérify if user exist
     */
    function EmailExist($email){
        $eventUser = new CalendarEventUser($this->Core);
        $event =  $eventUser->Find("Email = '" .$email. "' AND EventId = " . $this->IdEntite); 
        
        return (count($event) > 0);
    }
    
    /***
     * Get number place icon
     */
    function GetPlacesIcon(){
        
        if($this->NumberPlace->Value != ""){
            return count($this->GetUsers()) ."/".$this->NumberPlace->Value . "<i class='fa fa-users showUserCalendar' id=".$this->IdEntite."></i>";
        }
    }

    /***
     * Get The Comment
     */
    function GetComments() {
        
        $commentPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Calendar", "Comment");
  
          if($commentPlugin != null ){
  
                $comments = $commentPlugin->GetByEntity($this->Core, "CalendarEvent", $this->IdEntite);
  
                 if (count($comments) > 0) {
  
                $view = "<ul style='text-align:left;'>";
  
                foreach ($comments as $comment) {
                    $view .= "<li><b>" . $comment->DateCreated->Value . "</b> .  " . $comment->UserName->Value  . "<span>" .$comment->Message->Value . "</span>";
                    $view .= "<div class='tool'><i id='$comment->IdEntite' class='publishComment fa " .( $comment->Status->Value == "1" ? "fa-eye" : "fa-eye-slash published"  ) . "'></i>";
                    $view .= "<i id='$comment->IdEntite' class='fa fa-trash removeComment'></i>";
                    $view .= "</div>";   
                    $view .= "</span></li>";
                }
  
                $view .= "</ul>";
            }
  
            return $view;
        }
      }
}

?>