<?php

if ( ! defined( 'ABSPATH' ) ) exit;

require_once "DigitalSignage4_Entity.php";

class DigitalSignage4_GabaritEntity extends DigitalSignage4_Entity
{
    private $id = null;
    private $nom = null;
    private $lien_video_exemple = null;
    private $champs = null;
    private $AE_version = null;
    private $actif = null;
    private $isUsable = null;
    private $textAdd = null;
    private $orientation = 1;
    private $automatic = 0;
    private $champs_custom = null;
	private $iamodelequestion = null;


    /**
     * DigitalSignage4_GabaritEntity constructor.
     */
    public function __construct()
    {
    }

    /**
     * @return null
     */
    public function getAutomatic()
    {
        return $this->automatic;
    }

    /**
     * @param null $isUsable
     */
    public function setAutomatic($automatic)
    {
        $this->automatic = $automatic;
    }

    /**
     * @return null
     */
    public function getIsUsable()
    {
        return $this->isUsable;
    }

    /**
     * @param null $isUsable
     */
    public function setIsUsable($isUsable)
    {
        $this->isUsable = $isUsable;
    }

    public function setCustom($champs_custom)
    {
        $this->champs_custom = $champs_custom;
        global $wpdb;
        $sql ="SELECT * FROM ".$wpdb->prefix ."digitalsignage_gabarit WHERE id_gabarit = %d LIMIT 1;";
        $sql_secu = $wpdb->prepare($sql, $this->id);
        $resultat = $wpdb->get_row($sql_secu);
        if ($resultat!=null) {
            $sql = "DELETE FROM ".$wpdb->prefix ."digitalsignage_gabarit WHERE id_gabarit= %d LIMIT 1;";
			$sql_secu = $wpdb->prepare($sql, $this->id);
            $wpdb->query($sql_secu);
        }
        $wpdb->insert($wpdb->prefix ."digitalsignage_gabarit", array("parameters" => wp_json_encode($this->champs_custom), "id_gabarit" => intval($this->id)));
    }

    public function getCustom()
    {
        //creer la table des gabarits si elle n'existe pas
        $this->champs_custom=array();
        global $wpdb;
       

        //chercher si il y a une personnalisation
        $sql ="SELECT * FROM ".$wpdb->prefix ."digitalsignage_gabarit WHERE id_gabarit = %d LIMIT 1;";
        $sql_secu = $wpdb->prepare($sql, $this->id);
        $resultat = $wpdb->get_row($sql_secu);
        if ($resultat!=null) {
            $this->champs_custom =json_decode($resultat->parameters, true);
        }
        return $this->champs_custom;
    }


    public function initCustomChamp()
    {
        $customisation = $this->getCustom();
        if (count($customisation)>0) {
            $champs_retour =array();
            $champs_gabarit = $this->getChamps();
			
            foreach ($champs_gabarit as $champs_) {
                if (array_key_exists($champs_['name'], $customisation)) {
                    $champs_['prestahsop_champ_name']=$customisation[$champs_['name']];
                    $champs_['wordpress_champ_name']=$customisation[$champs_['name']];
                    $champs_['default']=$customisation[$champs_['name']];
					
					if (array_key_exists($champs_['name'].'_ia_question', $customisation)) {
						$champs_['ia_question']=$customisation[$champs_['name'].'_ia_question'];
					}
					$champs_retour[]=$champs_;
                } else {
					
						$champs_retour[]=$champs_;
					
                }
				
            }
			
			
			
            $this->setChamps($champs_retour);
        }
        return $this->champs;
    }


    /**
     * @return null
     */
    public function getTextAdd()
    {
        return $this->textAdd;
    }

    /**
     * @param null $textAdd
     */
    public function setTextAdd($textAdd)
    {
        $this->textAdd = $textAdd;
    }
	
	
	    /**
     * @return null
     */
    public function getIamodelequestion()
    {
        return $this->iamodelequestion;
    }

    /**
     * @param null $ia_modele_question
     */
    public function setIamodelequestion($iamodelequestion)
    {
        $this->iamodelequestion = $iamodelequestion;
    }
	

    /**
     * @return null
     */
    public function getActif()
    {
        return $this->actif;
    }

    /**
     * @param null $actif
     */
    public function setActif($actif)
    {
        $this->actif = $actif;
    }

    /**
     * @return null
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param null $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return null
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * @param null $nom
     */
    public function setNom($nom)
    {
        $this->nom = $nom;
    }

    /**
     * @return null
     */
    public function getLienVideoExemple()
    {
        return $this->lien_video_exemple;
    }

    /**
     * @param null $lien_video_exemple
     */
    public function setLienVideoExemple($lien_video_exemple)
    {
        $this->lien_video_exemple = $lien_video_exemple;
    }

    /**
     * @return null
     */
    public function getChamps()
    {
        return $this->champs;
    }

    /**
     * @param null $champs
     */
    public function setChamps($champs)
    {
        $this->champs = $champs;
    }

    /**
     * @return null
     */
    public function getAEVersion()
    {
        return $this->AE_version;
    }

    /**
     * @param null $AE_version
     */
    public function setAEVersion($AE_version)
    {
        $this->AE_version = $AE_version;
    }
    /**
     * @return null
     */
    public function getOrientation()
    {
        return $this->orientation;
    }

    /**
     * @param null $AE_version
     */
    public function setOrientation($orientation)
    {
        $this->orientation = $orientation;
    }
}
