/** * @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); } }
</head> <body> <?php require "../api/DocumentsDatabase.php"; require "../api/SettingsDatabase.php"; // API TESTS echo "<h1>DOCUMENT DB</h1>"; $db = new DocumentsDatabase(); $db->create(); //create topics $db->insertTopic(array("id" => null, "topic_name" => "Life, Universe and the whole Rest", "topic_color" => 1)); $db->insertTopic(array("id" => null, "topic_name" => "Lorem ipsum", "topic_color" => 2)); $db->insertTopic(array("id" => null, "topic_name" => "Schildkröten", "topic_color" => 3)); $db->insertTopic(array("id" => null, "topic_name" => "To be deleted", "topic_color" => 4)); //fill database $db->insertDocument(array("id" => null, "title" => "The Hitchhikers Guide to the Galaxy", "author" => "Adams, Douglas", "description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis vestibulum nisi at fermentum. Cras tempus, metus in pretium elementum, est dolor fringilla diam, ut euismod nulla ipsum non est. Proin dignissim nulla nunc, at tristique sem facilisis vel. Nunc ", "keywords" => "scifi fantasy comedy", "topic_id" => 1, "published" => 1979, "language" => "German", "file" => "hitchhiker.pdf")); $db->insertDocument(array("id" => null, "title" => "Madame Bovary", "author" => "Flaubert Gustave", "description" => "Etiam vulputate neque turpis, vel malesuada tellus commodo ut. Proin gravida augue odio, 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 vitae odio id, ultrices euismod ante. Curabitur lacinia est sed urna cursus, vitae finibus metus vulputate. Duis iaculis a est eget mollis. Maecenas feugiat sem sit amet tellus convallis, in placerat nisi sollicitudin. Duis vehicula ipsum nec elit dignissim, at ornare orci blandit. Donec varius sollicitudin euismod. Praesent ac imperdiet massa.", "keywords" => "french Literature", "topic_id" => 2, "published" => 1856, "language" => "French", "file" => "bovary.pdf")); $db->insertDocument(array("id" => null, "title" => "Madame Bovary", "author" => "Flaubert Gustave", "topic_id" => 2, "keywords" => "French literature", "language" => "French", "file" => "bovary.pdf")); $db->insertDocument(array("id" => null, "title" => "Madame Bovary", "author" => "Flaubert Gustave", "topic_id" => 3, "description" => "Etiam vulputate neque turpis, vel malesuada tellus commodo ut. Proin gravida augue odio,", "file" => "bovary.pdf")); $db->insertDocument(array("id" => null, "title" => "To be deleted", "author" => "Noone", "file" => "bovary.pdf")); // list all documents echo "<h2>ALL DOCUMENTS</h2>"; $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();