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

class modReportGenerator  {
  public $scope = "reportgenerator";  
  public $form_defs;  
  public $gen_reports;
  
  public function __construct() {

    $dal = new DAL();

    $sql = "SELECT CI.instance AS field, " .
           "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 = 'frmReportQuery') ";
    $this->rqform_data = $dal->query($sql);  
    
    $sql = "SELECT id, text FROM combinetype WHERE id > 0 ";
    $this->combines = $dal->query($sql);


//    $sql = "SELECT id, reportfilename AS Name, reportdescr AS Description, " .
//           "querycriteria AS 'Condition Inputs' " .
//           "FROM reportquery " .
//           "WHERE active = 1 AND combineidx < 2 " .
//           "ORDER BY name ";
//    $this->gen_reports = $dal->query($sql);


    $default = array ("id"=>0,             "sumid"=>1,
                      "filename"=>"",      "filename_text"=>"",
                      "reportdescr"=>"",   "reportdescr_text"=>"",
                      "queryselect"=>"",   "queryselect_text"=>"",
                      "queryfrom"=>"",     "queryfrom_text"=>"",
                      "querywhere"=>"",    "querywhere_text"=>"",
                      "queryorganize"=>"", "queryorganize_text"=>"",
                      "querycriteria"=>"", "querycriteria_text"=>"");
    $this->rqform_defs = $this->toObject ($default);
  }

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

}

?>