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

class modArtifactBag {

private $pa_id;
private $pwo_id;

private $site_defs;
private $prop_part;
private $form_blank;

  public function __construct($lo_id, $pw_id, $pa_id, $cu_id, $su_id, $sl_id) {
    
    $this->lo_id  = isset($lo_id) ? $lo_id : 0;  
    $this->pw_id  = isset($pw_id) ? $pw_id : 0;  
    $this->pa_id  = isset($pa_id) ? $pa_id : 0;  
    $this->cu_id  = isset($cu_id) ? $cu_id : 0;  
    $this->su_id  = isset($su_id) ? $su_id : 0;  
    $this->sl_id  = isset($sl_id) ? $sl_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 = "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 = 'label' " .
           "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 = 'frmCollectBag') ";
    $this->cbform_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 = 'label' " .
           "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 = 'frmArtifactBatch') ";
    $this->abform_data = $dal->query($sql);    


    $sel = [["label"=>"Site","fkey"=>"parcel_fk","sumid"=>$pa_id,"selid"=>$cu_id,"width"=>"34%","type"=>"SELECT"], 
            ["label"=>"PTP", "fkey"=>"site_fk",  "sumid"=>$cu_id,"selid"=>$su_id,"width"=>"33%","type"=>"SELECT"],
            ["label"=>"Lot", "fkey"=>"ptp_fk",   "sumid"=>$su_id,"selid"=>$sl_id,"width"=>"33%","type"=>"SELECT"]];    
    $this->selectors = $sel;

    $this->collbag_hdr = ["0", "RefNo", "Title"];

    $this->artifact_hdr = ["0", "Bag Reference", "Title", "Affiliation", "Bag Count"];

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

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

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

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

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

    $values = ["1"=>["id"=>"Yes", "text"=>"Yes"], "2"=>["id"=>"No", "text"=>"No"]];    

    $default = array ("id"=>0, "fkid"=>0,
                  "refno"=>"", "refno_text"=>"",
                  "title"=>"", "title_text"=>"",
                  "spatialunitlot_id"=>0, "spatialunitlot_id_text"=>"");
    $this->cbform_defs = $this->toObject ($default);

    $default = array ("id"=>0,   "fkid"=>0,
        "affiliation_id"=>0,     "affiliation_id_text"=>"",
        "material_id"=>0,        "material_id_text"=>"", 
        "form_id"=>0,            "from_id_text"=>"",
        "completeness_id"=>0,    "completeness_id_text"=>"", 
        "diagnostic"=>0,        "diagnostic_text"=>"",
        "collectcount"=>0,       "collectcount_text"=>"",
        "docucount"=>0,          "docucount_text"=>"",
        "samplerationale_id"=>0, "samplerationale_id_text"=>"",
        "note"=>"",              "note_text"=>"");
    $this->abform_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;
  }
}

?>
