<?php 
function env($name,$default = null){
	if(!file_exists(dirname(__DIR__)."/.env")) return $default;
	$name = strtolower(trim($name));
	$handle = fopen(dirname(__DIR__)."/.env", "r");
	if ($handle) {
		while (($line = fgets($handle)) !== false) {
			list($before, $after) = explode(':', $line, 2);
			if (strtolower(trim($before)) == $name) {
				fclose($handle);
				switch (strtolower(trim($after))) {
					case 'true':
						return true;
					break;
					case 'false':
						return false;
					break;
					default:
						if (is_numeric(trim($after))) {
							return floatval(trim($after));
						}
						return trim($after);
					break;
				}
			}
		}
		fclose($handle);
	} 
	return $default;
}
function setEnv($name,$value){

}

class api{
	static function __callStatic($name,$args){
		app::api($name,$args[0]);
	}
}


function ce($name){
	return new CustomElement($name);
}
class CustomElement{
	var $name = "";
	function __construct($name){
		$this->name = $name;
	}
	var $tpl = "";
	function tpl($fn){
		ob_start();
			$fn();
		$this->tpl=ob_get_clean();
		return $this;
	}
	var $js = "";
	function js($fn){
		ob_start();
		$fn();
		$this->js = "\n//".str_repeat("-", 100)."\n";
		$this->js .="//      CustomElement:$this->name ";
		$this->js .= "\n//".str_repeat("-", 100)."\n";
		$this->js .= str_replace('{{inject-tpl}}', json_encode(trim($this->tpl)), Route::rmvTag(ob_get_clean()));
		echo "\n $this->js";
		return $this;
	}
	function body($fn){
		return $this->js($fn);
	}
}



function mkpath($path) {
  if(@mkdir($path) or file_exists($path)) return true;
  return (mkpath(dirname($path)) and mkdir($path));
}


function clog($data){
	app::apiRep("log",$data);
}


function param($name,$default = null){
	$tables = explode(",","RESEAU_SOCIAL,CONFIG_SITE,EDITORIAL");
	$sql = [];
	foreach ($tables as $table) {
		$sql[] = "SELECT name,titre,data FROM $table";
	}
	$sql = join(" union ",$sql);
	$sql ="SELECT * FROM ($sql) t where name='$name'";
	$list = db::get()->table($sql);
	if (count($list) >= 1) {
		return $list[0]["data"];
	}
	return $default;
}




function chifre_en_lettre($montant) {
	return asLetters(number_format(floatval($montant),3,'.','')."");
}


function asLetters($number,$dev="") {
	if(!empty($dev)) $dev = " $dev";
	$separateur = ".";
    $convert = explode($separateur, $number);
    $num[17] = array('zero', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit',
                     'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize');
                      
    $num[100] = array(20 => 'vingt', 30 => 'trente', 40 => 'quarante', 50 => 'cinquante',
                      60 => 'soixante', 70 => 'soixante-dix', 80 => 'quatre-vingt', 90 => 'quatre-vingt-dix');
                                      
    if (isset($convert[1]) && $convert[1] != '') {
      return asLetters($convert[0]*1,"Dinars").' et '.asLetters($convert[1]*1,"Millimes");
    }

    if ($number < 0) return 'moins '.asLetters(-$number).$dev;
    if ($number < 17) {
      return $num[17][$number].$dev;
    }
    elseif ($number < 20) {
      return 'dix-'.asLetters($number-10).$dev;
    }
    elseif ($number < 100) {
      if ($number%10 == 0) {
        return $num[100][$number].$dev;
      }
      elseif (substr($number, -1) == 1) {
        if( ((int)($number/10)*10)<70 ){
          return asLetters((int)($number/10)*10).'-et-un'.$dev;
        }
        elseif ($number == 71) {
          return 'soixante-et-onze'.$dev;
        }
        elseif ($number == 81) {
          return 'quatre-vingt-un'.$dev;
        }
        elseif ($number == 91) {
          return 'quatre-vingt-onze'.$dev;
        }
      }
      elseif ($number < 70) {
        return asLetters($number-$number%10).'-'.asLetters($number%10).$dev;
      }
      elseif ($number < 80) {
        return asLetters(60).'-'.asLetters($number%20).$dev;
      }
      else {
        return asLetters(80).'-'.asLetters($number%20).$dev;
      }
    }
    elseif ($number == 100) {
      return 'cent'.$dev;
    }
    elseif ($number < 200) {
      return asLetters(100).' '.asLetters($number%100).$dev;
    }
    elseif ($number < 1000) {
      return asLetters((int)($number/100)).' '.asLetters(100).($number%100 > 0 ? ' '.asLetters($number%100): '').$dev;
    }
    elseif ($number == 1000){
      return 'mille'.$dev;
    }
    elseif ($number < 2000) {
      return asLetters(1000).' '.asLetters($number%1000).$dev;
    }
    elseif ($number < 1000000) {
      return asLetters((int)($number/1000)).' '.asLetters(1000).($number%1000 > 0 ? ' '.asLetters($number%1000): '').$dev;
    }
    elseif ($number == 1000000) {
      return 'millions'.$dev;
    }
    elseif ($number < 2000000) {
      return asLetters(1000000).' '.asLetters($number%1000000).$dev;
    }
    elseif ($number < 1000000000) {
      return asLetters((int)($number/1000000)).' '.asLetters(1000000).($number%1000000 > 0 ? ' '.asLetters($number%1000000): '').$dev;
    }
  }