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

class modProjectWorkday {

  private $pw_id;
  private $pa_id;
  private $today;
  private $start_time;
  private $end_time;
  private $break_duration;
  private $parcel_id;
  private $parcel_name;
  private $project_id;
  private $project_name;
  private $workday_id;
  private $date;
  private $status;
  private $hour_list;
  private $min_list;
  private $workdays;
  private $form_defs;

  public function __construct($lo_id, $pr_id, $pw_id) {
      
    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->pr_id = isset($pr_id) ? $pr_id : 0;  
    $this->pw_id = isset($pw_id) ? $pw_id : 0;  
//print_r ("$pr_id-:-$pw_id");    
    $this->selectors = 
    [["index"=>"01","route"=>"00","label"=>"Instance","field"=>"sumFieldProjects","sumid"=>$this->pr_id,"selid"=>$this->pw_id,"width"=>"0%","type"=>"FORM","attr"=>""]];    
    

    $today = date("Y-m-d");
    $today_text = date("D M j, Y");
    $this->min_list  = "00,15,30,45";
    $this->frac_list = "00,25,50,75";
    $startime        = "08:30";
    $stoptime        = "17:00";
    $breakdur        = "00.50";

    $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 = 'label' " .
           "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 = 'frmFieldProject') ";
    $this->form_data = $dal->query($sql);  
    
    $this->summary_hdr = ["0","Project","Ref No","Parcel","Login","Workday","Time"];

    $default = array("id"=>0,        "fkid"=>$this->pr_id,
              "project_id"=>0,       "project_id_text"=>"",
              "projectparcel_id"=>0, "projectparcel_id_text"=>"",
              "date"=>$today,        "date_text"=>$today_text,
              "startime"=>$startime, "starttime_text"=>$starttime,
              "stoptime"=>$stoptime, "stoptime_text"=>$stoptime,
              "breakdur"=>$breakdur, "breakdur_text"=>$breakdur,
              "note"=>"",            "note_text"=>"");
    $this->form_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;
  }
  
}

?>
