/**
  * @noAuth
  * @url POST /?documents
  * @url PUT /?documents/$id
  */
 function insertDocument($id = null)
 {
     //validate post data
     $validator = new Validator($_POST);
     $errors = $validator->validate(array('title' => VALIDATE_RULE_REQUIRED | VALIDATE_RULE_NON_EMPTY_STRING, 'author' => VALIDATE_RULE_REQUIRED | VALIDATE_RULE_NON_EMPTY_STRING, 'published' => VALIDATE_RULE_YEAR, 'keywords' => VALIDATE_RULE_REQUIRED, 'isbn' => VALIDATE_RULE_ISBN));
     if (!empty($errors)) {
         throw new RestException(400, implode(" ", $errors));
     }
     //change string cases
     $_POST['title'] = ucfirst($_POST['title']);
     $_POST['author'] = ucwords($_POST['author']);
     $_POST['keywords'] = strtolower($_POST['keywords']);
     // submit new entry and upload file
     if ($id == null) {
         if (!isset($_FILES['file']) || empty($_FILES['file']['name'])) {
             throw new RestException(400, 'No File submitted');
         }
         $file = $_FILES['file'];
         if ($file['size'] < 1 || $file['size'] > UPLOAD_FILE_MAX_SIZE) {
             throw new RestException(400, "File is too large, maximum file size is " . strval(UPLOAD_FILE_MAX_SIZE / 8 / 1024 / 1024) . " MB.");
         }
         // append filename to post data and insert in database
         $db = new DocumentsDatabase();
         $_POST['file'] = $file['name'];
         $db->insertDocument($_POST);
         //upload file
         $id = $db->lastInsertRowid();
         $upload_dir = DIR_RECORD_FILES . '/' . $id;
         try {
             checkFileType($file['name']);
             uploadFile($file['tmp_name'], $upload_dir, $file['name']);
         } catch (Exception $e) {
             // delete entry if upload failed
             $db->deleteDocument($id);
             throw new RestException(400, $e->getMessage());
         }
         return $db->getDocument($id);
         // modify entry
     } else {
         //insert Model and return it
         $db = new DocumentsDatabase();
         $db->insertDocument($_POST);
         return $db->getDocument($id);
     }
 }
$rows = $db->listDocuments();
var_dump($rows);
// list all topics
echo "<h2>ALL TOPICS</h2>";
$rows = $db->listTopics();
var_dump($rows);
echo "<h2>DELETE TOPIC</h2>";
$db->deleteTopic(4);
$rows = $db->listTopics();
var_dump($rows);
echo "<h2>DELETED 5th DOCUMENT</h2>";
$db->deleteDocument(5);
$rows = $db->listDocuments();
var_dump($rows);
echo "<h2>GET THIRD DOCUMENT</h2>";
$rows = $db->getDocument(3);
var_dump($rows);
echo "<h2>CHANGE TITLE</h2>";
$db->insertDocument(array("id" => 1, "title" => "The Hitchhikers Guide to the Galaxy (2)", "author" => "Adams, Douglas", "description" => "Aenean varius quam at quam convallis, id porta nulla luctus. Nullam a tincidunt odio, sit amet mollis elit. Pellentesque et tristique enim. Aliquam vel dolor et lectus venenatis varius at aliquam nulla. Integer vehicula nibh justo, aliquet feugiat ex maximus et. Curabitur ex velit, commodo", "keywords" => "scifi fantasy comedy", "topic_id" => 42, "published" => 1979, "language" => "German", "file" => "hitchhiker.pdf"));
$rows = $db->getDocument(1);
var_dump($rows);
echo "<h1>SETTINGS DB</h1>";
$db = new SettingsDatabase();
$db->create();
echo "<h2>GET SETTINGS</h2>";
$rows = $db->getSettings();
var_dump($rows);
echo "<h2>CHANGE SETTINGS</h2>";
$db->updateSettings(array("about_text" => "-", "footer_text" => "Powered By OfflineLibrary.", "logo" => "images/logo.png", "header_color" => 0, "content_color" => 0));
$rows = $db->getSettings();
var_dump($rows);