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

class modControls  {
  private $baseDir;
  private $controls;
  private $selControl;
  private $selVariable;
  
  
  public function __construct() {
      
    $dal = new DAL();
    $sql = "SELECT id, control, operation FROM controlconfig " . 
	       "WHERE active = 1 ORDER BY id";
    $this->controls = $dal->query($sql);
    $this->selControl = 0;
    $this->selVariable = 0;
    $this->baseDir = basename(__DIR__);    
 
    $sql = "SELECT id, control FROM controlconfig " . 
    $this->controls = $dal->query($sql);
  }
  
  public function get($prop) {
    return $this->$prop;
  }
  
  public function setControl($cc_id, $control, $operation, $usage_notes) {
    $this->modifyControl ($cc_id, $control, $operation, $usage_notes);
  }  

  public function delControl($cc_id) {
    $this->deleteControl ($cc_id);
  }  

  public function setVariable($cv_id, $controlconfig_id, $variable, $setting) {
    $this->modifyVariable ($cv_id, $controlconfig_id, $variable, $setting);
  }  

  public function delVariable($cv_id) {
    $this->deleteVariable ($cv_id);
  }  

  private function modifyControl ($cc_id, $control, $operation, $usage_notes) {
      
    $dal = new DAL();

    if ($cc_id == 0) {
      $sql = "INSERT INTO controlconfig (control, operation, usage_notes) VALUES ('$control', '$operation, '$usage_notes')";
      $this->selControl = $dal->query($sql);
    }  

    else {
      $sql = "UPDATE controlconfig SET control='$control', operation='$operation', usage_notes='$usage_notes' " .
             "WHERE id=$cc_id ";
      $result = $dal->query($sql);
      $this->selControl = $cc_id;
    }
    
    $sql = "SELECT id, control, operation, usage_notes FROM controlconfig " . 
	       "WHERE active = 1 ORDER BY id";
    $this->controls = $dal->query($sql);

  }    

  private function deleteControl ($cc_id) {
      
    $dal = new DAL();

    $sql = "UPDATE controlvars SET active=0 WHERE controlconfig_Id = $cc_id ";
    $result = $dal->query($sql);


    $sql = "UPDATE controlconfig SET active=0 WHERE id=$cc_id ";
    $result = $dal->query($sql);
    
    $sql = "SELECT id, control, operation FROM controlconfig " . 
	       "WHERE active = 1 ORDER BY id";
    $this->controls = $dal->query($sql);

  }    

  private function modifyVariable ($cv_id, $controlconfig_id, $variable, $setting) {
      
    $dal = new DAL();

    if ($cv_id == 0) {
      $sql = "INSERT INTO controlvars (controlconfig_id, variable, setting) VALUES ('$controlconfig_id','$variable', '$setting')";
      $this->selVariable = $dal->query($sql);
      $this->selControl  = $controlconfig_id;
    }  

    else {
      $sql = "UPDATE controlvars SET controlconfig_id=$controlconfig_id, variable='$variable', setting='$setting' " .
             "WHERE id=$cv_id ";
      $result = $dal->query($sql);
      $this->selVariable = $cv_id;
      $this->selControl  = $controlconfig_id;

      $sql = "SELECT id, control, operation FROM controlconfig " . 
	       "WHERE active = 1 ORDER BY id";
      $this->controls = $dal->query($sql);

    }
  }	
	
  private function deleteVariable ($cv_id) {
    $dal = new DAL();
    $sql = "UPDATE controlvars SET active=0 WHERE id = $cv_id ";
    $result = $dal->query($sql);
  }
}

?>