<?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, $pwo_id) {
    if (isset($pa_id))
      $this->pa_id = $pa_id;
    else  
      $this->pa_id = 0;
    
    if (isset($pwo_id))
      $this->pwo_id = $pwo_id;
    else  
      $this->pwo_id = 0;

    $dal = new DAL();
      
    $sql = "SELECT SU.id, SU.name, LA.descr, P.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 = $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 AND P.spatialunit_id = SU.id " ;
    $this->spatialunits = $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->prop_parts = $dal->query($sql);
    
//$cnt = count($this->spatialunits);
//print_r ("mcnt:$cnt ");    
    if (count($this->spatialunits) == 0) {

      $default = array('1' => array("su_id"=>0, "name"=>"", "descr"=>"", "location"=>""));
      $this->spatialunits = $this->toObject ($default);
    }
    
    $sql = "SELECT id, descr " .
           "FROM topography " ;
    $this->topographies = $dal->query($sql);

    $sql = "SELECT id, direction AS descr " .
           "FROM cardinal " ;
    $this->cardinals = $dal->query($sql);

    $sql = "SELECT id, descr " .
           "FROM landuse " ;
    $this->land_use = $dal->query($sql);

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

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

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

    $blank = array
      (array ("ph_id"=>0, "bound_id"=>0, "descr"=>""));
    $this->image_blank = $this->toObject ($blank);
    
  }

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

?>
