--- a/index.php Tue Oct 25 22:24:36 2011 +0200
+++ b/index.php Tue Oct 25 23:55:33 2011 +0200
@@ -1,124 +1,47 @@
<?php
+// Template Engine
+require_once 'Twig-1.3.0/lib/Twig/Autoloader.php';
+Twig_Autoloader::register();
+$twig = new Twig_Environment(new Twig_Loader_Filesystem("templates"));
const FILE = "var/abook.txt";
-require 'addressbook.class.php';
-
-$abook = new AddressBook(FILE);
+if ($_REQUEST) {
+ require 'addressbook.class.php';
+ $abook = new AddressBook(FILE);
-switch (@$_REQUEST['action']) {
- case 'add': if ($abook->add_entry($_REQUEST)) {
- header("Location: $_SERVER[PHP_SELF]?action=add");
- exit(0);
- }
- break;
- case 'search': $entries = $abook->search_entries($_REQUEST['pattern']);
- if (@$_REQUEST['format'] == 'table') {
- header("Content-Type: text/html; charset=UTF-8");
- if (!$entries) {
- echo "Sorry, keine Einträge.";
+ switch (@$_REQUEST['action']) {
+ // Nach dem Eintragen bleiben wir auf der Eintragsseite,
+ // aber wir verhindern Duplikate, die mit RELOAD passieren
+ case 'add': if ($abook->add_entry($_REQUEST)) {
+ header("Location: $_SERVER[PHP_SELF]?action=add");
exit(0);
+ }
+ echo $twig->render("add.html", array());
+ exit;
+ break;
+
+ // Suchen…
+ case 'search': $entries = $abook->search_entries($_REQUEST['pattern']);
+ if (@$_REQUEST['format'] == 'table') {
+ if (is_numeric($_REQUEST['max'])) {
+ $left = count($entries) - $_REQUEST['max'];
+ if ($left > 0)
+ $entries = array_slice($entries, 0, $_REQUEST['max']);
+ else
+ $left = NULL;
+ }
+ echo $twig->render("results.html",
+ array('entries' => $entries, 'left' => $left));
+ exit;
}
- echo "<table><tr><th>Name<th>Tel<th>Mail</tr>";
- for ($i = 1; $entry = array_shift($entries); $i++) {
- echo "<tr>"
- . "<td>" . htmlspecialchars($entry['name'])
- . "<td>" . htmlspecialchars($entry['tel'])
- . "<td>" . htmlspecialchars($entry['mail']);
- if ($i > @$_REQUEST['max']) break;
- }
- echo "</table>";
- if ($entries)
- echo "<p>Und noch ".count($entries)." weitere Einträge";
- exit(0);
- }
- break;
+ echo $twig->render("search.html", array('entries' => $entries));
+ exit;
+ break;
+ }
}
-
-?>
-<!-- HTML -->
-<?=header("Content-Type: text/html; charset=UTF-8");?>
-<html>
-<head>
-
-<script type=text/javascript>
- // source: http://de.wikibooks.org/wiki/Websiteentwicklung:_AJAX:_Erstes_Programm
- var _ajax;
-
- function got_answer() {
- if (_ajax.readyState == 4 && _ajax.status == 200) {
- document.getElementById('result').innerHTML = _ajax.responseText;
- }
- }
-
- function search_entries(value) {
- value = encodeURIComponent(value);
- request = "?action=search&format=table&max=3&pattern=" + value;
- _ajax = new XMLHttpRequest();
- _ajax.onreadystatechange = got_answer;
- _ajax.open("GET", request, true);
- _ajax.send();
- }
-</script>
-
-<style type="text/css">
- form label { display:block; float:left; width:10ex; }
-</style>
-
-</head><body>
-
-<? if (@$_REQUEST['action'] == 'add') { ?>
- [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ]
-<? } else { ?>
- [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ]
-<? } ?>
-
-<div id=debug>
- DEBUG
-</div>
-<h1>Adressbuch</h1>
+echo $twig->render("search.html", array());
-<? if (@$_REQUEST['action'] == 'add') { ?>
- <p>
- <form>
- <label for=name>Name</label>
- <input id=name type=text name=name /><br>
- <label for=tel>Telefon</label>
- <input id=tel type=text name=tel /><br>
- <label for=mail>Mail</label>
- <input id=mail type=text name=mail /><br>
- <input type=hidden name=action value=add />
- <input type=submit />
- </form>
-
-<? } else { ?>
-
- <form autocomplete=off>
- <input type=text name=pattern
- onKeyUp="search_entries(this.value)"
- onChange="submit()") />
- <input type=hidden name=action value=search autocomplete=off />
- <noscript><input type=submit /></noscript>
- </form>
-
- <div id=result>
- <? if (isset($entries) and $entries) { ?>
- <table>
- <tr><th>Name<th>Telefon<th>Mail</tr>
- <? foreach ($entries as $entry) { ?>
- <tr>
- <td><?=$entry['name']?>
- <td><?=$entry['tel']?>
- <td><?=$entry['mail']?>
- </tr>
- <? } ?>
- </table>
- <? } else { ?>
- Sorry.
- <? } ?>
- </div>
-
-<? } ?>
-
-</body>
+exit;
+?>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/add.html Tue Oct 25 23:55:33 2011 +0200
@@ -0,0 +1,27 @@
+<?=header("Content-Type: text/html; charset=UTF-8");?>
+<html>
+<head>
+
+<style type="text/css">
+ form label { display:block; float:left; width:10ex; }
+</style>
+
+</head><body>
+
+[ <a href="?">Home</a> ]
+
+<h1>Adressbuch</h1>
+
+ <p>
+ <form>
+ <label for=name>Name</label>
+ <input id=name type=text name=name /><br>
+ <label for=tel>Telefon</label>
+ <input id=tel type=text name=tel /><br>
+ <label for=mail>Mail</label>
+ <input id=mail type=text name=mail /><br>
+ <input type=hidden name=action value=add />
+ <input type=submit />
+ </form>
+
+</body>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/results.html Tue Oct 25 23:55:33 2011 +0200
@@ -0,0 +1,24 @@
+<html>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<body>
+
+{% for entry in entries %}
+
+ {% if loop.first %}
+ <table>
+ <tr><th>Name<th>Telefon<th>Mail</tr>
+ {% endif %}
+
+ <tr><td>{{entry.name}}
+ <td>{{entry.tel}}
+ <td>{{entry.mail}}
+
+ {% if loop.last %}
+ </table>
+ <font color=red>{{ left ? left ~ ' weitere Einträge' : '' }}</font>
+ {% endif %}
+{% else %}
+Sorry, keine Einträge gefunden.
+{% endfor %}
+</body>
+</html>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/search.html Tue Oct 25 23:55:33 2011 +0200
@@ -0,0 +1,48 @@
+<html>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<head>
+
+<script type=text/javascript>
+ // source: http://de.wikibooks.org/wiki/Websiteentwicklung:_AJAX:_Erstes_Programm
+ var _ajax;
+
+ function got_answer() {
+ if (_ajax.readyState == 4 && _ajax.status == 200) {
+ document.getElementById('result').innerHTML = _ajax.responseText;
+ }
+ }
+
+ function search_entries(value) {
+ value = encodeURIComponent(value);
+ request = "?action=search&format=table&max=3&pattern=" + value;
+ _ajax = new XMLHttpRequest();
+ _ajax.onreadystatechange = got_answer;
+ _ajax.open("GET", request, true);
+ _ajax.send();
+ }
+</script>
+
+<style type="text/css">
+ form label { display:block; float:left; width:10ex; }
+</style>
+
+</head><body>
+
+[ <a href="?action=add">Add Entries</a> ]
+
+<h1>Adressbuch</h1>
+
+ <form autocomplete=off>
+ <label for=pattern>Suche</label>
+ <input id=pattern type=text name=pattern
+ onKeyUp="search_entries(this.value)"
+ onChange="submit()") />
+ <input type=hidden name=action value=search autocomplete=off />
+ <noscript><input type=submit /></noscript>
+ </form>
+
+ <div id=result>
+ {% include "results.html" %}
+ </div>
+
+</body>