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

class modSiteVisit {
    private $pa_id;

  public function __construct($lo_id, $pw_id, $pp_id, $os_id) {

    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->pw_id = isset($pw_id) ? $pw_id : 0;  
    $this->pp_id = isset($pp_id) ? $pp_id : 0;  
    $this->os_id = isset($os_id) ? $os_id : 0;  

//print_r("os:$this->os_id");
    $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 = 'frmSiteVisit') ";
    $this->svform_data = $dal->query($sql); //Generates/populates local storage.
   
    $sql = "SELECT OS.id, SU.name AS 'Part Name', SU.mapareaid AS 'Map ID', " .
           "LU.descr AS 'Land Use', " .
           "(CASE WHEN UC.groundcovertype_id IS NULL " .
           "THEN '' ELSE GC.descr END) AS 'Ground Cover', " .
           "(CASE WHEN AG.modification IS NULL " .
           "THEN AP.descr ELSE AG.modification END) AS 'Assessment Strategy' " .
           "FROM (projectworkday PW, projectworkdayoperation PWO, " .
           "operationspatialunit OS, spatialunit SU, part P, landuse LU) " .
           "LEFT JOIN (unitcondition UC, groundcovertype GC, " .
           "assesstrategy AG, assesstype AP) " .
           "ON (UC.operationspatialunit_id = OS.id " .
           "AND UC.groundcovertype_id = GC.id " .
           "AND AG.operationspatialunit_id = OS.id " .
           "AND AP.id = AG.assesstype_id) " . 
           "WHERE PW.parcel_id = $this->pa_id " .
           "AND PWO.projectworkday_id = PW.id " .
           "AND OS.operation_id = PWO.operation_id " .
           "AND SU.id = OS.spatialunit_id " .
           "AND P.spatialunit_id = SU.id AND P.landuse_id = LU.id ";
    $this->site_visits = $dal->query($sql);

    if (count ($this->site_visits) == 0) {
      $default = array('1' => array("id"=>0, "Part Name"=>"", 
                   "Map ID"=>"", "Land Use"=>"", 
                   "Ground Cover"=>"", "Assessment Strategy"=>""));
      $this->site_visits = $this->toObject ($default);
    }

    $sql = "SELECT id, descr AS text FROM assesstype " .
           "WHERE active = 1 ORDER BY id ";
    $this->assessment_types = $dal->query($sql);

    $sql = "SELECT id, descr AS text FROM groundcovertype " .
           "WHERE active = 1 ORDER BY id ";
    $this->ground_covers = $dal->query($sql);
    
    $sql = "SELECT id, descr AS text FROM groundconditiontype " .
           "WHERE active = 1 ORDER BY id ";
    $this->ground_conditions = $dal->query($sql);
    
    $sql = "SELECT id, descr AS text FROM groundvisibility " .
           "WHERE active = 1 ORDER BY id DESC ";
    $this->ground_visibilities = $dal->query($sql);
    
    $sql = "SELECT OS.id,  PW.parcel_id AS fkid, " .
           "SU.name,       '' AS name_text,  " .
           "SU.mapareaid,  '' AS mapareaid_text, " .
           "P.landuse_id,  LA.descr AS landuse_id_text, " .
           "P.location,    '' AS location_text, " .
           "'' AS mapcode, '' AS mapcode_text " . 
           "FROM operationspatialunit OS, spatialunit SU, part P, landuse LA, " .
           "projectworkdayoperation PWO, projectworkday PW " .
           "WHERE OS.id = $this->os_id " .
           "AND PWO.operation_id = OS.operation_id " .
           "AND PW.id = PWO.projectworkday_id " .
           "AND SU.id = OS.spatialunit_id " .
           "AND P.spatialunit_id = SU.id AND LA.id = P.landuse_id " . 
           "LIMIT 1 " ;
    $part = $dal->query($sql);


    $default = array("id"=>$part->id,   "fkid"=>$part->fkid, 
     "name"=>"$part->name",             "name_text"=>$part->name_text, 
     "mapareaid"=>$part->mapareaid,     "mapareaid_text"=>$part->mapareaid_text, 
     "landuse"=>$part->landuse_id_text, "landuse_id_text"=>$part->landuse_id_text, 
     "location"=>$part->location,       "location_text"=>$part->location_text,
     "groundcovertype_id"=>0,           "groundcovertype_id_text"=>"",       
     "groundconditiontype_id"=>0,       "groundconditiontype_id_text"=>"",
     "groundvisibility_id"=>0,          "groundvisibility_id_text"=>"",
     "assesstype_id"=>0,                "assesstype_id_text"=>"", 
     "modification"=>"",                "modification_text"=>"", 
     "rationale"=>"",                   "rationale_text"=>"",
     "mapcode"=>$part->mapcode,         "mapcode_text"=>$part->mapcode_text,  
     "note"=>"",                        "note_text"=>"");
    $this->svform_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;
  }
 
  
}

?>