<?php

class doc extends client
{
  public function showDocDataByKundenId($kundenId)
  {
    $db                                   = $this->Database;
    $sql                                  = 'SELECT * FROM docs WHERE kunden_id = :kunden_id ORDER BY doc_type ASC, description ASC';
    $db->query($sql);
    $db->bind(':kunden_id', $kundenId);
    return $db->resultset();
  }
  
  public function saveUploadDocs($values, $filename)
  {
    $insertArray                            = array(
        'uuid'                              => $values['uuid'],
        'kunden_uuid'                       => $values['kunden_uuid'],
        'kunden_id'                         => $values['kunden_id'],
        'path_to_doc'                       => $filename,
        'doc_type'                          => $values['doc_type'],
        'description'                       => makeHtmlEntities($values['description']),
        'reg_by'                            => $_SESSION[ALLOW],
        'reg_datetime'                      => date("Y-m-d H:i:s"),
    );
  pdoDbInsert("docs", $insertArray);
  }
  
  public function getDropdownDocType()
  {
    $dropdownArray                          = array(
        'Bericht'                           => 1,
        'Mitarbeiterformular'               => 2,
        'Verfahren'                         => 3,
        'Vertrag'                           => 4,
        'Vorlage'                           => 5,
    );
    return $dropdownArray;
  }
  
  public function getDocType($value)
  {
    switch ($value) {
      case 1:
        return 'Bericht';
        break;
      case 2:
        return 'Mitarbeiterformular';
        break;
      case 3:
        return 'Verfahren';
        break;
      case 4:
        return 'Vertrag';
        break;
      case 5:
        return 'Vorlage';
        break;
      default:
        return 'nicht def.';
    }
  }
  
  public function saveUpdateDocData($values)
  {
    $updateArray                            = array(
        'doc_type'                          => $values['doc_type'],
        'description'                       => makeHtmlEntities($values['description']),
    );
    $whereCause                             = 'uuid = :uuid';
    $whereVal                               = $values['uuid'];
    pdoDbUpdate("docs", $updateArray, $whereCause, $whereVal);
  }
  
  public function showDocsByUuid($uuid)
  {
    $db                                   = $this->Database;
    $sql                                  = 'SELECT * FROM docs WHERE uuid = :uuid';
    $db->query($sql);
    $db->bind(':uuid', $uuid);
    return $db->single();
  }
  
  public function deleteDoc($uuid)
  {
    $db                                   = $this->Database;
    
    $sqlFilePath                          = 'SELECT path_to_doc FROM docs WHERE uuid = :uuid';
    $db->query($sqlFilePath);
    $db->bind(':uuid', $uuid);
    $row                                  = $db->single();
    chmod($row['path_to_doc'],0666);
    unlink($row['path_to_doc']);
    
    $sql                                  = 'DELETE FROM docs WHERE uuid = :uuid';
    $db->query($sql);
    $db->bind(':uuid', $uuid);
    return $db->execute();
  }
}

