<?php

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

class DigitalSignage4_FormBuilder
{
    private $class = "";
    private $champs = array();
    private $validLabel = "";
    private $name = "";
    private $datas = array();
    private $extraLink = array();
    private $extraText = array();
    private $html = "";

    /**
     * DigitalSignage4_FormBuilder constructor.
     */
    public function __construct($name = "", $validLabel = "", $datas = array())
    {
        $this->setName($name);
        $this->setClass($name);
        $this->setValidLabel($validLabel);
        $this->setDatas($datas);
    }


    public function generateField()
    {
        return new DigitalSignageFormField();
    }

    /**
     * @param string $class
     * @return DigitalSignage4_FormBuilder
     */
    public function setClass($class)
    {
        $this->class = $class;
        return $this;
    }

    /**
     * @param string $validLabel
     * @return DigitalSignage4_FormBuilder
     */
    public function setValidLabel($validLabel)
    {
        $this->validLabel = $validLabel;
        return $this;
    }

    /**
     * @param $champ DigitalSignageFormField
     * @return bool
     */
    public function addChamp($champ)
    {
        if (!$champ instanceof DigitalSignageFormField) {
            return false;
        }

        array_push($this->champs, $champ);
        return true;
    }

    /**
     * @return string
     */
    public function getClass()
    {
        return $this->class;
    }

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

    /**
     * @return string
     */
    public function getValidLabel()
    {
        return $this->validLabel;
    }

    public function setName($string)
    {
        $this->name = $string;
    }

    public function setHtml($html)
    {
        $this->html = $html;
    }

    public function getHtml()
    {
        return $this->html;
    }

    public function getName()
    {
        return $this->name;
    }

    public function isSubmit()
    {
        //check_admin_referer( 'ds4_form_nonce' );
        if (isset($_POST)) {
            if (array_key_exists($this->name, $_POST)) {
                $this->datas = array();
                
                foreach($_POST as $itKey => $itValue)
                {
                    $this->datas[$itKey] = sanitize_text_field(wp_unslash($_POST[$itKey]));
                }

                foreach ($this->getChamps() as $champ) {
                    $nameChamp = $champ->getName();
                    unset($_POST[$nameChamp]);
                }

                unset($_POST[$this->name]);

                return true;
            }
        }

        return false;
    }

    public function getDatas()
    {
        return $this->datas;
    }

    private function setDatas($datas)
    {
        $this->datas = $datas;
    }

    public function addExtraText($text)
    {
        $this->extraText[] = $text;
    }

    public function getExtraText()
    {
        return $this->extraText;
    }

    public function addExtraLink($label, $url, $extraId = "", $fullUrl = true, $blank = true)
    {
        //remplacement de l'url pour un url vide
        if (empty($url)) {
            if (empty($extraId)) {
                $url = '#';
            }
        } else {
            if (strpos($url, 'http') === false && $fullUrl) {
                $tmpProtocol = isset($_SERVER["SERVER_PROTOCOL"])?sanitize_text_field(wp_unslash($_SERVER["SERVER_PROTOCOL"])):"";
                $protocol = strtolower(substr($tmpProtocol, 0, strpos($tmpProtocol, '/'))) . '://';
                $url = $protocol . $url;
            }
        }

        //ajout d'un special id pour recuperer une action local
        if (!empty($extraId)) {
            if (!strpos($url, $extraId . "=true") !== false) {
                //si l'url a deja des données en GET
                if (strpos($url, 'index.php?') !== false || strpos($url, 'admin.php?page') !== false) {
                    $url = $url . "&" . $extraId . "=true";
                } else {  //si l'url n'a pas deja de données en GET
                    $url = $url . "?" . $extraId . "=true";
                }
            }
        }

        $this->extraLink[] = array(
            'label' => $label,
            'url'   => $url,
            'blank' => $blank
        );
    }

    public function getExtraLink()
    {
        return $this->extraLink;
    }

    public function submitExtraLink($string)
    {
        return isset($_GET[$string]);
    }
}

class DigitalSignageFormField
{
    private $name = "";
    private $id = "";
    private $value = "";
    private $type = "";
    private $label = "";
    private $checked = false;

    /**
     * @return bool
     */
    public function getChecked()
    {
        return $this->checked;
    }

    /**
     * @param bool $checked
     * @return DigitalSignageFormField
     */
    public function setChecked($checked)
    {
        $this->checked = $checked;
        return $this;
    }

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


    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return DigitalSignageFormField
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

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

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

    /**
     * @return string
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param string $value
     * @return DigitalSignageFormField
     */
    public function setValue($value)
    {
        $this->value = $value;
        return $this;
    }

    /**
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param string $type
     * @return DigitalSignageFormField
     */
    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return $this->label;
    }

    /**
     * @param string $label
     * @return DigitalSignageFormField
     */
    public function setLabel($label)
    {
        $this->label = $label;
        return $this;
    }
}
