<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");

include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');

$ObjStructureOrganizationMod = new StructureOrganizationMod($MS_SP);
if(true === isset($_GET['id']) && true === is_numeric($_GET['id'])){
  $VarWhere = "a.structureorganization_parent_id = '". $_GET['id'] ."'";
}
else{
  $VarWhere = "a.structureorganization_parent_id = '0'";
}
$ArrStructureOrganization = $ObjStructureOrganizationMod->GetFromDb('SqlBuildTree', 'GetAll', $VarWhere, 'a.structureorganization_position');
//$VarReturn = '[';
//if (true === is_array($ArrStructureOrganization) && 0 < count($ArrStructureOrganization)){
  $ArrFields = array();
  foreach ($ArrStructureOrganization AS $key => $row){
    if (1 <= $row['childcount']){
      $VarState = 'closed';
    }
    else{
      $VarState = '';
    }
    $ArrFields[$key] = array("attr" => array("id" => "node_". $row['structureorganization_id'], "rel" => $row['structureorganization_type']),
                        "state" => $VarState,
                        "data" => array("title" => $row['structureorganization_title'])
                       );
  }
  echo json_encode($ArrFields);
//  foreach ($ArrStructureOrganization AS $key => $row){
//    if('[' != $VarReturn){
//      $VarReturn .= ',';
//    }
//    if (1 <= $row['childcount']){
//      $VarState = '"state" : "closed"';
//    }
//    else{
//      $VarState = '"state" : ""';
//    }
//    $VarReturn .=
//    '{ "attr" : { "id" : "node_'. $row['structureorganization_id'] .'", "rel" : "'. $row['structureorganization_type'] .'"  },
//        "data" : "'. $row['structureorganization_title'] .'", '. $VarState .'
//    }';
//  }
//}
//$VarReturn .= ']';

//$VarReturn = iconv("windows-1251", "UTF-8", $VarReturn);
//echo $VarReturn;
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>