<?php
require_once DAO . 'ConditionDao.php';
require_once DAO . 'CompensationDao.php';
require_once DTO . 'ConditionDto.php';
require_once INTERNAL . 'NoCache.php';
final class Condition extends Action {
  private $dto;
  public function __construct() {
    $this->checkAccess();
    $this->dto = new ConditionDto();
  }
  protected function getDto() {
    return $this->dto;
  }
  public function index() {
    $dao = new ConditionDao();
    $conditions = $dao->getAll();
    while ($row = mysql_fetch_assoc($conditions)) {
      $this->dto->condition[] = array('name' => $row['condition_name'], 'value' => $row['condition_id']);
    }
    $dao = new CompensationDao();
    $compensations = $dao->getAll();
    while ($row = mysql_fetch_assoc($compensations)) {
      $this->dto->compensation[] = array('name' => $row['compensation_name'], 'value' => $row['compensation_id']);
    }
    return new Forward();
  }
  public function add() {
    header('Content-Type: application/json; charset=utf-8');
    $dao;
    switch ($this->dto->option) {
      case 'condition':
        $dao = new ConditionDao();
        break;
      case 'compensation':
        $dao = new CompensationDao();
    }
    echo '{"' . $dao->insert($this->dto->name) . '":"' . $this->dto->name . '"}';
    return new Direct();
  }
  public function edit() {
    header('Content-Type: application/json; charset=utf-8');
    $dao;
    switch ($this->dto->option) {
      case 'condition':
        $dao = new ConditionDao();
        $dao->update($this->dto->name, $this->dto->conditionId);
        break;
      case 'compensation':
        $dao = new CompensationDao();
        $dao->update($this->dto->name, $this->dto->compensationId);
    }
    echo '{"result":"OK"}';
    return new Direct();
  }
  public function del() {
    header('Content-Type: application/json; charset=utf-8');
    $dao;
    switch ($this->dto->option) {
      case 'condition':
        $dao = new ConditionDao();
        $dao->delete($this->dto->conditionId);
        break;
      case 'compensation':
        $dao = new CompensationDao();
        $dao->delete($this->dto->compensationId);
    }
    echo '{"result":"OK"}';
    return new Direct();
  }
}
// EOF.
