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

class modProjectDefinition {

  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.commit, " .
           "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 = 'frmSetProjectName') ";
    $this->pnform_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.commit, " .
           "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 = 'frmSetOrgContact') ";
    $this->ocform_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.commit, " .
           "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 = 'frmProjectDefinition') ";
    $this->pdform_data = $dal->query($sql);  

    $this->project_hdr = ["0", "RefNo", "Project Name", "Stage",
                         "Contact", "Organization", "Department"];

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT OC.id, CONCAT(CN.firstname, ' ', CN.surname) AS text " .
           "FROM organizationcontact OC, contact CN " .
           "WHERE OC.approver = 1 AND OC.contact_id = CN.id " ;
    $this->approvers = $dal->query($sql);

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

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

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

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

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

    $default = array("id"=>0,  "fkid"=>0,
           "name"=>"",         "name_text"=>"",
           "site_name"=>"", "site_name_text"=>"",
           "refno"=>"",        "refno_text"=>"");
    $this->pnform_defs = $this->toObject ($default);

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

    $default = array("id"=>0,      "fkid"=>0,
      "project_id"=>0,             "project_id_text"=>"",
      "project_fk"=>0,             "project_fk_text"=>"",
      "organizationcontact_fk"=>0, "organizationcontact_fk_text"=>"",
      "contactype_id"=>0,          "contactype_id_text"=>"",
      "descr"=>"",                 "descr_text"=>"",
      "stage_id"=>0,               "stage_id_text"=>"",
      "development_refno"=>"",     "development_refno_text"=>"", 
      "approver_id"=>0,            "approver_text"=>"",
      "department_id"=>0,          "department_id_text"=>"",
      "licence_no"=>0,             "licence_no_text"=>"",
      "note"=>"",                  "note_text"=>"");
    $this->pdform_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;
  }
 
  
}

?>