| 
<?php/**
 * This is an example of usage for Naf Database Tools:
 * - Naf_Table
 * - Naf_Record
 * - Naf_Select
 *
 * This is a basic CRUD (Create/Read/Update/Delete) application
 */
 
 /* the necessary includes. in my projects, I use __autoload(),
 therefore I don't need ot write these routines */
 $root = dirname(__FILE__) . '/';
 require_once $root . 'Naf/Table.php';
 require_once $root . 'Naf/Validator.php';
 require_once $root . 'Naf/Validator/Result.php';
 require_once $root . 'Naf/Record.php';
 require_once $root . 'Naf/Select.php';
 
 // the Book class: ActiveRecord for the book table
 require_once $root . 'lib/Book.php';
 
 // let's connect to database
 $pdo = new PDO("sqlite:" . $root . "sqlite/db");
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 try {
 $pdo->query("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title varchar(255), description TEXT)");
 } catch (PDOException $e) {
 die("Unable to execute queries: " . $e->getMessage());
 }
 Naf_Table::setDefaultConnection($pdo);
 
 $errorList = array();
 if ($_SERVER['REQUEST_METHOD'] == 'POST')
 {
 switch (@$_POST['do'])
 {
 case "create":
 // CREATE action
 $newBook = new Book();
 $newBook->import($_POST);// notice - we don't care about redundant keys we are importing.
 // Naf_Record will care about importing only the needed keys
 if ($newBook->save())// should return the new book #ID, obviously > 0
 {
 header("Location: " . $_SERVER['PHP_SELF']);
 exit();
 } else {
 $errorList = $newBook->getErrorList();
 }
 break;
 case "update":
 // UPDATE action
 $updated = new Book();
 if ($updated->load(@$_POST['id']))// first, we need to know what row to update
 {
 $updated->import($_POST);
 if ($updated->save())
 {
 header("Location: " . $_SERVER['PHP_SELF']);
 exit();
 } else {
 $errorList = $updated->getErrorList();
 }
 } else {
 $errorList[] = "Book #ID not found: " . var_export(@$_POST['id'], 1);
 }
 break;
 case "delete":
 // DELETE action
 $deleted = new Book();
 if ($deleted->load(@$_POST['id']))// first, we need to know what row to delete
 {
 if ($deleted->delete())
 {
 header("Location: " . $_SERVER['PHP_SELF']);
 exit();
 } else {
 $errorList[] = "Unfortunately, delete failed";
 }
 } else {
 $errorList[] = "Book #ID not found: " . var_export(@$_POST['id'], 1);
 }
 break;
 default:
 break;
 }
 }
 
 ?>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>Naf Database Tools example: basic CRUD (Create/Read/Update/Delete) application</title>
 </head>
 <body>
 
 <?php /* Display errors if any */ ?>
 <?php if (count($errorList)) : ?>
 <ul style="color:red">
 <?php foreach ($errorList as $message) : ?>
 <li><?=$message?></li>
 <?php endforeach; ?>
 </ul>
 <?php endif; ?>
 
 <h2>Create new book</h2>
 <form method="POST" action="">
 Title: <input type="text" name="title" size="50" />
 <br />
 Description: <textarea name="description" cols="50" rows="5"></textarea>
 <br />
 <input type="submit" name="do" value="create" />
 </form>
 
 <?php /* form to submitted in order to delete a book */ ?>
 <form method="POST" action="">
 <input type="hidden" name="id" id="delete-form-id" />
 <input type="hidden" name="go" value="delete" />
 </form>
 
 <h2>Existing books</h2>
 <?php
 $bookList = new Naf_Select('book');
 /* apply search filter - ONLY in case a search form has been submitted (notice registerFilter_--If--_()) */
 $bookList->registerFilterIf(is_string(@$_GET['query']), 'title LIKE ?', "%" . @$_GET['query'] . "%");
 if (! $bookList->count())
 {
 ?><h3>No books matching your criteria in the database</h3><?php
 }
 ?>
 <form method="GET" action="">
 Search for a book: <input type="text" name="query" value="<?=htmlspecialchars(@$_GET['query'], ENT_QUOTES)?>" />
 <input type="submit" value="find" />
 </form>
 <style>
 .books td {
 vertical-align:top;
 }
 </style>
 <table border="1" class="books">
 <thead>
 <tr>
 <th>Title</th>
 <th>Description</th>
 <th colspan="2">Actions</th>
 </tr>
 </thead>
 <tbody>
 <?php foreach ($bookList->export() as $item) : ?>
 <?php /* the markup is not valid, but this is an example only... */ ?>
 <form method="POST" action="">
 <input type="hidden" name="id" value="<?=$item['id']?>" />
 <tr>
 <td><input type="text" name="title" value="<?=htmlspecialchars($item['title'], ENT_QUOTES)?>" /></td>
 <td><textarea name="description" cols="50" rows="2"><?=htmlspecialchars($item['description'], ENT_QUOTES)?></textarea></td>
 <td><input type="submit" name="do" value="update" /></td>
 <td><input type="submit" name="do" value="delete" /></td>
 </tr>
 </form>
 <?php endforeach; ?>
 </tbody>
 </table>
 
 <h2>NOTES:</h2>
 <ol>
 <li>Remember to download naf-validator package from phpclasse.org!</li>
 <li>For the example to work, you will have to create a folder named `Naf' in this folder,
 with contents as follows:
 <b>Naf/Table.php</b>, <b>Naf/Validator.php</b>, <b>Naf/Validator/Result.php</b>,
 <b>Naf/Record.php</b>, <b>Naf/Select.php</b></li>
 <li>The SQLite database is supposed to be in `sqlite/db'</li>
 <li>To get the example working, you won't need the actual database file, but...</li>
 <li>REMEMBER: the web-server user will need a write-access to `sqlite' directory!</li>
 </ol>
 </body>
 </html>
 |