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

class modDailyJournal  {
  private $baseDir;
  private $clouds;
  private $atmosphere;
  private $precip;
  private $journal;
  private $lw_id;
  private $start_time;
  private $hour_list;
  private $min_list;
  
  public function __construct($lo_id, $lw_id) {

    $this->lo_id = isset($lo_id) ? $lo_id : 0;
    $this->lw_id = isset($lw_id) ? $lw_id : 0;

    $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 = 'frmWeatherLog') ";
    $this->wlform_data = $dal->query($sql);  

    $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 = 'frmDailyJournal') ";
    $this->djform_data = $dal->query($sql);  

    $sql = "SELECT WE.id as id,  start_time AS Time, CL.name AS Cloud, " .
           "AT.descr AS Atmosphere, temperature AS Temperature, " .
           "humidity AS Humidity, LI.descr AS Lighting, " .
           "PR.descr AS Precipitation " .
           "FROM weather WE, cloudcover CL, atmosphere AT, " .
           "precipitation PR, lighting LI " .
           "WHERE WE.loginworkday_id = $this->lw_id AND WE.active = 1 " .
           "AND WE.cloudcover_id = CL.id AND WE.atmosphere_id = AT.id " . 
           "AND WE.precipitation_id = PR.id AND LI.id = WE.lighting_id " .
           "ORDER BY WE.start_time DESC ";
    $this->weather_logs = $dal->query($sql);

    if (count ($this->weather_logs) == 0) {
      $default = array('1' => array("id"=>0, "Time"=>"", "Cloud"=>"",
                 "Atmosphere"=>"", "Temperature"=>"", "Humidity"=>"",
                 "Lighting"=>"", "Precipitation"=>"" ));
      $this->weather_logs = $this->toObject ($default);
    }

    $sql = "SELECT id, journal FROM loginworkday " . 
           "WHERE id = $this->lw_id LIMIT 1";
    $this->journal_notes = $dal->query($sql)->journal;

    $sql = "SELECT id, name AS text FROM cloudcover WHERE active = 1 ";
    $this->cloud_covers = $dal->query($sql);

    $sql = "SELECT id, descr AS text FROM atmosphere WHERE active = 1 ";
    $this->atmospheres = $dal->query($sql);

    $sql = "SELECT id, descr AS text FROM precipitation WHERE active = 1 ";
    $this->precipitations = $dal->query($sql);

    $sql = "SELECT id, descr AS text FROM lighting WHERE active = 1 ";
    $this->light_levels = $dal->query($sql);

    $hours = date("H");
    $minutes = date("i");
    $m = ((($minutes + 7.5)/15 | 0) * 15) % 60;
    $h = (((($minutes/105) + .5) | 0) + $hours) % 24;
    $now = sprintf("%02d", $h) . ':' . sprintf("%02d", $m);

    $this->hour_range = "00,24";
    $this->min_list  = "00,15,30,45";
    
    $default = array("id"=>0,      "sumid"=>$this->lw_id,
            "start_time"=>$now,    "start_time_text"=>$now,
            "cloudcover_id"=>0,    "cloudcover_text_id"=>"",
            "atmosphere_id"=>0,    "atmosphere_text_id"=>"",
            "temperature"=>"",     "temperature_text"=>"",
            "humidity"=>"",        "humidity_text"=>"",
            "lighting_id"=>0,      "lighting_id_text"=>"",
            "precipitation_id"=>0, "precipitation_id_text"=>"");
    $this->wlform_def0 = $this->toObject ($default);

    $default = array("id"=>0,       "fkid"=>$this->lw_id,
                     "journal"=>"", "journal_text"=>"");
    $this->djform_def0 = $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;
  }
 
  
}

?>