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

class modControlInstance  {
  private $instance_sum;
  private $instance_def;
  private $entry_def;
  private $control_opt;
  
  public function __construct($lo_id, $mm_id, $ci_id) {

    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->mm_id = isset($mm_id) ? $mm_id : 0;  
    $this->ci_id = isset($ci_id) ? $ci_id : 0;  
print_r ("mm:$this->mm_id ci:$this->ci_id");      

    $this->selectors = 
    [["index"=>"1","refine"=>"1","filter"=>"1","label"=>"Panel Folder","field"=>"sumControlInstances","table"=>"sumControlInstances","sumid"=>1,"selid"=>$this->mm_id,"width"=>"50%","type"=>"SELECT","attr"=>""]];

    $dal = new DAL();

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

    $sql = "SELECT CI.instance AS field, CF.keytype, " .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, 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 = 'frmControlInstance') ";
    $this->ciform_data = $dal->query($sql);  
   
    $sql = "SELECT CI.instance AS field, CF.keytype," .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, 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 = 'frmControlEntry') ";
    $this->ceform_data = $dal->query($sql); 
    
//foreach  ($this->ceform_data as $data) {
//    print_r ($data->field);
//}
//$cnt=count($this->ceform_data);
//print ("cnt=$cnt");
//
//    $default = array('1' => array("id"=>0, "Instance"=>"", 
//                                  "Location"=>"", "Control"=>""));
//    $this->instance_sum = $this->toObject ($default);

    $this->instance_hdr = ["Instance", "Control"];


    $default = array ("id"=>0,
               "panelfolder_id"=>0,   "panelfolder_id_text"=>"",
               "instance"=>"xxx",     "instance_text"=>"",
               "controlobject_id"=>0, "controlobject_id_text"=>"",
               "complete_ind"=>0,     "complete_ind_text"=>"");
    $this->instance_defs = $this->toObject ($default);

    $default = array ("id"=>0, 
      "controlinstance_id"=>0, "controlinstance_id_text"=>"",
      "property"=>"",          "property_text"=>"",
      "value"=>"",             "value_text"=>"");
    $this->entry_defs = $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);
  }
  
  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;
  }
  
}

?>