<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


class CoreUsers{
	public $resId=NULL;				
	public $old_recordset=NULL;		
	public $recordset=NULL;			
	public $mail=NULL;				
	public $prefs=NULL;				
	
	
	private function addPermission($r){
		$sql="select * from ".SYS_RESOURCES_TABLE." where idResource=(select parent from ".SYS_RESOURCES_TABLE." where idResource='{$r['idResource']}')";
		$q=mysql_query($sql);
		if(mysql_num_rows($q)>0){			
			$f=mysql_fetch_assoc($q);
			$idUser0=$r['idUser']==NULL ? "idUser IS NULL" : "idUser=".$r['idUser'];
			$idGroup0=$r['idGroup']==NULL ? "idGroup IS NULL" : "idGroup=".$r['idGroup'];
			$idUser=$r['idUser']==NULL ? "NULL" : $r['idUser'];
			$idGroup=$r['idGroup']==NULL ? "NULL" : $r['idGroup'];
			$sql="SELECT * FROM ".SYS_PERMISSIONS_TABLE." WHERE {$idUser0} AND {$idGroup0} AND idResource='{$f['idResource']}'";
			$qq=mysql_query($sql);
			if(mysql_num_rows($qq)>0){		
				while($ff=mysql_fetch_assoc($qq)){
					mysql_query("UPDATE ".SYS_PERMISSIONS_TABLE." SET v=1 WHERE id={$ff['id']}");
					$this->addPermission($ff);
				}
			}else{							
				$sql="INSERT INTO ".SYS_PERMISSIONS_TABLE."(idGroup, idUser, idResource, v) VALUES ({$idGroup}, {$idUser}, '{$f['idResource']}', 1)";
				mysql_query($sql);
				$sql="SELECT * FROM ".SYS_PERMISSIONS_TABLE." WHERE id=".mysql_insert_id();
				$last=mysql_fetch_assoc(mysql_query($sql));
				$this->addPermission($last);
			}
		}
	}
	
	private function delPermission($r){
		$idUser=$r['idUser']==NULL ? "idUser IS NULL" : "idUser=".$r['idUser'];
		$idGroup=$r['idGroup']==NULL ? "idGroup IS NULL" : "idGroup=".$r['idGroup'];
		$sql="SELECT * FROM ".SYS_RESOURCES_TABLE." WHERE parent='{$r['idResource']}'";
		$q=mysql_query($sql);
		if(mysql_num_rows($q)>0){
			while($f=mysql_fetch_assoc($q)){
				$sql="SELECT * FROM ".SYS_PERMISSIONS_TABLE." WHERE idResource='{$f['idResource']}' AND $idUser AND $idGroup";
				$qq=mysql_query($sql);
				if(mysql_num_rows($qq)>0){
					$ff=mysql_fetch_assoc($qq);
					$this->delPermission($ff);
					mysql_query("DELETE FROM ".SYS_PERMISSIONS_TABLE." WHERE id={$ff['id']}");
				}
			}
		}
	}
	
	public function permissionAdd(){
		$f=$this->recordset;
		$this->addPermission($f);
	}
	
	public function permissionEdit(){
		$f=$this->recordset;
		$o=$this->old_recordset;
		if(($f['idResource']!=$o['idResource']) || ($f['idGroup']!=$o['idGroup']) || ($f['idUser']!=$o['idUser'])){
			$this->delPermission($o);
			$this->addPermission($f);
		}else{
			$v=$f['v']==NULL ? "NULL" : $f['v'];
			$i=$f['i']==NULL ? "NULL" : $f['i'];
			$e=$f['e']==NULL ? "NULL" : $f['e'];
			$d=$f['d']==NULL ? "NULL" : $f['d'];
			$dt=$f['dt']==NULL ? "NULL" : $f['dt'];
			$sql="UPDATE ".SYS_PERMISSIONS_TABLE." SET v=$v, i=$i, e=$e, d=$d, dt=$dt WHERE id={$f['id']}";
			$q=mysql_query($sql);
		}
	}
	
	public function permissionDelete(){
		foreach($this->recordset as $f){
			$this->delPermission($f);
		}
	}
	
	public function procLevel(){
		$f=$this->recordset;
		if($f['grupo']==1){
			$sql="UPDATE ".SYS_USERS_TABLE." SET level=2 WHERE id={$f['id']}";
		}else{
			$sql="UPDATE ".SYS_USERS_TABLE." SET level=3 WHERE id={$f['id']}";
		}
		$q=mysql_query($sql);
	}
}
?>