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

class modSiteDefinition {

private $pa_id;
private $pwo_id;

private $site_defs;
private $prop_part;
private $form_blank;

  public function __construct($pa_id, $os_id) {
    
    $this->pr_id  = isset($pr_id) ? $pr_id : 0;  
    $this->pp_id  = isset($pp_id) ? $pp_id : 0;  
    $this->pwo_id = isset($pwo_id) ? $pwo_id : 0;  

    $this->selectors = 
    [["label"=>"Parcel","fkey"=>"projectworkday_fk","sumid"=>$pr_id,"selid"=>$pp_id,"width"=>"34%","type"=>"SELECT"], 
     ["label"=>"Operation","fkey"=>"parcel_fk","sumid"=>$pp_id,"selid"=>$pwo_id,"width"=>"33%","type"=>"SELECT"]];

    $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 = 'label' " .
           "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 = 'frmSiteDefinition') ";
    $this->sdform_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 = 'label' " .
           "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 = 'frmSiteNaming') ";
    $this->snform_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 = 'label' " .
           "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 = 'frmPropertyPart') ";
    $this->ppform_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 = 'label' " .
           "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 = 'frmShovelPTP') ";
    $this->spform_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 = 'label' " .
           "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 = 'frmPedestrianSurvey') ";
    $this->psform_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 = 'label' " .
           "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 = 'frmPTPIntensification') ";
    $this->piform_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 = 'label' " .
           "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 = 'frmPTPLot') ";
    $this->plform_data = $dal->query($sql);    

    $this->sitedef_hdr = ["0", "Site Name", "Property Part", "Method", 
                          "Description", "Interpretation"];
//print_r ("model tableHeader:");        
//print_r ($this->sitedef_hdr);

//    $sql = "SELECT OS.id, SU.name AS text " .
//           "FROM operationspatialunit OS, spatialunit SU " .
//           "WHERE OS.id = $this->os_id AND SU.id = OS.spatialunit_id " ;
//    $this->prop_parts = $dal->query($sql);
//$cnt = count($this->prop_parts);
//print_r ("ppcnt=$cnt");
    

//    $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);

//    $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);

    $default = array('1' => array("id"=>0, "Test-pit Name"=>"", "Lots"=>"",
                                    "Total Artifacts"=>""));
    $this->ptp_summary = $this->toObject ($default);

//    $sql = "SELECT LO.id, LO.name AS text " .
//           "FROM projectworkday PW, projectworkdayoperation PWO, " .
//           "operationspatialunit OS, spatialunitlot SL, lot LO " .
//           "WHERE PW.parcel_id = $this->pa_id " .
//           "AND PWO.projectworkday_id = PW.id " .
//           "AND PWO.operation_id = OS.operation_id " .
//           "AND SL.operationspatialunit_id = OS.id " .
//           "AND LO.id = SL.lot_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);

    $default = array('1' => array("id"=>0, "Lot Name"=>"", "Composition"=>"",
                       "Colour"=>"", "Interpretation"=>"", "Artifacts"=>""));
    $this->lot_summary = $this->toObject ($default);

//    $sql = "SELECT 0 AS id, 'Select' AS text " .
//           "UNION ALL " .
//           "SELECT SL.id, CONCAT ('Lot ', LO.name) as text " .
//           "FROM projectworkday PW, projectworkdayoperation PWO, " .
//           "operationspatialunit OS, spatialunitlot SL, lot LO " .
//           "WHERE PW.parcel_id = $this->pa_id " .
//           "AND PWO.projectworkday_id = PW.id " .
//           "AND PWO.operation_id = OS.operation_id " .
//           "AND SL.operationspatialunit_id = OS.id " .
//           "AND LO.id = SL.lot_id ORDER BY text " ;
//    $this->parcel_lots = $dal->query($sql);

    $sql = "SELECT id, descr AS text " .
           "FROM relationship " ;
    $this->relationships = $dal->query($sql);
    
    $sql = "SELECT 0 AS id, 'Type' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text FROM pointype WHERE active = 1 " .
           "UNION ALL " .
           "SELECT -1 AS id, 'Delete' AS text ";
    $this->point_types = $dal->query($sql);

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

    $sql = "SELECT id, descr AS text " .
           "FROM artifactdensity WHERE active = 1 " ;
    $this->art_densities = $dal->query($sql);

    $sql = "SELECT id, descr AS text " .
           "FROM intensificationtype WHERE active = 1 " ;
    $this->intensif_types = $dal->query($sql);

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

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

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

    $sql = "SELECT id, elevmark_id AS text " .
           "FROM elevation " ;
    $this->elevations = $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"=>"Yes", "text"=>"Yes"], "2"=>["id"=>"No", "text"=>"No"]];    
    $this->yes_no = $this->toObject ($values);

    $default = array ("id"=>0,        "fkid"=>0,
                "name"=>"",           "name_text"=>"",
                "propertypart_fk"=>0, "propertypart_fk_text"=>"");
    $this->snform_defs = $this->toObject ($default);

    $default = array ("id"=>0,        "fkid"=>0,
               "name"=>"",            "name_text"=>"",
               "projectparcel_fk"=>$this->pa_id, "projectparcel_fk_text"=>"");
    $this->ppform_defs = $this->toObject ($default);

    $default = array ("id"=>0,     "fkid"=>$this->os_id,
       "descr"=>"",                "descr_text"=>"",
       "mapareaid"=>"",            "mapareaid_text"=>"",
       "location"=>"",             "location_text"=>"",
       "longaxis"=>"0.000",        "longaxis_text"=>"", 
       "shortaxis"=>"0.000",       "shortaxis_text"=>"",
       "orientation_id"=>0,        "orientation_id_text"=>"",
       "planshape_id"=>0,          "planshape_id_text"=>"",
       "interpret_id"=>0,          "interpret_id_text"=>"",
       "rationale"=>"",            "rationale_text"=>"",
       "note"=>"",                 "note_text"=>"",
       "topography_id"=>0,         "topography_id_text"=>"",
       "bordenizable"=>"No",       "bordenizable_text"=>"",
       "bordenrationale_id"=>0,    "bordenrationale_id_text"=>"",
       "stg2recommendation"=>No,   "stg2recommendation_text"=>"",
       "recommendrationale_id"=>0, "recommendrationale_id_text"=>"");
	$this->sdform_defs = $this->toObject ($default);

    $default = array ("id"=>0, "fkid"=>0,
               "name"=>"",     "name_text"=>"",
               "mapareaid"=>"", "mapareaid_text"=>"");
    $this->spform_defs = $this->toObject ($default);

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

    $default = array ("id"=>0, "fkid"=>0,
                 "lot_id"=>0,             "lot_id_text"=>"", 
                 "name_id"=>0, "name_id_text"=>"", 
                 "value"=>"",  "value_text"=>"",
                 "pos"=>"",    "pos_text"=>"", 
                 "mark"=>0,   "mark_text"=>"");
    $this->plform_defs = $this->toObject ($default);

    $default = array ("id"=>0, "fkid"=>0,
      "lot_id"=>0,             "lot_id_text"=>"", 
      "artifactdensity_id"=>0, "artifactdensity_id_text"=>"", 
      "activitytype_id"=>0,    "activitytype_id_text"=>"", 
      "descr"=>"",             "descr_text"=>"", 
      "rationale"=>"",         "rationale_text"=>""); 
    $this->psform_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;
  }
}

?>
