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

class modParcelFop {

private $pa_id;
private $form_defs;
private $parcel_fops;

  public function __construct($lo_id, $pw_id, $pa_id) {
    
    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->pw_id = isset($pw_id) ? $pw_id : 0;
    $this->pa_id = isset($pa_id) ? $pa_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 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 = 'frmParcelFop') ";
    $this->form_data = $dal->query($sql);    

    $sql = "SELECT PF.id, FO.name AS 'Feature Name', " .  
           "(CASE WHEN FT.descr IS NULL THEN '' ELSE FT.descr END) AS Type, " .
           "(CASE WHEN PF.fieldefined = 1 " . 
             "THEN 'Field' ELSE 'Office' END) AS Defined, " .
           "FO.mapareaid AS 'Map Id', " .
           "(CASE WHEN PF.fieldefined = 1 " .
           "THEN PF.field_descr ELSE PF.office_descr END) AS Description " .
           "FROM (parcelfop PF, fop FO)  " .
           "LEFT JOIN foptype FT ON FT.id = FO.foptype_id " .
           "WHERE PF.parcel_id = $this->pa_id AND FO.id = PF.fop_id " .
           "ORDER BY FO.name " ; 
    $this->parcel_fops = $dal->query($sql);
    
//$cnt = count($this->parcel_fops);
//print_r ("mcnt:$cnt ");    
    if (count($this->parcel_fops) == 0) {

      $default = array('1' => array("id"=>0, "Feature Name"=>"", "Type"=>0, 
                 "Defined"=>"", "Map Id"=>"", "Description"=>"",));
      $this->parcel_fops = $this->toObject ($default);
    }

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

    $default = array ("id"=>0, "fkid"=>$this->pa_id,
           "name_id"=>0,       "name_id_text"=>"",
           "foptype_id"=>0,    "foptype_id_text"=>"",
           "mapareaid"=>"",    "mapareaid_text"=>"",
           "note"=>"",         "note_text"=>"",
           "field_descr"=>"",  "field_descr_text"=>"",
           "office_descr"=>"", "office_descr_text"=>"",
           "fielddefined"=>0,  "fielddefined_text"=>"No");
    $this->form_defs = $this->toObject ($default);
    $default = array ("ph_id"=>0, "bound_id"=>0, "descr"=>"");
    $this->image_blank = $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;
  }
}

?>
