<?php

    //ini_set('display_errors', 1);
    //ini_set('display_startup_errors', 1);
    //error_reporting(E_ALL);
    if ( ! defined( 'ABSPATH' ) ) exit; //It was a ajax file

    if (file_exists("./../../../../../wp-load.php")) {
        define('WP_USE_THEMES', false); // Don't load theme support functionality
        require('./../../../../../wp-load.php');
    } else {
        require('./../../../../../config/config.inc.php');
        require('./../../../../../init.php');
        require_once"./../../../digitalsignage4.php";
        DigitalSignage4_Translate::setLang(substr(get_bloginfo('language'), 0, 2));
    }

    require_once __DIR__ . "/../../Classes/DigitalSignage4_Tools.php";
    require_once __DIR__ . "/../../Kernel/API/Manager/DigitalSignage4_Exception.php";
    require_once __DIR__ . "/../../Kernel/Translations/DigitalSignage4_Translate.php";
    require_once __DIR__ . "/../../Classes/DigitalSignage4_caller.php";
    require_once __DIR__ . "/../../Kernel/API/Manager/DigitalSignage4_DemandeManager.php";
    require_once __DIR__ . "/../../controllers/override/DigitalSignage4_UpdateVideoHandlerManager.php";
	
	if (!function_exists('wp_handle_upload')) {
					require_once ABSPATH . 'wp-admin/includes/file.php';
				}
    if (isset($_FILES)) {
        if (isset($_FILES['file'])) {
			//$uploaddir= DigitalSignage4_Tools::getModuleDir().'../Files/';
            //$uploaddir=str_replace('\\', "/", $uploaddir);
			
			$upload_dir_global = wp_upload_dir();
			$uploaddir =$upload_dir_global['basedir'] . '/' . 'ds4' . '/Files';
			
			if (!file_exists($uploaddir)) {
				wp_mkdir_p($uploaddir);
			}
			$uploaddir .='/';

            $uploadfile = $uploaddir . "gabarit_customize_".(isset($_POST['id_gabarit'])?sanitize_text_field(wp_unslash($_POST['id_gabarit'])):"")."_".(isset($_POST['destination'])?sanitize_text_field(wp_unslash($_POST['destination'])):"").".png";
            
            $responseText="";
            if (isset($_FILES['file']['size'])?$_FILES['file']['size']:0 > 0) {
                $overrides = [ 'test_form' => false ];
				

                $movefile = wp_handle_upload($_FILES['file'] , $overrides );
                
                if ( $movefile && empty( $movefile['error'] ) ) {
                    $watermarkUploaded = true;
                    $file_path = $movefile['file'];
                    @unlink($uploadfile);
                    copy($file_path,$uploadfile);
                }
                else {
                    $watermarkUploaded = false;
                }

                if ($watermarkUploaded) {
                    chmod($uploadfile, 0777);
                }
                //$url_file=DigitalSignage4_Tools::getModuleUrl().'Files/'."gabarit_customize_".sanitize_text_field($_POST['id_gabarit'])."_".sanitize_text_field($_POST['destination']).".png";
				$url_file=$upload_dir_global['baseurl'] . '/' . 'ds4' . '/Files/'."gabarit_customize_".(isset($_POST['id_gabarit'])?sanitize_text_field(wp_unslash($_POST['id_gabarit'])):"")."_".(isset($_POST['destination'])?sanitize_text_field(wp_unslash($_POST['destination'])):"").".png";
                $responseText=$url_file.";".$uploadfile;
                
            }

            exit(wp_json_encode($responseText));
        }
    }

    if (isset($_POST)) {
        $datas['gabarit'] = (isset($_POST['id_gabarit'])?sanitize_text_field(wp_unslash($_POST['id_gabarit'])):"");
        $gabaritManager = new DigitalSignage4_GabaritManager();
        $gabarit = $gabaritManager->getGabarit(sanitize_text_field(wp_unslash($datas['gabarit'])));
		
		$gabaritChamps = $gabarit->getChamps();
		foreach ($gabaritChamps as $champ) {
			$champName = $champ['name'];
			$datas[$champName]=isset($_POST[$champName])?sanitize_text_field(wp_unslash($_POST[$champName])):"";
		}
		
        $gabarit->setCustom($datas);
    }
