<!DOCTYPE html>
<html lang="en">

<head>
  <meta http-equiv="Content-Language" content="en-us">
  <title>Form Report</title>
  <meta charset="utf-8">

  <link rel="stylesheet" href="/ext/bootstrap/bootstrap.min.css">
  <link rel="stylesheet" href="/ext/bootstrap/bootstrap-theme.min.css">
  <link rel="stylesheet" href="/site/css/stickythead.css">
  <link rel="stylesheet" href="/site/css/bootstrap.css">
  <link rel="stylesheet" href="/site/css/multiselect.css">
  <link rel="stylesheet" href="/site/css/slider.css">
  <link rel="stylesheet" href="/site/css/style.css">

  <script src="/ext/jquery/jquery-3.1.1.min.js"></script>
  <script src="/ext/bootstrap/bootstrap.min.js"></script>
  <script src="/ext/jquery/jquery.stickytableheaders.min.js"></script>
  <script src="/site/js/main.js"></script>

</head>
<body>

<?php

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

$dal = new DAL();

function toObject($array) {
  $object = new stdClass();
  foreach ($array as $key => $value) {
   if (is_array($value)) {
     $value = toObject($value);
   }
   $object->$key = $value;
 }
 return $object;
}

function newTarget ($code, $id) {
  $dal = new DAL();
  $sql = "SELECT $id AS proc " .
         "FROM controlcomm " .
         "WHERE code = '$code' LIMIT 1 ";
  $proc = $dal->query($sql)->proc;

  $new =  array ("id"=>"$id", "proc"=>"$proc");
  return $new;       
} 
//$formList = "frmLocationDefinition,frmPartFeature";
//$formList = "frmSetSiteName,frmSiteDefinition,frmPedestrian,frmShovelTest,frmPTPLot";
//$formList = "frmSetLotName,frmLotDefinition,frmLotComposition";
//$formList = "frmCollectBag,frmArtifactBatch";
//$formList = "frmSetClientContact,frmSetOrganization,frmContactAffiliation,frmContactAddress";
//$formList = "frmStoredProcedure";
//$formList = "frmSetProjectName,frmSetOrgContact,frmProjectDefinition";
//$formList = "frmSetParcelProject,frmProjectParcel";
//$formList = "frmControlInstance,frmControlEntry";
$formList = "frmOfficeWorkday,frmOfficeProject,frmOfficeTimesheet";
//$formList = "frmFieldProject";
//$formList = "frmOperationDefinition,frmDailyOperation";
//$formList = "frmFieldProject";
//$formList = "frmWeatherLog";
$fdh = explode (",", $formList);

//$cnt = count($fdh);
//print_r ("cnt:$cnt<br>");
//print_r ("fhd:$fdh[0]<br>");
//print_r ("fhd:$fdh[1]<br>");


  $layout = "
<div class='container'>	
  <div class='row'>
    <div class='col-md-12'>
      <table class='table table-bordered'>
        <h2>Form Process Report</h2>  
";


$targets = [];

for ($i=0; $i<count($fdh); $i++) {

//print_r ("fhd1:$fdh[$i]");

  $sql = "SELECT SUBSTR(CI.instance, 1, 3) AS 'code', " .
         "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
         "FROM controlinstance CI, controlform CF, " .
         "controlentry COL, controlentry LAB " . 
         "WHERE LAB.controlinstance_id = CI.id " .
         "AND LAB.property IN ('label', 'noLabel') " .
         "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
         "AND CF.controlinstance_id = CI.id AND CF.action = 'sendid' " .
         "AND CF.formhandler_id = " .
         "(SELECT FH.id FROM controlinstance FH " .
         "WHERE FH.instance = '$fdh[$i]') ";
  $seq_data = $dal->query($sql);
//print_r ($seq_data);  
  foreach ($seq_data as $sd) {

    if ($sd->sequent != "") {
      $seq = explode (',', $sd->sequent);
      for ($j=0; $j<count($seq); $j++) {
//print_r ($sd->code);          
        list ($field, $id) = explode(':', $seq[$j]);
        $code = substr($field, 0, 3);
        $field = 'ALL' . $field;
        $new = newTarget($code, $id); 
        $targets[$field] = $new;
      }    
    }

    if ($sd->sequent_frm != "") {
      $seq = explode (',', $sd->sequent_frm);
      for ($j=0; $j<count($seq); $j++) {
//print_r ($sd->code);          
        list ($field, $id) = explode(':', $seq[$j]);
        $code = substr($field, 0, 3);
        $field = 'FRM' . $field;
        $new = newTarget($code, $id);        
        $targets[$field] = $new;
      }    
    }  

    if ($sd->sequent_upd != "") {
      $seq = explode (',', $sd->sequent_upd);
      for ($j=0; $j<count($seq); $j++) {
//print_r ($sd->code);          
        list ($field, $id) = explode(':', $seq[$j]);
        $code = substr($field, 0, 3);
        $field = 'UPD' . $field;
        $new = newTarget($code, $id);        
        $targets[$field] = $new;
      }    
    }

    if ($sd->sequent_ent != "") {
      $seq = explode (',', $sd->sequent_ent);
      for ($j=0; $j<count($seq); $j++) {
//print_r ($sd->code);          
        list ($field, $id) = explode(':', $seq[$j]);
        $code = substr($field, 0, 3);
        $field = 'ENT' . $field;
        $new = newTarget($code, $id);        
        $targets[$field] = $new;
      }    
    }
    if ($sd->sequent_chk != "") {
      $seq = explode (',', $sd->sequent_chk);
      for ($j=0; $j<count($seq); $j++) {
//print_r ($sd->code);          
        list ($field, $id) = explode(':', $seq[$j]);
        $code = substr($field, 0, 3);
        $field = 'CHK' . $field;
        $new = newTarget($code, $id);        
        $targets[$field] = $new;
      }    
    }
  }
}

//print_r ($targets);

for ($i=0; $i<count($fdh); $i++) {
//print_r ("fhd2:$fdh[$i]");

  $layout .= "
          <tr>
            <th colspan='12'>
    Form: $fdh[$i]
            </th>
          </tr>
          <tr>
            <th>Code</th>
            <th>Control</th>
            <th>Field</th>
            <th>Type</th>
            <th>Recid</th>
            <th>Column</th>
            <th>Commit</th>
            <th>ALL Form Changes</th>
            <th>FRM Form User Entry</th>
            <th>UPD Form After Update</th>
            <th>ENT Form On Entry</th>
            <th>CHK Form Check Row</th>
          </tr>
  ";
  
  $sql = "SELECT SUBSTR(CI.instance, 1, 3) AS 'Code', " .
         "(CASE SUBSTR(CI.instance, 1, 3) " . 
           "WHEN 'frm' THEN 'FormHandler' " .
           "WHEN 'sum' THEN 'SummaryTable' " .
           "WHEN 'sel' THEN 'SelectOption' " .
           "WHEN 'liv' THEN 'LiveSearch' " .
           "WHEN 'mul' THEN 'MultiSelect' " .
           "WHEN 'add' THEN 'AddonForm' " .
           "WHEN 'inp' THEN 'InputText' " .
           "WHEN 'chk' THEN 'CheckBox' " .
           "WHEN 'but' THEN 'Button' " .
           "WHEN 'sas' THEN 'SearchSelect' " .
           "WHEN 'tim' THEN 'TimeKeeper' " .
           "WHEN 'fky' THEN 'FkeyField' " .
           "WHEN 'cxt' THEN 'Selector' " .
           "ELSE '' " .
         "END) AS Control, " .      
         "(CASE SUBSTR(CI.instance, 1, 3) " . 
           "WHEN 'frm' THEN 1 " .
           "WHEN 'sum' THEN 2 " .
           "ELSE 9 " .
         "END) AS controlOrder, " .      
         "(CASE CF.action " . 
           "WHEN 'sendid' THEN 1 " .
           "WHEN 'none' THEN 9 " .
           "ELSE 9 " .
         "END) AS actionOrder, " .      
         "CI.instance AS Field, " .
         "CF.keytype AS Type, " .
         "CI.id AS RecId, " .
         "COL.value AS 'Column', LAB.value AS Label, " .
         "CF.commit AS Commit, CF.action, " .
         "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
         "FROM controlinstance CI, controlform CF, " .
         "controlentry COL, controlentry LAB " . 
         "WHERE LAB.controlinstance_id = CI.id " .
         "AND LAB.property IN ('label', 'noLabel') " .
         "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
         "AND CF.controlinstance_id = CI.id AND CF.formhandler_id = " .
         "(SELECT FH.id FROM controlinstance FH " .
         "WHERE FH.instance = '$fdh[$i]') " .
         "ORDER BY controlOrder, actionOrder";
  $form_data = $dal->query($sql);

  foreach ($form_data as $fd) {

    $layout .= "
          <tr>
            <td>
    $fd->Code
          </td>
          <td>
    $fd->Control
          </td>
          <td>
    $fd->Field
          </td>
          <td>
    $fd->Type
          </td>
          <td>
    $fd->RecId
          </td>
          <td>
    $fd->Column 
          </td>
          <td>
    $fd->Commit 
          </td>
          <td>
    ";
    $seq = explode (',', $fd->sequent);
    $br = '';
    for ($j=0; $j<count($seq); $j++) {
      $layout .= "
        $br$seq[$j]
      ";    
      $br = '<br>';
    }
    $layout .= "
            </td>
          <td>
    "; 
    
    $frm = explode (',', $fd->sequent_frm);
    $br = '';
    for ($j=0; $j<count($frm); $j++) {
      $layout .= "
        $br$frm[$j]
      ";    
      $br = '<br>';
    }
    $layout .= "
          </td>
          <td>
    ";    

    $upd = explode (',', $fd->sequent_upd);
    $br = '';
    for ($j=0; $j<count($upd); $j++) {
      $layout .= "
        $br$upd[$j]
      ";    
      $br = '<br>';
    }
    $layout .= "
          </td>
          <td>
    ";    

    $ent = explode (',', $fd->sequent_ent);
    $br = '';
    for ($j=0; $j<count($ent); $j++) {
      $layout .= "
        $br$ent[$j]
      ";    
      $br = '<br>';
    }  
    $layout .= "
          </td>
          <td>
    ";
    
    $chk = explode (',', $fd->sequent_chk);
    $br = '';
    for ($j=0; $j<count($chk); $j++) {
      $layout .= "
        $br$chk[$j]
      ";    
      $br = '<br>';
    }  
    $layout .= "
          </td>
        </tr>  
    ";
    
    $comm = "";    
    $br = "";
    foreach (["ALL","FRM","UPD","ENT", "CHK"] as $pref)  {  
      $field = $pref . $fd->Field;
      if (null !== $targets[$field]["id"]) {
        $id =  $targets[$field]['id'];
        $comm .= $br . '<i><b>' . $pref . '</b>' . ':' . $fd->Field . ':' . $id . ': ' . $targets[$field]['proc'] . '</i>';
        $br = "<br>";
      }
    }
//print_r ($comm);    
    if ($comm != "") {
      $layout .= "
          <tr>
            <td>

          </td>
          <td colspan='7'>
         $comm
          </td>
        </tr>  
      "; 
    }  
  }
}

$layout .= "
      </table> 
    </div>
  </div>
</div>
";

echo $layout;
?>
   
<script>


</script>

</body>
</html>































