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

class modLotDescription {

private $pp_id;
private $pwo_id;
private $lot_list;
private $comp_list;
private $form_blank;
private $no_inclusion;
private $in_inclusion;

  public function __construct($lo_id, $pw_id, $pr_id, $pp_id, $ld_id) {

    $this->lo_id  = isset($lo_id) ? $lo_id : 0;  
    $this->pw_id  = isset($pw_id) ? $pw_id : 0;  
    $this->pr_id  = isset($pr_id) ? $pr_id : 0;  
    $this->pp_id  = isset($pp_id) ? $pp_id : 0;  
    $this->ld_id  = isset($ld_id) ? $ld_id : 0;  

    $this->selectors = 
    [["index"=>"1","refine"=>"1","filter"=>"1","label"=>"Project Parcel","field"=>"fkyProjectParcel","table"=>"sumLotDefinitions","sumid"=>$pp_id,"selid"=>$pp_id,"width"=>"50%","type"=>"READONLY","attr"=>"disabled"]];    

    $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 = 'frmSetLotName') ";
    $this->lnform_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 = 'frmLotDefinition') ";
    $this->ldform_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 = 'frmLotComposition') ";
    $this->lcform_data = $dal->query($sql);    

    $this->lot_hdr = ["0", "Name", "Type", "Composition", 
                      "Texture", "Colour", "Interpretation"];

    $this->comp_hdr = ["0", "Material", "Colour", "Compaction"]; 

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT I.id, I.descr AS text " .
           "FROM  inclusion I " .
           "LEFT JOIN (lotcompositioninclusion LCI, lotcomposition LC) " .
           "ON (LCI.inclusion_id = I.id AND LC.id = LCI.lotcomposition_id " .
           "AND LC.lot_id = 0) " .
           "WHERE LC.composition_id IS NULL " .
           "ORDER BY text ";
    $this->no_inclusion = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT I.id, I.descr AS text " .
           "FROM inclusion I, lotcompositioninclusion LCI, lotcomposition LC " .
           "WHERE LCI.inclusion_id = I.id AND LC.id = LCI.lotcomposition_id " .
           "AND LC.lot_id = 0 " .
           "ORDER BY text ";
    $this->in_inclusion = $dal->query($sql);

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

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

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM lotinterpret WHERE active = 1 " ;
    $this->interpretations = $dal->query($sql);
    
    $sql = "SELECT 0 AS id, 'Material' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM composition WHERE active = 1 " ;
    $this->comp_materials = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Colour' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM munselcolour WHERE active = 1 " ;
    $this->comp_colours = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Compaction' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM compaction WHERE active = 1 " ;
    $this->comp_compactions = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Texture' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM texture WHERE active = 1 " ;
    $this->comp_textures = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Sphericity' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM sphericity WHERE active = 1 " ;
    $this->comp_sphericities = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Rounding' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM rounding WHERE active = 1 " ;
    $this->comp_roundings = $dal->query($sql);

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

    $blank = array
      (array ("p_id"=>0, "bound_id"=>0, "descr"=>""));
    $this->form_blank = $this->toObject ($blank);

    $blank = array
      (array ("ph_id"=>0, "bound_id"=>0, "descr"=>""));
    $this->image_blank = $this->toObject ($blank);
    
    $default = array ("id"=>0, 
                    "fkid"=>0, "fkid_text"=>"",
                    "name"=>"", "name_text"=>"");
	$this->lnform_defs = $this->toObject ($default);

    $default = array ("id"=>0, "fkid"=>0,
         "lottype_id"=>0,      "lottype_id_text"=>"",
         "rationale"=>"",      "rationale_text"=>"",
         "lotinterpret_id"=>0, "lotinterpret_id_text"=>"",
         "note"=>"",           "note_text"=>"");
	$this->ldform_defs = $this->toObject ($default);

    $default = array ("id"=>0, "fkid"=>0,
         "composition_id"=>0,  "composition_id_text"=>"", 
         "munselcolour_id"=>0, "munselcolour_id_text"=>"", 
         "compaction_id"=>0,   "compaction_id_text"=>"", 
         "texture_id"=>0,      "texture_id_text"=>"", 
         "sphericity_id"=>0,   "sphericity_id_text"=>"", 
         "rounding_id"=>0,     "rounding_id_text"=>"",
         "note"=>"",           "note_text"=>"");
	$this->lcform_defs = $this->toObject ($default);

  }

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

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

?>
