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

class modLocationDefinition {

private $pa_id;
private $pwo_id;
private $spatialunits;
private $form_blank;

  public function __construct($lo_id, $pw_id, $pr_id, $pp_id, $su_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->su_id  = isset($su_id) ? $su_id : 0;  

    $this->selectors = 
    [["index"=>"1","refine"=>"1","filter"=>"1","label"=>"Project Parcel","field"=>"sumLocationDefinitions","table"=>"sumLocationDefinitions","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 = 'frmLocationDefinition') ";
    $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 = 'frmPartFeature') ";
    $this->pfform_data = $dal->query($sql);    

    $this->parts_hdr = ["0","Part Name","Map Id","Land Use","Location"];

    $this->features_hdr = ["0","Feature","Feature Type","Location in Part"];

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

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, direction AS text " .
           "FROM cardinal ORDER BY id " ;
    $this->cardinals = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Land Use' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM landuse " ;
    $this->land_uses = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Geomorphological Feature' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM geomorphology " ;
    $this->geomorphologies = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Named Feature' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM namedfeature " ;
    $this->named_features = $dal->query($sql);

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

    $default = array ("id"=>0,
        "projectparcel_fk"=>0, "projectparcel_fk_text"=>"",
        "name"=>"",            "name_text"=>"",
        "longaxis"=>"",        "longaxis_text"=>"",
        "shortaxis"=>"",       "shortaxis_text"=>"",
        "location"=>"",        "location"=>"",
        "longdirection_id"=>0, "longdirection_id_text"=>"",
        "mapareaid"=>"",       "mapareaid_descr_text"=>"",
        "topography_id"=>0,    "topography_id_text"=>"",
        "landuse_id"=>0,       "landuse_id_text"=>"",
        "geomorphology_id"=>0, "geomorphology_id_text"=>"",
        "namedfeature_id"=>0,  "topography_id_text"=>"",
        "descr"=>"",           "descr_text"=>"");
    $this->ldform_def0 = $this->toObject ($default);

    $default = array ("id"=>0, "fkid"=>0,
      "location"=>"", "location_text"=>"");
    $this->pfform_def0 = $this->toObject ($default);

    $blank = array
      (array ("ph_id"=>0, "bound_id"=>0, "descr"=>""));
    $this->image_blank = $this->toObject ($blank);
    
    $sql = "SELECT BO.id, BO.cardinal_id AS selValue, BO.descr AS inpValue " .
           "FROM bound BO, part P " .
           "WHERE P.spatialunit_id = 0 AND BO.part_id = P.id " ;
    $this->bounds = $dal->query($sql);

  }

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

?>
