<?php 















function Droit($name){
	if (!Droit::$data[$name]) {
		if (user::isAdmin()) {
			return new DroitInstance($name,$name);
		}else{
			return new DroitInstance($name,$name);
		}
	}
	return Droit::$data[$name];
}
class Droit{
	static $data = [];
	static function add($name,$titre,$options = [],$default = null){
		return new DroitInstance($name,$titre,$options,$default);;
	}
	static $group = [];
	static function addGroup($name,$titre,$fn = null){
		$Groupdroit = new DroitGroupInstance($name,$titre);
		if (is_callable($fn)) {
			$fn($Groupdroit);
		}
		return $Groupdroit;
	}
	static function json($iduser = null){
		$json = [];
		foreach (self::$group as $gr) {
			$json[] = $gr->json($iduser);
		}
		foreach (self::$data as $dr) {
			if(!$dr->parent){
				$json[] = $dr->json($iduser);
			}
		}
		return json_encode($json);
	}
	static function get($name){
		return self::$data[$name];
	}
	static function setAll($iduser,$data){
		foreach ($data as $key => $value) {
			if ($value["data"]["type"] == "droit") {
				self::get($value["data"]["name"])->set($value["data"]["value"],$iduser);
			}elseif(!empty($value["children"])){
				self::setAll($iduser,$value["children"]);
			}
		}
	}
}

class DroitGroupInstance{
	var $name = "";
	var $titre = "";
	var $parent = null;
	var $listChild = [];
	var $childGroup = [];
	function __construct($name,$titre){
		$this->name = $name;
		$this->titre = $titre;
		Droit::$group[$name] = $this;
	}
	function add($name,$titre,$options = [],$default = null){
		$droit = new DroitInstance($name,$titre,$options,$default);
		$droit->parent = $this;
		$this->listChild[$name] = $droit;
		return $droit;
	}
	function addGroup($name,$titre,$fn = null){
		$Groupdroit = new DroitGroupInstance($name,$titre);
		$Groupdroit->parent = $this;
		$this->childGroup[$name] = $Groupdroit;
		if (is_callable($fn)) {
			$fn($Groupdroit);
		}
		return $Groupdroit;
	}
	function child($fn){
		if (is_callable($fn)) {
			$fn($this);
		}
		return $this;
	}
	function json($iduser){
		$json = [
			"text"=>$this->titre,
			"data"=>["type"=>"group","name"=>$this->name,"titre"=>$this->titre],
			"children"=>[],
		];
		foreach ($this->childGroup as $gr) {
			$json["children"][] = $gr->json($iduser);
		}
		foreach ($this->listChild as $dr) {
			$json["children"][] = $dr->json($iduser);
		}
		return $json;
	}
}

class DroitInstance{
	var $name = "";
	var $titre = "";
	var $options = [];
	var $default = null;
	var $parent = null;
	function __construct($name,$titre,$options = [],$default = null){
		$this->name = $name;
		$this->titre = $titre;
		$this->options = $options;
		$this->default = $default;
		if (empty($this->options)) {
			$this->options = ["Oui","Non","Demande"];
		}
		Droit::$data[$name] = $this;
	}
	function set($value,$iduser = null){
		user::set("Droit:$this->name",$value,$iduser);
		return $this;		
	}
	function yes($iduser = null){
		if (user::isAdmin($iduser)) {
			return true;
		}
		//$value = user::get("Droit:$this->name",null,$iduser);
		$value=null;
		if (is_null($value)) {
			$value = $this->default;
		}
		if (is_numeric($value)) {
			return floatval($value);
		}elseif(is_string($value)){
			switch (strtolower(trim($value))) {
				case 'oui':
				case 'true':
				case 'yes':
				case 'demande':
					return true;
				break;
				case 'non':
				case 'false':
				case 'no':
					return false;
				break;
				default:
					return $value;
				break;
			}
		}
		return $value;
	}
	function ok($iduser = null){
		return $this->yes($iduser);
	}
	function bool($iduser = null){
		return $this->yes($iduser);
	}
	function access($iduser = null){
		return $this->yes($iduser);
	}
	function value($iduser = null){
		//$value = user::get("Droit:$this->name",null,$iduser);
		$value=null;
		if (is_null($value)) {
			return $this->default;
		}else{
			return $value;
		}
	}
	function val($iduser = null){
		return $this->value($iduser);
	}
	function demande($iduser = null){
		return strtolower(trim($this->value($iduser))) == "demande";
	}
	function json($iduser){
		$json = [
			"text"=>$this->titre,
			"data"=>["type"=>"droit","name"=>$this->name,"titre"=>$this->titre,"options"=>$this->options,"value"=>$this->value($iduser)],
			"icon" => "glyphicon glyphicon-ok",
			"children"=>[],
		];
		return $json;
	}
}