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


include("../conf/preferences.php");
session_name(SYS_APP_SESSNAME);
session_start();
include("../conf/langs/".$langs[$_SESSION['userlang']][0]."/MAIN.php");
include("../conf/connections.php");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");

if(!isset($_REQUEST['resId'])) header("Location:error.php");
$resId=$_REQUEST['resId'];
$conf=simplexml_load_file("../conf/resources/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->masterview);
$id=$_REQUEST['id'];
$table=$conf->data->table;
$dep_field=(string)$conf->data->dependency_field;
$key=$conf->data->field_key;
$orderby=$conf->data->orderby;
$ancestor=$_REQUEST['ancestor'];
$action=isset($_REQUEST['action']) ? $_REQUEST['action'] : "get";

switch($action){
	case "get":		
		$query=exclContent("SELECT a.*, (SELECT COUNT($key) FROM $table WHERE $dep_field=a.$key) AS children FROM $table a WHERE $dep_field=$id ORDER BY $orderby");
		$q=mysql_query($query) or die("ERROR SQL: " . $query);
		
		$cadena='{"ResultSet":{"Result":[';
		
		while($f=mysql_fetch_array($q)){
			$cadena.='{label:"'.$f[(string)$conf->data->label_field].'",data:"'.$f[(string)$conf->data->field_key].'",children:'.$f['children'].',ancestor:' . $f[(string)$conf->data->dependency_field] . '},';
		}
		$cadena=rtrim($cadena,",");
		$cadena.=']}}';
		echo $cadena;
		break;
	case "orderNext":
		$nextId=$_REQUEST['nextId'];
		$a1="SELECT orden FROM $table WHERE id=$id";
		$a2="SELECT orden FROM $table WHERE id=$nextId";
		$q1=mysql_query($a1);
		$f1=mysql_fetch_array($q1);
		$q2=mysql_query($a2);
		$f2=mysql_fetch_array($q2);
		$q=mysql_query("UPDATE $table SET $orderby=".$f1['orden'] . " WHERE $key=$nextId") or die(mysql_error());
		$q=mysql_query("UPDATE $table SET $orderby=".$f2['orden'] . " WHERE $key=$id") or die(mysql_error());
		echo 'ok';
		break;
	case "orderPrev":
		$prevId=$_REQUEST['prevId'];
		$a1="SELECT orden FROM $table WHERE id=$id";
		$a2="SELECT orden FROM $table WHERE id=$prevId";
		$q1=mysql_query($a1);
		$f1=mysql_fetch_array($q1);
		$q2=mysql_query($a2);
		$f2=mysql_fetch_array($q2);
		$q=mysql_query("UPDATE $table SET $orderby=".$f1['orden'] . " WHERE $key=$prevId") or die(mysql_error());
		$q=mysql_query("UPDATE $table SET $orderby=".$f2['orden'] . " WHERE $key=$id") or die(mysql_error());
		echo 'ok';
		break;
	case "orderLast":
		$lastId=$_REQUEST['lastId'];
		$a1="SELECT orden FROM $table WHERE id=$id";
		$a2="SELECT orden FROM $table WHERE id=$lastId";
		$q1=mysql_query($a1);
		$f1=mysql_fetch_array($q1);
		$q2=mysql_query($a2);
		$f2=mysql_fetch_array($q2);
		$q=mysql_query("UPDATE $table SET $orderby=".($f2['orden']+1) . " WHERE $key=$id") or die(mysql_error());
		echo 'ok';
		break;
	case "orderFirst":
		$firstId=$_REQUEST['firstId'];
		$a1="SELECT orden FROM $table WHERE id=$id";
		$a2="SELECT orden FROM $table WHERE id=$firstId";
		$q1=mysql_query($a1);
		$f1=mysql_fetch_array($q1);
		$q2=mysql_query($a2);
		$f2=mysql_fetch_array($q2);
		$q=mysql_query("UPDATE $table SET $orderby=".($f2['orden']-1) . " WHERE $key=$id") or die(mysql_error());
		echo 'ok';
		break;
	case "levelUp":
		$a1="UPDATE $table SET $dep_field=$ancestor WHERE $key=$id";
		$q=mysql_query($a1) or die('ko');
		echo 'ok';
		break;
	case "levelDown":
		break;
	case "removeNode":
		echo (recursiveErase($table,$key,$id,$dep_field));
		break;
}

function recursiveErase($table,$key,$id,$dep){
	$cadena="SELECT $key FROM $table WHERE $dep=$id";
	$q=mysql_query($cadena) or die(mysql_error());
	while($f=mysql_fetch_array($q)){
		$resultado=recursiveErase($table,$key,$f[(string)$key],$dep);
		
		if(isset($conf->data->events)){
			foreach($conf->data->events->event as $evento){
				if((string)$evento['type']!='onBeforeDelete') continue;
				if(!estaArchivoIncluido((string)$evento['src'])) include("../".$evento['src']);
				$nombreclase=(string)$evento['class'];
				$metodo=(string)$evento['method'];
				if(isset($evento['params'])){
					$params=explode("@@@",(string)$evento['params']);
					for($i=0;$i<count($params);$i++) $params[$i]=Content::parse($params[$i]);
				}else{
					$params=NULL;
				}
				$objeto=new $nombreclase();
				$objeto->resId=$resId;
				$objeto->recordset=$f;
				$objeto->mail=$mail;
				$objeto->$metodo($params);
			}
		}
		
		$sql="SELECT * FROM " . SYS_MEDIA_TABLE . " WHERE idres=$resId AND idrec IN(" . implode(",",$valores) . ")";
		$q=mysql_query($sql);
		while($item=mysql_fetch_assoc($q)){
			if($item['readonly']!=1){
				@unlink($item['filename']);
				mysql_query("DELETE FROM " . SYS_MEDIA_TABLE . " WHERE id={$item['id']}");
			}
		}
	
		$cadena="DELETE FROM $table WHERE $key=$id";
		$q=mysql_query($cadena);
		return $q ? "ok" : mysql_error();
		if(SYS_LOG_OPS==true){
			registerLog($resId,3);
		}
		
	
		if(isset($conf->data->events)){
			foreach($conf->data->events->event as $evento){
				if((string)$evento['type']!='onAfterDelete') continue;
				if(!estaArchivoIncluido((string)$evento['src'])) include("../".$evento['src']);
				$nombreclase=(string)$evento['class'];
				$metodo=(string)$evento['method'];
				if(isset($evento['params'])){
					$params=explode("@@@",(string)$evento['params']);
					for($i=0;$i<count($params);$i++) $params[$i]=Content::parse($params[$i]);
				}else{
					$params=NULL;
				}
				$objeto2=new $nombreclase();
				$objeto2->resId=$resId;
				$objeto2->recordset=$F;
				$objeto2->mail=$mail;
				$objeto2->$metodo($params);
			}
		}

	}
	
}

?>