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

class modControlField  {
  private $cfform_data;  
  private $cfform_seq;  
  private $cfsumm_hdr;  
  private $caform_data; 
  private $caform_seq; 

  private $field_sum;
  private $field_def;
  private $entry_def;
  private $control_opt;
  
  public function __construct($lo_id, $en_id, $ct_id, $mi_id, $cf_id, $ca_id) {

    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->en_id = isset($en_id) ? $en_id : 0;  
    $this->ct_id = isset($ct_id) ? $ct_id : 0;  
    $this->mi_id = isset($mi_id) ? $mi_id : 0;  
    $this->cf_id = isset($cf_id) ? $cf_id : 0;  
    $this->ca_id = isset($ca_id) ? $ca_id : 0;  

    $dal = new DAL();

    $sql = "SELECT name FROM login WHERE id = $this->lo_id LIMIT 1 ";
    $this->login_name = $dal->query($sql)->name;

   $sql = "DELETE FROM controlcontexttmp WHERE login_id = $this->lo_id ";
   $this->result = $dal->query($sql);

   $sql = "INSERT controlcontexttmp (login_id, summaryid, selectid, field) VALUES " .
           "($this->lo_id, $en_id, $ct_id, 'fkyControlType'), " .
           "($this->lo_id, $ct_id, $mi_id, 'fkyMenuItem'), " .
           "($this->lo_id, $mi_id, $cf_id, 'sumControlFields'), " .
           "($this->lo_id, $cf_id, $ca_id, 'sumControlAttrs');  ";
    $this->result = $dal->query($sql);

    $sql = "SELECT CC.field, label, idx, filter, refine, " .
           "targetform, summaryid, selectid, width, type, attr " .
           "FROM menuitem MI, controlcontext CC, controlcontexttmp CT " .
           "WHERE MI.path = 'controlfield' AND CC.menuitem_id = MI.id " .
           "AND CT.field = CC.field AND CT.login_id = $this->lo_id " .
           "ORDER by idx DESC";
    $this->selectors = $dal->query($sql);

    $this->cfform_data = $this->formData (46);  
    $this->cfform_seq  = $this->formSeq (46);  

    $this->caform_data = $this->formData (44); 
    $this->caform_seq = $this->formSeq (44); 
    
    $this->cfsumm_hdr = ["0", "Field", "Control"];
    $this->casumm_hdr = ["0", "Attribute", "Value"];

    $default = array ("id"=>0,
               "menuitem_id"=>0,      "menuitem_id_attr"=>"10",
               "field"=>"xxx",        "field_attr"=>"",
               "controlobject_id"=>0, "controlobject_id_attr"=>"",
               "formhandler_id"=>0,   "formhandler_id_attr"=>"",
               "complete_ind"=>1,     "complete_ind_attr"=>"");
    $this->cfform_def = $this->toObject ($default);

    $default = array ("id"=>0, 
      "controlfield_id"=>0, "controlfield_id_attr"=>"10",
      "attribute"=>"",      "attribute_attr"=>"",
      "value"=>"",          "value_attr"=>"");
    $this->caform_def = $this->toObject ($default);
    
    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, control AS text FROM controlobject ORDER BY 'text' ";
    $this->control_opt = $dal->query($sql);
  }

  function formData($fh_id) {
    $dal = new DAL();
    $sql = "SELECT field, keytype, dbcolumn, label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "mandatory, uniquekey, confirm, commit, message " .
           "FROM controlfield WHERE formhandler_id = $fh_id " .
           "UNION ALL " .
           "SELECT field, 'aky', dbcolumn, label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "1, 0, 0, 0, '' " .
           "FROM controlcontext WHERE formhandler_id = $fh_id ";
    $result = $dal->query($sql);

    $object = new stdClass();
    foreach ($result as $key => $value) {
      $key = $value->field;
      $object->$key = $value;
    }
    return json_encode($object);
  }    

  function formSeq($fh_id) {
    $dal = new DAL();
    $sql = "SELECT sender AS field, receiver, comkey " .
           "FROM controlsequent " .
           "WHERE formhandler_id = $fh_id " .
           "ORDER BY field ";
    $result = $dal->query($sql);

    $object = new stdClass();
    $key = "";
    $currField = '';
    $sequents = [];
    foreach ($result as $key => $value) {
      if ($currField != $value->field) {
        $key = $currField;
        if ($currField != "")
          $object->$currField = $sequents;
        $sequents = [];
      }
      $sequents[] = $value;
      $currField = $value->field;
    }
    if ($currField != "")
      $object->$currField = $sequents;
    
    return json_encode($object);
  }    

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

  public function get($prop) {
    return $this->$prop;
  }
  
}

?>