<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

function buildCombined ($id, $query) {

  $dal = new DAL();

  $sql = "SELECT RQ.id, query, combinequery_id, " .
         "(CASE WHEN CT.text = 'COMBINE' THEN '' ELSE CONCAT(CT.text, ' ') END) as combine " .
         "FROM reportquery RQ, combinetype CT " .
         "WHERE RQ.id = $id AND RQ.combineidx > 0 " .
         "AND RQ.combinetype_id = CT.id AND RQ.active = 1 LIMIT 1" ;
  $report = $dal->query($sql);

  if (null == $report)
    return $query;
  else {
//  print_r ("query:$report->query XX");      
    return buildCombined ($report->combinequery_id, $query .
                          $report->combine . $report->query . " ");
  }                          
}  

function buildCriteria ($criteria, $offset, $where) {
//print_r ("crit:$criteria $offset $where ");    
  $start = strpos ($where, "?", $offset);
  if ($start == false)
    return $criteria;
  else {
    $comma = ($criteria == "") ? "" : ",";  
    $offset = $start;
    $end = strpos ($where, " ", $offset);
    if (!$end) {
      $next = substr ($where, $start);
      $end = strlen($where); 
    }
    else { 
      $length = $end - $start;
      $next = substr ($where, $start, $length);
    }  
    return buildCriteria($criteria . $comma . $next, $end, $where);
  }
}   

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id")
    $rg_id = $paramValue;
  else if ($paramName == "reportname")
    $name = $paramValue;
  else if ($paramName == "repdescription")
    $repdescription = $paramValue;
  else if ($paramName == "queryselect")
    $queryselect = $paramValue;
  else if ($paramName == "queryfrom")
    $queryfrom = $paramValue;
  else if ($paramName == "querywhere")
    $querywhere = $paramValue;
  else if ($paramName == "queryorganize")
    $queryorganize = $paramValue;
  elseif ($paramName == "querycriteria") 
    $querycriteria = $paramValue;  
  elseif ($paramName == "combinetype_id") 
    $combinetype_id = $paramValue;  
}

$catqueryselect = str_replace("\n", " ", $queryselect); // remove new lines
$catqueryfrom   = str_replace("\n", " ", $queryfrom); // remove new lines
$catquerywhere  = str_replace("\n", " ", $querywhere); // remove new lines

$querycriteria  = buildCriteria ("", 0, $catquerywhere);

$query = "SELECT " .  $catqueryselect . " FROM " . $catqueryfrom . 
         " WHERE " . $catquerywhere . " " .  $queryorganize ;

$dal = new DAL(); 

if ($rg_id == 0) {
  $sql = "INSERT INTO reportquery " .
         "(name, repdescription, queryselect, queryfrom, querywhere, " .
         "queryorganize, querycriteria, query, combinetype_id) " .
         "VALUES ('$name', '$repdescription', '$queryselect', '$queryfrom', " .
         "'$querywhere', '$queryorganize', '$querycriteria', $query, " .
         "'$combinetype_id') " ;
  $result = $dal->query($sql);

  $sql = "INSERT INTO reportquerysetup () VALUES () " ;
  $result = $dal->query($sql);
}
else {
  $sql = "UPDATE reportquery SET " .
         "name='$name', repdescription='$repdescription', " .
         "queryselect='$queryselect', queryfrom='$queryfrom', " .
         "querywhere='$querywhere', queryorganize='$queryorganize', " .
         "querycriteria='$querycriteria', query='$query', " .
         "WHERE id = $rg_id ";

  $result = $dal->query($sql);
}         

$combinedQuery = buildCombined ($rg_id, "");

if ($combinedQuery != "") {
  $sql = "UPDATE reportquery SET " .
         "combinedQuery='$combinedQuery' " .
         "WHERE id = $rg_id ";
  $result = $dal->query($sql);
} 

echo json_encode($result);

?>

