<?php

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

namespace Apps\Lang\Helper;

use Core\Entity\Entity\Argument;
use Core\Entity\Langs\Langs;
use Core\Entity\Langs\LangsCode;
use Core\Entity\Langs\LangsElement;
use Core\Entity\Langs\LangsEntityElement;


class ElementHelper
{
    /**
     * Supprime un element
     * @param type $core
     * @param type $elementId
     */
    public static function RemoveElement($core, $elementId)
    {
        //Suppression des enfants
        $request = "DELETE FROM ee_lang_element WHERE CodeId = " . $elementId;
        $core->Db->Execute($request);
 
         $code = new LangsCode($core);
         $code->GetById($elementId);
         
         $code->Delete();
    }
    
    /*
     * Met a jour un element dans la langue courante
     */
    public static function UpdateElement($core, $idElement, $value)
    {
        //Recuperation de l'identifiant de la langue
        $Lang = new Langs($core);
        $Lang->AddArgument(new Argument("Core\Entity\Langs\Langs","Code",EQUAL, $core->GetLang()));
        $Langs=$Lang->GetByArg();
    
        $langElement = new LangsElement($core);
        $elements= $langElement->Find(" CodeId=" .$idElement ." AND LangId = " .$Langs[0]->IdEntite );
        
        if(count($elements) > 0){
           $element = $elements[0];
           $element->Libelle->Value = $value;
           $element->Save();
           
           //On suppprimmes les doublons
           $request = "Delete FROM ee_lang_element where CodeId = " .$idElement . ' And Id != ' .$element->IdEntite;
           $core->Db->Execute($request);
        }
        else{
            //Update ou insert l'element
            $element = new LangsElement($core);
            $element->CodeId->Value = $idElement;
            $element->LangId->Value = $Langs[0]->IdEntite;
            $element->Libelle->Value = $value;

            $element->Save();
        }
    }
    
    /***
     * Translate an element
     */
    static function TranslateElement($core, $data){

        $entityElement = new LangsEntityElement($core);

        $existElement = $entityElement->Find("EntityName ='". $data["EntityName"] . "' and EntityId = " . $data["EntityId"] . " AND LangId = " .$data["LangId"]);

        if(count($existElement) > 0){

            $existElement = $existElement[0];
            $existElement->Title->Value = $data["Title"];
            $existElement->Description->Value = $data["Description"];
            $existElement->Content->Value = $data["Content"];
            
            $existElement->Save();
        } else {

            $entityElement = new LangsEntityElement($core);
            $entityElement->LangId->Value = $data["LangId"];
            $entityElement->EntityId->Value = $data["EntityId"];
            $entityElement->EntityName->Value = $data["EntityName"];
            $entityElement->Title->Value = $data["Title"];
            $entityElement->Description->Value = $data["Description"];
            $entityElement->Content->Value = $data["Content"];
         
            $entityElement->Save();
        }

        return true;
    }

    static function GetEntityLangElement($core, $entityName, $entityId, $langId){

        $entityElement = new LangsEntityElement($core);

        $existElement = $entityElement->Find("EntityName ='". $entityName . "' and EntityId = " . $entityId . " AND LangId = " .$langId);

        if(count($existElement) > 0){
            return $existElement[0];
        }

        return null;
    }
}

