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

class modExcavationRecord {

private $pa_id;
private $pwo_id;

private $culturalunits;
private $prop_part;
private $form_blank;

  public function __construct($lo_id, $pp_id, $pwo_id, $su_id) {
    
    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->pp_id = isset($pp_id) ? $pp_id : 0;
    $this->pwo_id = isset($pwo_id) ? $pwo_id : 0;
    $this->su_id = isset($su_id) ? $su_id : 0;

    $os_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 SS.id, CU.name AS site_name, SU.name AS prop_part, " .
           "CU.descr AS cu_descr, IP.text AS int_descr " .
           "FROM projectworkday PW, projectworkdayoperation PWO, operationspatialunit OS, " . 
           "spatialunit SU, sitespatialunit SS, culturalunit CU " .
           "LEFT JOIN interpret IP ON IP.id = CU.interpret_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 SS.operationspatialunit_id  = OS.id " .
           "AND CU.id = SS.culturalunit_id " .
           "ORDER BY prop_part, site_name ";
    $this->culturalunits = $dal->query($sql);

    $sql = "SELECT OS.id, SU.name AS text " .
           "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_part = $dal->query($sql);
    
    if (null != $this->prop_part) {
      $firstRow = reset($this->prop_part);
      $os_id = $firstRow->id;
    }
    
    $sql = "SELECT CU.id, CU.name AS text, " .
           "CASE WHEN OS.id = $os_id THEN '' ELSE '' END AS attribute " . 
           "FROM projectworkday PW, projectworkdayoperation PWO, operationspatialunit OS, " . 
           "spatialunit SU, sitespatialunit SS, culturalunit CU " .  
           "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 SS.operationspatialunit_id  = OS.id " .
           "AND CU.id = SS.culturalunit_id " ;
    $this->site_names = $dal->query($sql);
 
    if (count($this->culturalunits) == 0) {

      $default = array('1' => array("id"=>0, "site_name"=>"", "prop_part"=>"",
                                    "cu_descr"=>"", "int_descr"=>""));
      $this->culturalunits = $this->toObject ($default);
    }

    
    $sql = "SELECT SU.id, SU.name AS test_pit_name, space(1) AS lots, space(1) AS total_artifacts " .
           "FROM operationspatialunit OS, spatialunit SU " .
           "WHERE OS.projectworkdayoperation_id = $this->pwo_id " .
           "AND SU.id = OS.spatialunit_id " ;
    $this->ptp_summary = $dal->query($sql);

    if (count($this->ptp_summary) == 0) {
      $default = array('1' => array("id"=>0, "test_pit_name"=>"", "lots"=>"",
                                    "total_artifacts"=>""));
      $this->ptp_summary = $this->toObject ($default);
    }
    
    $sql = "SELECT SL.id, LO.name " .
           "FROM operationspatialunit OS, spatialunitlot SL, lot LO " .
           "WHERE OS.projectworkdayoperation_id = $this->pwo_id " .
           "AND SL.operationspatialunit_id = OS.id AND SL.lot_id = LO.id " ;
    $this->operation_lots = $dal->query($sql);

    $sql = "SELECT SL.id, LO.name AS 'Lot Name', CO.descr AS Composition, " .
           "MC.descr AS Colour, LI.descr as Interpretation " .
           "FROM operationspatialunit OS, spatialunitlot SL, lot LO, " .
           "lotcomposition LC, composition CO, munselcolour MC, lotinterpret LI " .
           "WHERE OS.projectworkdayoperation_id = $this->pwo_id " .
           "AND SL.operationspatialunit_id = OS.id AND SL.lot_id = LO.id " .
           "AND LC.lot_id = LO.id AND CO.id = LC.composition_id " .
           "AND MC.id = LC.munselcolour_id AND LI.id = lotinterpret_id " ;
    $this->lot_summary = $dal->query($sql);

    if (count($this->lot_summary) == 0) {
      $default = array('1' => array("id"=>0, "lot_name"=>"", "Composition"=>"",
                                    "Composition"=>"", "Interpretation"=>"", 
                                    "total_artifacts"=>""));
      $this->lot_summary = $this->toObject ($default);
    } 
    
    $sql = "SELECT SL.id, CONCAT ('Lot ', LO.name) as text " .
           "FROM operationspatialunit OS, spatialunit SU, spatialunitlot SL, lot LO " .
           "WHERE OS.projectworkdayoperation_id = $pwo_id " .
           "AND SU.id = OS.spatialunit_id AND SU.spatialunittype_id = 1 " .
           "AND SL.operationspatialunit_id = OS.id AND LO.id = SL.lot_id " ;
    $this->part_lots = $dal->query($sql);
           

    $sql = "SELECT id, descr " .
           "FROM relationship " ;
    $this->relationships = $dal->query($sql);
    
    $sql = "SELECT id, text as descr " .
           "FROM pointtype " ;
    $this->point_types = $dal->query($sql);

    $sql = "SELECT id, text as descr " .
           "FROM activitytype " ;
    $this->activity_types = $dal->query($sql);
    
    $sql = "SELECT id, direction AS descr " .
           "FROM cardinal " ;
    $this->cardinals = $dal->query($sql);
    
    $sql = "SELECT id, text AS descr " .
           "FROM planshape " ;
    $this->plan_shape = $dal->query($sql);
    
    $sql = "SELECT id, text AS descr " .
           "FROM interpret " ;
    $this->interpret = $dal->query($sql);

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

    $sql = "SELECT id, descr " .
           "FROM recommendrationale WHERE stage = 2 " ;
    $this->rec_rationales = $dal->query($sql);
    
    $sql = "SELECT id, descr " .
           "FROM bordenrationale " ;
    $this->bord_rationales = $dal->query($sql);

    $sql = "SELECT id, mark AS descr " .
           "FROM elevation " ;
    $this->bord_rationales = $dal->query($sql);

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

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

    $values = ["1"=>["id"=>"1", "text"=>"Yes"], "2"=>["id"=>"0", "text"=>"No"]];    
    $this->yes_no = $this->toObject ($values);

    //    $array = 
//      array ("office"=> 
//      array ("checkconf_office"=>"1", "field_descr"=>"0")), 
//      array ("field"=>
//      array ("fop_name"=>"1", "foptype_id"=>"1", "mapareaid"=>"1", "field_descr"=>"1"));
//    $this->form_control = $this->toObject ($array); 

  }

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

?>
