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

class modWorkdayOperation {

public $pw_id;
public $pa_id;
public $pr_id;
public $fraction_list;
public $minute_list;
public $start_range;
public $duration_range;
public $starttime;
public $pwoperations;
public $operations;
public $no_operation;
public $in_operation;
public $no_observe;
public $in_observe;
public $op_types;
public $form_defs;

  public function __construct($pr_id, $pa_id, $pw_id, $pwo_id) {

    $this->pr_id = isset($pr_id) ? $pr_id : 0;
    $this->pa_id = isset($pa_id) ? $pa_id : 0;
    $this->pw_id = isset($pw_id) ? $pw_id : 0;
    $this->pwo_id = isset($pwo_id) ? $pwo_id : 0;

print_r ("id: $this->pr_id");      
    $this->fraction_list  = "00,25,50,75";
    $this->minute_list    = "00,15,30,45";
    $this->start_range    = "00,24";
    $this->duration_range = "00,09";
    $starttime      = "08:30";

    $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 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 = 'frmOperationDefinition') ";
    $this->odform_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 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 = 'frmHighgranularity') ";
    $this->hgform_data = $dal->query($sql); 

    $sql = "SELECT PWO.id, OP.name AS 'Operation Name', " .
           "(CASE WHEN OT.id IS NULL THEN '' ELSE OT.descr END) AS 'Operation Type', " .
           "(CASE WHEN PWO.starttime ='00:00' THEN '' 
                  ELSE PWO.starttime END) AS 'Start Time', " .
           "(CASE WHEN PWO.duration = '0.00' THEN '' 
                  ELSE PWO.duration END) AS 'Duration' " .
           "FROM projectworkdayoperation PWO, operation OP " .
           "LEFT JOIN operationtype OT ON OT.id = OP.operationtype_id " .
           "WHERE PWO.projectworkday_id = $this->pw_id AND OP.id = PWO.operation_id " .
           "ORDER BY starttime " ;
    $this->operations = $dal->query($sql);

    if (count($this->operations) == 0) {

      $default = array('1' => array("id"=>0, "Operation Name"=>"",
        "Operation Type"=>"", "Start Time"=>"", "Duration"=>""));
      $this->operations = $this->toObject ($default);
    }
// "(CASE WHEN (PW.id = $this->pw_id) THEN 'disabled' ELSE '' END) AS attribute " .

    $sql = "SELECT OP.id, OP.name AS descr " .
           "FROM projectworkdayoperation PWO, operation OP " .
           "WHERE PWO.projectworkday_id = $this->pw_id AND OP.id = PWO.operation_id " .
           "ORDER BY OP.name " ;
    $this->operation_names = $dal->query($sql);

    $sql = "SELECT PWP.id, " .
           "CASE WHEN " .
              "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
                "THEN CONCAT(surname,', ',firstname) " . 
                "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
           "END AS text " .
           "FROM  (personnel PE, projectworkdaypersonnel PWP) " .
           "LEFT JOIN projectworkdayoperationpersonnel POP " .
           "ON (POP.projectworkdaypersonnel_id = PWP.id AND POP.projectworkdayoperation_id  = 0) " .
           "WHERE PWP.projectworkday_id = $this->pw_id AND PE.id = PWP.personnel_id AND POP.id IS NULL " .
           "ORDER BY text ";
    $this->no_operation = $dal->query($sql);

    $sql = "SELECT PWP.id, " .
           "CASE WHEN " .
              "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
                "THEN CONCAT(surname,', ',firstname) " . 
                "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
           "END AS text " .
           "FROM personnel PE, projectworkdaypersonnel PWP, " .
           "projectworkdayoperationpersonnel POP, projectworkdayoperation PWO " .
           "WHERE PWP.projectworkday_id = $this->pw_id AND POP.projectworkdayoperation_id = 0 " .
           "AND PE.id = PWP.personnel_id " .
           "ORDER BY text ";
    $this->in_operation = $dal->query($sql);

    $sql = "SELECT POB.id, " .
           "CASE WHEN " .
             "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
               "THEN CONCAT(surname,', ',firstname) " . 
               "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
           "END AS text " .
           "FROM (contact CO, projectworkdayobserver POB) " . 
           "LEFT JOIN projectworkdayoperationobserver POO " .
           "ON (POO.projectworkdayobserver_id = POB.id AND POO.projectworkdayoperation_id = 0) " .
           "WHERE POB.projectworkday_id = $this->pw_id AND CO.id = POB.contact_id " .
           "AND POO.id IS NULL " .
           "ORDER BY text ";
    $this->no_observe = $dal->query($sql);

    $sql = "SELECT POB.id, " .
           "CASE WHEN " .
             "(LENGTH(surname) < 10 && LENGTH(firstname) < 10) " . 
               "THEN CONCAT(surname,', ',firstname) " . 
             "ELSE CONCAT(surname,', ',SUBSTRING(firstname,1,1)) " . 
           "END AS text " .
           "FROM contact CO, projectworkdayobserver POB, projectworkdayoperationobserver POO " .
           "WHERE POB.projectworkday_id = $this->pw_id AND CO.id = POB.contact_id " .
           "AND POO.projectworkdayobserver_id = POB.id AND POO.projectworkdayoperation_id = 0 " .
           "ORDER BY text ";
    $this->in_observe = $dal->query($sql);

    $sql = "SELECT id, descr AS text FROM operationtype WHERE active = 1";
    $this->op_types = $dal->query($sql);
    
    $sql = "SELECT id, linename, units FROM activitybudget WHERE project_id = $this->pr_id";
    $this->budget_lines = $dal->query($sql);
    

    $blank = array ("id"=>0,       "fkid"=>$this->pw_id,
          "name_id"=>0,            "name_id_text"=>"",    
          "operationtype_id"=>0,   "operation_id_text"=>"", 
          "starttime"=>"00:00",    "starttime_text"=>"", 
          "breakduration"=>"0.00", "breakduration_text"=>"",
          "stoptime"=>"00:00",     "stoptime_text"=>"", 
          "note"=>"",              "note_text"=>"");
    $this->odform_def0 = $this->toObject ($blank);
    
  }

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

}

?>
