varName; $json = new Services_JSON(); function hazPadding($nivel) { for ($j=0;$j<$nivel;$j++) echo "\t"; } function RecorreNodos(&$curNode,$nivel, &$json) { if (count($curNode->children)>0) { echo "{\n"; $i=0; foreach ($curNode->children as $node) { if ($i) echo ",\n"; hazPadding($nivel); echo $json->encode($node->name) . ':'; RecorreNodos($node,$nivel+1, $json); $i++; } echo "\n"; hazPadding($nivel); echo "}"; } else { $recurVar = new stdClass(); $recurVar->id = $curNode->id; $recurVar->type = $curNode->type; $recurVar->datatype = $curNode->datatype; if ($curNode->datatype=='FILE') { $folder = ((int)$curNode->id)%255; $fileName = $_SERVER['DOCUMENT_ROOT'].'/components/com_vxc/resources/'.$folder.'/'.$curNode->id . '_L'; $data = @file_get_contents($fileName); if ($data!==FALSE) { //$data = chunk_split(@base64_encode($data), 64, "\n"); $data = @base64_encode(gzcompress($data)); $data = @str_replace(array('+','/','\r','\n','='),array('*','-','','','_'),$data); $recurVar->data_B = $data; } } //echo "\n"; //hazPadding($nivel); echo $json->encode($recurVar); } } switch($varName) { case 'staticresources': $db =& VoxelCareDB::getDatabase(); $resources = array(); $sql = "SELECT r.id id, r.user user, r.name name, r.father father ,ty.name type, d.name datatype FROM jos_vxc_resource r LEFT JOIN jos_vxc_attributetype ty ON ty.id = r.type LEFT JOIN jos_vxc_datatype d ON d.id = ty.datatype WHERE r.user IS NULL"; $db->setQuery($sql); $rows =& $db->loadObjectList(); $nodes = array(); // En una primera pasada nos guardamos los recursos con sus padres foreach ($rows as $row) { $resId = $row->id; $resource = new stdClass(); $resource->name = $row->name; $resource->datatype = $row->datatype; $resource->type = $row->type; $resource->fullname = ''; $resource->father = $row->father; $resource->id = $row->id; $resource->children = array(); $resource->parentNode =null; $nodes[$resId] = $resource; } // Contruimos el arbol de nodos $nodoRoot = new stdClass(); $nodoRoot->children = array(); $nodoDemo = new stdClass(); $nodoDemo->name = 'DEMO'; $nodoDemo->children = array(); $nodoRoot->children[] = &$nodoDemo; $nodoTemplates = new stdClass(); $nodoTemplates->name = 'TEMPLATES'; $nodoTemplates->children = array(); $nodoRoot->children[] = &$nodoTemplates; $nodoGeneral = new stdClass(); $nodoGeneral->name = 'GENERAL'; $nodoGeneral->children= array(); $nodoRoot->children[] = &$nodoGeneral; foreach($nodes as $id=> $resource) { if ($resource->father <0) //recursos demo -1 y templates -2 { if ($resource->father==-1) { $nodoDemo->children[] = &$nodes[$id]; } else if ($resource->father==-2) { $nodoTemplates->children[] = &$nodes[$id]; } } else { if ($resource->father) { if (!$nodes[$resource->father]->children) $nodes[$resource->father]->children = array(); $nodes[$resource->father]->children[] =& $nodes[$id]; } else { if (!$nodoGeneral->children) $nodoGeneral->children = array(); $nodoGeneral->children[] = &$nodes[$id]; } } } // Recorremos el arbol de nodos generando la variable recursiva echo $varName ."={\n"; //$tree =array(); $i =0; foreach ($nodoRoot->children as $node) { if ($i) echo ",\n"; echo $json->encode($node->name) . ':'; RecorreNodos($node,1, $json); $i++; } //echo $json->encode($tree); echo '}'; break; } ?>