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

class modProject {

  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($pw_id, $pr_id) {
    if (isset($pw_id))
      $this->pw_id = $pw_id;
    else 
      $this->pw_id = 0;

    if (isset($pr_id))
      $this->pr_id = $pr_id;
    else 
      $this->pr_id = 0;
    
    $this->today = date("Y-m-d");
    $this->frac_list = "00,25,50,75";
    $this->min_list  = "00,15,30,45";
    $start_time      = "08:30";
    $end_time        = "17:00";
    $break_dur       = "00:50";

    $dal = new DAL();
 
    $sql = "SELECT PW.id, PR.name AS project, PR.refno AS ref_no, " .
           "PA.name AS parcel, WO.textdate AS workday " .
           "FROM projectworkday PW, workday WO, parcel PA, project PR " .
           "WHERE PR.id = $this->pr_id AND PA.project_id = PR.id AND PW.parcel_id = PA.id " .
           "AND PW.fieldwork_day = 1 AND PW.active = 1 AND WO.id = PW.workday_id " .
           "ORDER BY WO.date DESC";
    $this->workdays = $dal->query($sql);
//$cnt = count($this->workdays);
//print_r ("cnr:$cnt ");    
    if (count($this->workdays) == 0) {

      $default = array('1' => array("pw_id"=>"0", "project"=>"", "ref_no"=>"",
                                    "parcel"=>"", "workday"=>""));
      $this->workdays = $this->toObject ($default);
    }

    $sql = "SELECT PA.id, PA.name as text " .
           "FROM parcel PA " .
           "WHERE PA.project_id  = " .
           "(SELECT PA.project_id " .
           "FROM projectworkday PW, parcel PA " .
           "WHERE PW.id = $this->pw_id AND PA.id = PW.parcel_id) " ;
    $this->parcelList = $dal->query($sql);
  
    $default = array("id"=>0, "project_name"=>"", "rowproject_name"=>0,
                     "parcel_id"=>0, "date"=>$this->today,
                     "start_time"=>$start_time, "end_time"=>$end_time,
                     "break_dur"=>$break_dur);
    $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;
  }
  
}

?>
