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

class modFormHandler  {
  private $lo_id;   
  private $login_name;   
  private $selectors;
  private $inform_data;  
  private $inform_seq;  
  private $inform_def;  
  private $insumm_hdr;  
  private $ipform_data; 
  private $ipform_seq; 
  private $ipform_def; 
  private $ipsumm_hdr;
  private $control_opt;
  
  public function __construct($lo_id, $en_id, $ig_id, $ic_id, $it_id, $in_id, $ip_id) {

    $this->lo_id = isset($lo_id) ? $lo_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, $ig_id, 'fkyInstrumentGroup'), " .
           "($this->lo_id, $ig_id, $ic_id, 'fkyInstrumentClass'), " .
           "($this->lo_id, $ic_id, $it_id, 'fkyInstrumentType'), " .
           "($this->lo_id, $it_id, $in_id, 'sumInstruments'), " .
           "($this->lo_id, $in_id, $ip_id, 'sumInstrPerforms');  ";
    $this->result = $dal->query($sql);

    $this->selectors = $this->formSel('formhandler', $this->lo_id);

    $this->inform_data = $this->formData(666);  
    $this->inform_seq  = $this->formSeq(666);  
    $this->ipform_data = $this->formData(664); 
    $this->ipform_seq  = $this->formSeq(664); 

    $this->insumm_hdr = ["0", "Instrument", "Group", "Class", "Type"];
    $this->ipsumm_hdr = ["0", "Period", "Change"];
    
    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, abbr AS text " .
           "FROM stockexchange WHERE 1 " ;
    $this->exchanges = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM instrperiod WHERE 1 " ;
    $this->perf_periods = $dal->query($sql);

    $default = array ("id"=>0, "id_attr"=>0,
            "name"=>"xxx",     "name_attr"=>"",
            "exchange_id"=>0,  "exchange_id_attr"=>"",
            "symbol"=>"ooo",   "symbol_attr"=>"",
            "type_id"=>0,      "type_id_attr"=>0,
            "class_id"=>0,     "class_id_attr"=>0,
            "group_id"=>0,     "group_id_attr"=>$en_id,
            "complete_ind"=>1, "complete_ind_attr"=>"");
    $this->inform_def = $this->toObject ($default);

    $default = array ("id"=>0, "id_attr"=>0, 
         "instrument_id"=>0,   "instrument_id_attr"=>"10",
         "perfperiod_id"=>"0", "perfperiod_id_attr"=>"",
         "changepct"=>"",      "changepct_attr"=>"");
    $this->ipform_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);
  }

  private function formSel ($path, $lo_id) {
    $dal = new DAL();
    $sql = "SELECT CC.division, CC.field, label, idx, filter, refine, " .
           "targetform, summaryid, selectid, width, type, attr " .
           "FROM menuitem MI, controlcontext CC, controlcontexttmp CT " .
           "WHERE MI.path = '$path' AND CC.menuitem_id = MI.id " .
           "AND CT.field = CC.field AND CT.login_id = $lo_id " .
           "ORDER by idx DESC";
    $result = $dal->query($sql);

    $object = new stdClass();
    $key = "";
    $currDivision = '';
    $selectors = [];
    foreach ($result as $key => $value) {
      if ($currDivision != $value->division) {
        $key = $currDivision;
        if ($currDivision != "")
          $object->$currDivision = $selectors;
        $selectors = [];
      }
      $selectors[] = $value;
      $currDivision = $value->division;
    }
    if ($currDivision != "")
      $object->$currDivision = $selectors;

    return json_encode($object);
  }    

  private 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, 'cky', 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);
  }    

  private 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;
  }
  
}

?>