<?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($pa_id, $op_id, $os_id) {

    $this->pa_id = isset($pa_id) ? $pa_id : 0;
    $this->op_id = isset($op_id) ? $op_id : 0;
    $this->os_id = isset($os_id) ? $os_id : 0;

    $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 = 'frmLocationDefinition') ";
    $this->ldform_data = $dal->query($sql);    

    $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 = 'frmPartFeature') ";
    $this->pfform_data = $dal->query($sql);    

    $sql = "SELECT OS.id, SU.name AS 'Part Name', SU.mapareaid AS 'Map Id', " . 
           "CASE WHEN LA.descr IS NULL " .
             "THEN '' ELSE LA.descr END AS 'Land Use', " .
           "P.location AS Location " .
           "FROM (projectworkday PW, projectworkdayoperation PWO, " .
           "operationspatialunit OS, spatialunit SU, part P) " .
           "LEFT JOIN landuse LA ON LA.id = P.landuse_id " .
           "WHERE PW.parcel_id = $this->pa_id " .
           "AND PWO.projectworkday_id = PW.id " .
           "AND OS.operation_id = PWO.operation_id " .
           "AND OS.spatialunit_id = SU.id " .  
           "AND SU.spatialunittype_id = 1 AND P.spatialunit_id = SU.id " .
           "ORDER BY SU.name ";
    $this->property_parts = $dal->query($sql);

    if (count($this->property_parts) == 0) {
      $default = array('1' => array("id"=>0, "Part Name"=>"", "Map Id"=>"", 
                 "Land Use"=>"", "Location"=>""));
      $this->property_parts = $this->toObject ($default);
    }

    $default = array('1' => array("id"=>0, "Feature Name"=>"",
      "Feature Type"=>"", "Location in Part"=>""));
    $this->su_features = $this->toObject ($default);


    $sql = "SELECT SU.id, SU.name AS text " .
           "FROM projectworkday PW, projectworkdayoperation PWO, " .
           "operation OP, operationspatialunit OS, spatialunit SU " .
           "WHERE PW.parcel_id = $this->pa_id " .
           "AND PWO.projectworkday_id = PW.id AND OP.id = PWO.operation_id " .
           "AND OS.operation_id = OP.id AND OS.spatialunit_id = SU.id " .  
           "AND SU.spatialunittype_id = 1 " ;
    $this->part_names = $dal->query($sql);

//    $sql = "SELECT SU.id, SU.name AS text, " .
//           "CASE WHEN PWO.id = $pwo_id THEN 'disabled' ELSE '' END AS attribute " . 
//           "FROM projectworkday PW, projectworkdayoperation PWO, " .
//           "operationspatialunit OS, spatialunit SU " .
//           "WHERE PW.parcel_id = $pa_id AND PWO.projectworkday_id = PW.id " .
//           "AND OS.projectworkdayoperation_id = PWO.id " .
//           "AND SU.id = OS.spatialunit_id AND SU.spatialunittype_id = 1 " ;
//    $this->part_names = $dal->query($sql);
    
//$cnt = count($this->spatialunits);
//print_r ("mcnt:$cnt ");    

    $sql = "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 id, descr AS text " .
           "FROM landuse " ;
    $this->land_uses = $dal->query($sql);

    $sql = "SELECT id, descr AS text " .
           "FROM geomorphology " ;
    $this->geomorphologies = $dal->query($sql);

    $sql = "SELECT id, descr AS text " .
           "FROM namedfeature " ;
    $this->named_features = $dal->query($sql);

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

    $default = array ("id"=>0, "fkid"=>$this->op_id,
      "name_id"=>0,            "name_id_text"=>"",
      "longaxis"=>0,           "longaxis_text"=>"",
      "shortaxis"=>0,          "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;
  }
}

?>
