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

class modProjectParcel {

  public function __construct($lo_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 = "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.commit, " .
           "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 = 'frmSetParcelProject') ";
    $this->spform_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.commit, " .
           "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 = 'frmProjectParcel') ";
    $this->ppform_data = $dal->query($sql);  

    $sel = ["1"=>["label"=>"Project", "fkey"=>"",          "sumid"=>1,      "selid"=>$pr_id, "width"=>"50%", "type"=>"search"], 
            "2"=>["label"=>"Parcel",  "fkey"=>"parcel_fk", "sumid"=>$pr_id, "selid"=>$pa_id, "width"=>"50%", "type"=>"select"]];    
    $this->selectors = $this->toObject ($sel);

    $this->parcel_hdr = ["0", "Name"];

    $sql = "SELECT 0 AS id, 'Add' AS text " .
           "UNION ALL " .
           "SELECT 1 AS id, 'Related' AS text ";
           "UNION ALL " .
           "SELECT -1 AS id, 'Delete' AS text ";
    $this->relate_opt = $dal->query($sql);

    $default = array("id"=>0,  "fkid"=>0,
      "name"=>"",              "name_text"=>"",
      "site_name"=>"xxx",      "site_name_text"=>"xxx",
      "refno"=>"rrr",             "refno_text"=>"");
    $this->spform_defs = $this->toObject ($default);

    $default = array("id"=>0, "fkid"=>0,
      "fkid"=>0,              "fkid_text"=>"",
      "name"=>"",             "name_text"=>"",
      "location"=>"",         "location_test"=>"",
      "natural_context"=>"",  "natural_context_text"=>"",
      "directions"=>"",       "directions_text"=>"");
    $this->ppform_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;
  }
 
  
}

?>