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

class modLotComposition {

private $pa_id;
private $pwo_id;
private $lot_list;
private $comp_list;
private $form_blank;
private $no_inclusion;
private $in_inclusion;

  public function __construct($pa_id, $pwo_id, $su_id) {

    $dal = new DAL();

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

      $sql = "SELECT id FROM operationspatialunit " .
             "WHERE spatialunit_id = $su_id AND projectworkdayoperation_id = $pwo_id " .
             "LIMIT 1 " ;
      $this->os_id = $dal->query($sql)->id;
    }  
    else  
      $this->os_id = 0;
      
print_r ("os_id:$this->os_id ");
      
    $sql = "SELECT LO.id, LO.name AS 'Lot', LT.descr AS Type, " .
           "LI.descr as Interpretation " .
           "FROM projectworkday PW, projectworkdayoperation PWO, " .
           "operationspatialunit OS, spatialunitlot SL, lot LO, lotinterpret LI, lottype LT " .
           "WHERE PW.parcel_id = $pa_id AND PWO.projectworkday_id = PW.id " .
           "AND OS.projectworkdayoperation_id = PWO.id AND SL.operationspatialunit_id = OS.id " .
           "AND LO.id = SL.lot_id AND LI.id = LO.lotinterpret_id AND LT.id = LO.lottype_id " .
           "ORDER BY LO.name_seq " ;
    $this->lot_list = $dal->query($sql);
    
    $sql = "" ;
    $this->comp_list = [];
    
//$cnt = count($this->spatialunits);
//print_r ("mcnt:$cnt ");    
    if (count($this->lot_list) == 0) {

      $default = array('1' => array("id"=>0, "lot"=>"", "type"=>"", "interpretation"=>""));
      $this->lot_list = $this->toObject ($default);
    }
    
    $default = array('1' => array("id"=>0, "material"=>"", "colour"=>"", "compaction"=>""));
    $this->comp_list = $this->toObject ($default);

    $sql = "SELECT LO.id, LO.name AS text, " .
           "CASE WHEN PWO.id = $pwo_id THEN 'disabled' ELSE '' END AS attribute " . 
           "FROM projectworkday PW, projectworkdayoperation PWO, operationspatialunit OS, " .
           "spatialunitlot SL, lot LO " .
           "WHERE PW.parcel_id = $pa_id AND PWO.projectworkday_id = PW.id " .
           "AND OS.projectworkdayoperation_id = PWO.id AND SL.operationspatialunit_id = OS.id " .
           "AND LO.id = SL.lot_id ORDER BY LO.name_seq " ;
    $this->lot_select = $dal->query($sql);
    
    $sql = "SELECT OS.id, SU.name AS text " .
           "FROM projectworkday PW, projectworkdayoperation PWO, " .
           "operationspatialunit OS, spatialunit SU " .
           "WHERE PW.parcel_id = $pa_id AND PWO.projectworkday_id = PW.id " .
           "AND OS.projectworkdayoperation_id = PWO.id " .
           "AND SU.id = OS.spatialunit_id AND SU.spatialunittype_id = 1 " ;
    $this->prop_part = $dal->query($sql);
    
    $sql = "SELECT I.id, I.descr AS text " .
           "FROM  inclusion I " .
           "LEFT JOIN (lotcompositioninclusion LCI, lotcomposition LC) " .
           "ON (LCI.inclusion_id = I.id AND LC.id = LCI.lotcomposition_id " .
           "AND LC.lot_id = 0) " .
           "WHERE LC.composition_id IS NULL " .
           "ORDER BY descr ";
    $this->no_inclusion = $dal->query($sql);

    $sql = "SELECT I.id, I.descr AS text " .
           "FROM inclusion I, lotcompositioninclusion LCI, lotcomposition LC " .
           "WHERE LCI.inclusion_id = I.id AND LC.id = LCI.lotcomposition_id " .
           "AND LC.lot_id = 0 " .
           "ORDER BY descr ";
    $this->in_inclusion = $dal->query($sql);

    $sql = "SELECT id, descr " .
           "FROM lotinterpret " ;
    $this->interpretations = $dal->query($sql);

    $sql = "SELECT id, descr " .
           "FROM lottype " ;
    $this->lot_types = $dal->query($sql);

    $blank = array
      (array ("p_id"=>0, "bound_id"=>0, "descr"=>""));
    $this->form_blank = $this->toObject ($blank);

    $blank = array
      (array ("ph_id"=>0, "bound_id"=>0, "descr"=>""));
    $this->image_blank = $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;
  }
}

?>
