<?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($pw_id, $pa_id, $pr_id) {

    if (isset($pw_id))
      $this->pw_id = $pw_id;
    else    
      $this->pw_id = 0;

    if (isset($pa_id))
      $this->pa_id = $pa_id;
    else    
      $this->pa_id = 0;

    if (isset($pr_id))
      $this->pr_id = $pr_id;
    else    
      $this->pr_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";
    $this->starttime      = "08:30";

    $dal = new DAL();

    $sql = "SELECT PWO.id, OP.name AS operation_name, OT.descr AS operation_type, " .
           "PWO.starttime AS 'start_time', PWO.op_duration 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->pwoperations = $dal->query($sql);

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

      $default = array('1' => array("id"=>0, "operation_name"=>"", "operation_type"=>"",
                                    "start_time"=>"", "duration"=>""));
      $this->pwoperations = $this->toObject ($default);
    }

    $sql = "SELECT OP.id, OP.name AS descr, (CASE WHEN (PW.id = $this->pw_id) THEN 'disabled' ELSE '' END) AS attribute " .
           "FROM parcel PA, projectworkday PW, projectworkdayoperation PWO, operation OP " .
           "WHERE PA.project_id = $this->pr_id AND PW.parcel_id = PA.id " .
           "AND PWO.projectworkday_id = PW.id AND OP.id = PWO.operation_id " .
           "ORDER BY OP.name " ;
    $this->operations = $dal->query($sql);

    $sql = "SELECT PWP.id, " .
           "CASE WHEN " .
              "(LENGTH(surname) < 10 && LENGTH(first_name) < 10) " . 
                "THEN CONCAT(surname,', ',first_name) " . 
                "ELSE CONCAT(surname,', ',SUBSTRING(first_name,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(first_name) < 10) " . 
                "THEN CONCAT(surname,', ',first_name) " . 
                "ELSE CONCAT(surname,', ',SUBSTRING(first_name,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 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
      (array ("id"=>0, "seloperationname"=>0, "operationtype_id"=>0,  "operationstarttime"=>"$this->starttime",
              "op_duration"=>"00:00", "operationnote"=>""));
    $this->form_defs = $this->toObject ($blank);
    
    $sql = "SELECT OP.id, OP.name AS operation_name, OP.operationtype_id AS operation_type, " .
           "SUM(PWO.duration) AS total_hours, 0.00 AS budget_hours " .
           "FROM parcel PA, projectworkday PW, projectworkdayoperation PWO, " .
           "operation OP, operationtype OT " .
           "WHERE PA.project_id = $this->pr_id AND PW.parcel_id = PA.id " .
           "AND PWO.projectworkday_id = PW.id " .
           "AND OP.id = PWO.operation_id AND OT.id = OP.operationtype_id " .
           "GROUP BY OP.id ";
    $this->ops_dashboard = $dal->query($sql);

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

      $default = array('1' => array("id"=>0, "operation_name"=>"", "operation_type"=>"",
                                    "total_hours"=>"", "budget_hours"=>""));
      $this->ops_dashboard = $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;
  }

}

?>
