<?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($lo_id, $pw_id, $pp_id, $pwo_id, $su_id, $cu_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->pwo_id = isset($pwo_id) ? $pwo_id : 0;  
    $this->su_id  = isset($su_id) ? $su_id : 0;  
    $this->cu_id  = isset($cu_id) ? $cu_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 = "DELETE FROM controlcontexttmp WHERE login_id = $this->lo_id ";
    $this->result = $dal->query($sql);

    $sql = "INSERT controlcontexttmp (login_id, summaryid, selectid, field) VALUES " .
           "($this->lo_id, $pw_id, $pp_id,  'fkyProjectParcel'), " .
           "($this->lo_id, $pp_id, $pwo_id, 'fkyDailyOperation'), " .
           "($this->lo_id, $pp_id, $su_id,  'fkyPropertyPart'), " .
           "($this->lo_id, $pp_id, $cu_id,  'sumSiteDefinition');  ";
    $this->result = $dal->query($sql);

    $sql = "SELECT CC.field, label, idx, filter, refine, " .
           "targetform, summaryid, selectid, width, type, attr " .
           "FROM menuitem MI, controlcontext CC, controlcontexttmp CT " .
           "WHERE MI.path = 'sitedefinition' AND CC.menuitem_id = MI.id " .
           "AND CT.field = CC.field AND CT.login_id = $this->lo_id " .
           "ORDER by idx DESC";
    $this->selectors = $dal->query($sql);

    // site definition 
    $this->sdform_data = formData ();    
    $this->sdform_seq  = formSeq ();    
 
    // set site name
    $this->snform_data = $dal->query($sql);    
    $this->snform_seq  = formSeq ();    

    // property part
    $this->ppform_data = $dal->query($sql);    
    $this->ppform_seq  = formSeq ();    

    // shovel text
    $this->stform_data = $dal->query($sql);
    $this->stform_seq  = formSeq ();    
    
    // pedestrian
    $this->psform_data = $dal->query($sql);
    $this->spsform_seq  = formSeq ();    
    
    // ptp intensification
    $this->piform_data = $dal->query($sql);    
    $this->piform_seq  = formSeq ();    

    // ptp lot
    $this->plform_data = $dal->query($sql);    
    $this->plform_seq  = formSeq ();    

    $this->sitedef_hdr = ["0", "Site Name", "Property Part", "Method", 
                          "Description", "Interpretation"];

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, name AS text " .
           "FROM lot WHERE projectparcel_id = $pp_id " ;
    $this->parcel_lots = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM intensivecollection WHERE active = 1 " ;
    $this->collections = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, value AS text " .
           "FROM intensiveinterval WHERE active = 1 " ;
    $this->intervals = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM intensiveobservation WHERE active = 1 " ;
    $this->observations = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM intensivestation WHERE active = 1 " ;
    $this->stations = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, value AS text " .
           "FROM intensivexpansion WHERE active = 1 " ;
    $this->expansions = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM relationship " ;
    $this->relationships = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, model AS text FROM device WHERE active = 1 ";
    $this->devices_lu = $dal->query($sql);
    
    $sql = "SELECT 0 AS id, 'Select' 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 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM activitytype " ;
    $this->activity_types = $dal->query($sql);

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

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

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, direction AS text " .
           "FROM cardinal " ;
    $this->cardinals = $dal->query($sql);
    
    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM planshape " ;
    $this->plan_shape = $dal->query($sql);
    
    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM topography " ;
    $this->topographies = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM bordenrationale " ;
    $this->bord_rationales = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text, 0 AS type " .
           "UNION ALL " .
           "SELECT 1 AS id, 'Add' AS text, 0 AS type " . 
           "UNION ALL " .
    $sql = "SELECT 1 AS id, 'Found' AS text, 1 AS type " .
           "UNION ALL " .
           "SELECT -1 AS id, 'Delete' AS text, 1 AS type " ;
    $this->rr_functions = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text, 0 AS type " .
           "UNION ALL " .
           "SELECT 1 AS id, 'Add' AS text, 0 AS type " . 
           "UNION ALL " .
    $sql = "SELECT 1 AS id, 'Entry' AS text, 1 AS type " .
           "UNION ALL " .
           "SELECT -1 AS id, 'Delete' AS text, 1 AS type " ;
    $this->con_func = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM recommendrationale " ;
    $this->rr_options = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text, 0 AS type " .
           "UNION ALL " .
           "SELECT 1 AS id, 'Add' AS text, 0 AS type " . 
           "UNION ALL " .
    $sql = "SELECT 1 AS id, 'Found' AS text, 1 AS type " .
           "UNION ALL " .
           "SELECT -1 AS id, 'Delete' AS text, 1 AS type " ;
    $this->si_functions = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text " .
           "FROM affiliation WHERE active = 1 " ;
    $this->affiliations = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text, 0 AS filter " .
           "UNION ALL " .
           "SELECT AR.id, AR.descr AS text, IT.affiliation_id AS filter " .
           "FROM archfunction AR, interpret IT " .
           "WHERE IT.culturalunitype_id = 4 AND AR.id = IT.archfunction_id " . 
           "AND AR.active = 1 " ;
    $this->archfunctions = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "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"=>"",
                "projectparcel_id"=>0,  "projectparcel_id_text"=>"",
                "dailyoperation_id"=>0, "dailyoperation_id_text"=>"",
                "propertypart_id"=>0,   "propertypart_id_text"=>"",
                "complete_ind"=>0,      "complete_ind_text"=>"");
	$this->snform_def = $this->toObject ($default);

    $default = array ("id"=>0,     "fkid"=>0,
       "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"=>"",
       "recommended"=>0,           "recommended_text"=>"");
	$this->sdform_def = $this->toObject ($default);

    $default = array ("id"=>0,
       "projectparcel_fk"=>0,  "projectparcel_fk_text"=>"", 
       "sitedefinition_fk"=>0, "sitedefinition_fk_text"=>"", 
       "dailyoperation_fk"=>0, "dailyoperation_fk_text"=>"", 
       "name"=>"",             "name_text"=>"",
       "mapid"=>"",            "mapid_text"=>"",
       "device_id"=>0,         "device_id_text"=>"",
       "pos"=>"",              "pos_text"=>"");
    $this->stform_def = $this->toObject ($default);

    $default = array ("id"=>0,  "fkid"=>0,
         "lot_id"=>0,         "lot_id_text"=>"",
         "interval_id"=>0,    "interval_id_text"=>"",
         "expansion_id"=>0,   "expansion_id_text"=>"",
         "observation_id"=>0, "observation_id_text"=>"",
         "collection_id"=>0,  "collection_id_text"=>"",
         "station_id"=>0,     "station_id_text"=>"",
         "note"=>"",           "note_id_text"=>"");
    $this->piform_def = $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_def = $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_def = $this->toObject ($default);

  }

  public function get($prop) {
    return $this->$prop;
  }

  private function formData($fh_id) {
    $dal = new DAL();
    $sql = "SELECT field, keytype, dbcolumn, label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "mandatory, uniquekey, confirm, commit, message " .
           "FROM controlfield WHERE formhandler_id = $fh_id ";
    $result = $dal->query($sql);

    $object = new stdClass();
    foreach ($result as $key => $value) {
      $key = $value->field;
      $object->$key = $value;
    }
    return json_encode($object);
  }    

  private function formSeq($fh_id) {
    $dal = new DAL();
    $sql = "SELECT sender AS field, receiver, comkey " .
           "FROM controlsequent " .
           "WHERE formhandler_id = $fh_id " .
           "ORDER BY field ";
    $result = $dal->query($sql);

    $object = new stdClass();
    $key = "";
    $currField = '';
    $sequents = [];
    foreach ($result as $key => $value) {
      if ($currField != $value->field) {
        $key = $currField;
        if ($currField != "")
          $object->$currField = $sequents;
        $sequents = [];
      }
      $sequents[] = $value;
      $currField = $value->field;
    }
    if ($currField != "")
      $object->$currField = $sequents;
    
    return json_encode($object);
  }    

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

?>
