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

class modClientContact {

  public function __construct($lo_id) {
      
    $this->lo_id  = isset($lo_id) ? $lo_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 = 'frmSetClientContact') ";
    $this->cdform_data = $dal->query($sql);  

    $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 = 'frmSetOrganization') ";
    $this->odform_data = $dal->query($sql);  

    $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 = 'frmContactAffiliation') ";
    $this->caform_data = $dal->query($sql);  

    $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 = 'frmContactAddress') ";
    $this->adform_data = $dal->query($sql);  

$cnt = count($this->ocform_data);
//print_r ("cnt:$cnt");
    
    $this->summary_hdr = ["0", "Contact Name", 
                          "Contact Organization", "Contact Email"];
  
    $sql = "SELECT id, name AS text FROM organization WHERE active = 1 ";
    $this->organizations = $dal->query($sql);

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

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

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

    $sql = "SELECT 0 AS id, 'Addr Type' AS text " .
           "UNION ALL " .
           "SELECT id, descr AS text FROM addresstype WHERE active = 1 ";
    $this->addr_types = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Street Type' AS text " .
           "UNION ALL " .
           "SELECT abbreviation AS id, abbreviation AS text " .
           "FROM streetype WHERE active = 1 ";
    $this->street_types = $dal->query($sql);

    $sql = "SELECT 0 AS id, 'Direction' AS text " .
           "UNION ALL " .
           "SELECT abbreviation AS id, abbreviation AS text " .
           "FROM cardinal WHERE active = 1 ";
    $this->street_dirs = $dal->query($sql);

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

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

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

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

    $sql = "SELECT licence_no AS id, licence_no AS text " .
           "FROM licence WHERE active = 1 ";
    $this->licences = $dal->query($sql);

    $default = array("id"=>0, "id_text"=>"",
        "fkid"=>0, 
        "surnamefirst"=>"",   "surnamefirst_text"=>"",
        "surname"=>"",        "surname_text"=>"",
        "firstname"=>"",      "firstname_text"=>"",
        "middlename"=>"",     "middlename_text"=>"",
        "title"=>0,           "title_text"=>"");
    $this->cdform_defs = $this->toObject ($default);

    $default = array("id"=>0,    "id_text"=>"",
                    "fkid"=>0, 
                    "name"=>"", "name_text"=>"");
    $this->odform_defs = $this->toObject ($default);

    $default = array("id"=>0, "fkid"=>0, 
        "contact_fk"=>0,      "contact_fk_text"=>"",
        "organization_fk"=>0, "organization_fk_text"=>"",
        "address_fk"=>0,      "address_fk_text"=>"",
        "email"=>"",          "email_text"=>"",
        "note"=>"",           "note_text"=>"",
        "login"=>"",          "login_text"=>"",
        "password"=>"",       "password_text"=>"");
    $this->caform_defs = $this->toObject ($default);

    $default = array("id"=>0,
        "address_id"=>0,       "address_id_text"=>"",
        "organizationcontact_id"=>0, "organizationcontact_id_text"=>"",
        "suite"=>"",          "suite_text"=>"",
//        "addresstype_id"=>0,  "addresstype_id_text"=>"",
        "careof"=>"",         "careof_text"=>"",
        "pobox"=>"",          "pobox_text"=>"",
        "station"=>"",        "station_text"=>"",
        "streetno"=>"",       "streetno_text"=>"",
        "streetname"=>"",     "streetname_text"=>"",
        "streetype"=>"0",     "streetype_text"=>"",
        "streetdir"=>"0",     "streetdir_text"=>"",
        "ruralroute"=>"",     "ruralroute_text"=>"",
        "city"=>"",           "city_text"=>"",
        "province"=>"ON",     "province_text"=>"",
        "postcode"=>"",       "postcode_text"=>"",
        "country"=>"Canada",  "postcode_text"=>"");
    $this->adform_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;
  }
 
  
}

?>