<?php

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

class modReportTracker {
  /*Don't really know what these variables are for. I presume that they are related to forms on the panel and 
  not each control. I think that there was only one form on the template panel in controlsdemo so the variables
  related to a potential second one are commented out here. */
  private $lo_id;
  private $login_name;
  private $selectors;
  private $inform_data;
  private $inform_seq;
  private $inform_def;
  private $insum_hdr;
//  private $ipform_data;
//  private $ipform_seq;
//  private $ipform_def;
//  private $ipform_hdr;
  private $control_opt;

  /*Whole pile of variables passed in here. These appear to be ids providing session values to the method.
  Current working hypothesis is that these are provided in case user is returning to the panel so that last
  known condition is replicated. From index.php, these values are all set to zero if not present in session. */
  
  Public function __construct($lo_id, $en_id, $ig_id, $ic_id, $it_id, $ih_id ) {

    //Why is it necessary to check to see if there is a session value for login? This check was made in index.php.
    $this->lo_id = isset($lo_id) ? $lo_id : 0;

    $dal = new DAL();

    //Translates login id to name from database and stores name in a variable.
    //Why is this needed? Login name is not apparently used anywhere.
    $sql = "SELECT name FROM login WHERE id = $this->lo_id ";
    $this->login_name = $dal->query($sql);

    /*Apparently a table of the database controlcontexttmp is used to store session values for the panel.
       Why? Isn't it the role of session variables to store these values?
       Presumably they could be retrieved from beyond the expiry of a session. 
       Each login apparently stores its values by login_id.
       But on the other hand each set of values is overwritten with new values as soon as the panel is created.
       These values will be zero where there are no session values currently set - so no past state is retained.
       Are these values used elsewhere for something?       
     */
											   
											   
    //First any existing records in the table for the current login are deleted.
    $sql = "DELETE FROM controlcontexttmp WHERE login_id = $this->lo_id ";
    $this->result = $dal->query($sql);

    //New values from session are inserted into the temporary relation controlcontexttmp.
    $sql = "INSERT controlcontexttmp(login_id, summary_id, selectid, field) VALUES " .
	   "($this->lo_id, $en_id, $ig_id, 'fkyInstrumentGroup', " .
	   "($this->lo_id, $ig_id, $ic_id, 'fkInstrumentClass', " .
	   "($this->lo_id, $ic_id, $it_id, 'fkInstrumentType', " .
	   "(this->lo_id, $it_id, $in_id, 'sumInstruments', " .
	   "(this->lo_id, $in_id, $ip_id, 'sumInstrPerforms'); ";
    $this->result = $dal->query($sql);

    //What are the variables in these tables?
    $sql = "SELECT CC.field, label, idx, filter, refine, targetform, " .
	   "summaryid, selectid, width, type, attr " .
	   "FROM menuitem MI, controlcontext CC, controlcontexttmp CT " .
	   "WHERE MI.path = 'formhandler' 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);

    //Form and seq data. Looks like one form and seq per form on the panel.
    //What do the values for form and seq represent?
    //Here one form is envisioned for now
    $this->inform_data = $this->formData(666);
    $this->inform_seq = $this->formSeq(666);

    //Header values for the summary table.
    $this->insum_hdr = ["0", "Instrument", "Group", "Class", "Type"];

    //Default values for each field in each form.
    //One array is declared for each form on the panel - resulting in a call
    //to the toObject method to assign the array to a form-specific variable.
    //Are there references in the template to fields not present on the form?
    //e.g. Exchange and symbol.
    //Also appears that some entries are required beyond the set of form fields.
    //e.g. values for id and complete_ind.
    $default = array ("id"=>0, "id_attr"=>0,
		      "name"=>"hello", "name_attr"=>"",
		      "type_id"=>0, "type_id_attr"=>"",
		      "class_id"=>0, "clas_id_attr"=>"",
		      "group_id"=>0, "group_id_attr"=>"",
		      "complete_ind"=>1, "complete_ind_attr"="");
    $this->inform_def = $this->toObject($default);

    //Perhaps establishment of static value dropdowns a la root_programs.txt
    $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);
  }
  
  //What does this method do?
  private function fromData($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);
  }    

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