<?
////////////////////////////////////////////////////
// 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");
include("../phplib/auth.php");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");

$resId=$_REQUEST['resId'];
if(isset($_REQUEST['parentId'])) $parentId=$_REQUEST['parentId'];
$conf=simplexml_load_file("../conf/resources/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->masterview);
$accion=$_REQUEST['action'];

$obj=NULL;		
$result="";		

switch($accion){
	case "reorder":
		if(!isset($conf->data->field_sort)){
			$obj->userdata->result="KO";
		}else{
			$items=explode(",",$_REQUEST['items']);
			for($i=0; $i<count($items); $i++){
				$sql="UPDATE ".$conf->data->table." SET {$conf->data->field_sort}={$i} WHERE {$conf->data->field_key}={$items[$i]}";
				$q=mysql_query($sql);
			}
			$nodos=$conf->xpath("//event[@type='onReorder']");
			foreach($nodos as $evento){
				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->$metodo($params);
			}
			
			$obj->userdata->result="OK";
		}
		$result=json_encode($obj);
		break;
	case "reparent":
		
		break;
}
echo $result;
?>