1 <?php |
1 <?php |
|
2 // Template Engine |
|
3 require_once 'Twig-1.3.0/lib/Twig/Autoloader.php'; |
|
4 Twig_Autoloader::register(); |
|
5 $twig = new Twig_Environment(new Twig_Loader_Filesystem("templates")); |
2 |
6 |
3 const FILE = "var/abook.txt"; |
7 const FILE = "var/abook.txt"; |
4 |
8 |
5 require 'addressbook.class.php'; |
9 if ($_REQUEST) { |
|
10 require 'addressbook.class.php'; |
|
11 $abook = new AddressBook(FILE); |
6 |
12 |
7 $abook = new AddressBook(FILE); |
13 switch (@$_REQUEST['action']) { |
|
14 // Nach dem Eintragen bleiben wir auf der Eintragsseite, |
|
15 // aber wir verhindern Duplikate, die mit RELOAD passieren |
|
16 case 'add': if ($abook->add_entry($_REQUEST)) { |
|
17 header("Location: $_SERVER[PHP_SELF]?action=add"); |
|
18 exit(0); |
|
19 } |
|
20 echo $twig->render("add.html", array()); |
|
21 exit; |
|
22 break; |
8 |
23 |
9 switch (@$_REQUEST['action']) { |
24 // Suchen… |
10 case 'add': if ($abook->add_entry($_REQUEST)) { |
25 case 'search': $entries = $abook->search_entries($_REQUEST['pattern']); |
11 header("Location: $_SERVER[PHP_SELF]?action=add"); |
26 if (@$_REQUEST['format'] == 'table') { |
12 exit(0); |
27 if (is_numeric($_REQUEST['max'])) { |
13 } |
28 $left = count($entries) - $_REQUEST['max']; |
14 break; |
29 if ($left > 0) |
15 case 'search': $entries = $abook->search_entries($_REQUEST['pattern']); |
30 $entries = array_slice($entries, 0, $_REQUEST['max']); |
16 if (@$_REQUEST['format'] == 'table') { |
31 else |
17 header("Content-Type: text/html; charset=UTF-8"); |
32 $left = NULL; |
18 if (!$entries) { |
33 } |
19 echo "Sorry, keine Einträge."; |
34 echo $twig->render("results.html", |
20 exit(0); |
35 array('entries' => $entries, 'left' => $left)); |
|
36 exit; |
21 } |
37 } |
22 echo "<table><tr><th>Name<th>Tel<th>Mail</tr>"; |
38 echo $twig->render("search.html", array('entries' => $entries)); |
23 for ($i = 1; $entry = array_shift($entries); $i++) { |
39 exit; |
24 echo "<tr>" |
40 break; |
25 . "<td>" . htmlspecialchars($entry['name']) |
41 } |
26 . "<td>" . htmlspecialchars($entry['tel']) |
|
27 . "<td>" . htmlspecialchars($entry['mail']); |
|
28 if ($i > @$_REQUEST['max']) break; |
|
29 } |
|
30 echo "</table>"; |
|
31 if ($entries) |
|
32 echo "<p>Und noch ".count($entries)." weitere Einträge"; |
|
33 exit(0); |
|
34 } |
|
35 break; |
|
36 } |
42 } |
37 |
43 |
|
44 echo $twig->render("search.html", array()); |
38 |
45 |
|
46 exit; |
39 ?> |
47 ?> |
40 <!-- HTML --> |
|
41 <?=header("Content-Type: text/html; charset=UTF-8");?> |
|
42 <html> |
|
43 <head> |
|
44 |
|
45 <script type=text/javascript> |
|
46 // source: http://de.wikibooks.org/wiki/Websiteentwicklung:_AJAX:_Erstes_Programm |
|
47 var _ajax; |
|
48 |
|
49 function got_answer() { |
|
50 if (_ajax.readyState == 4 && _ajax.status == 200) { |
|
51 document.getElementById('result').innerHTML = _ajax.responseText; |
|
52 } |
|
53 } |
|
54 |
|
55 function search_entries(value) { |
|
56 value = encodeURIComponent(value); |
|
57 request = "?action=search&format=table&max=3&pattern=" + value; |
|
58 _ajax = new XMLHttpRequest(); |
|
59 _ajax.onreadystatechange = got_answer; |
|
60 _ajax.open("GET", request, true); |
|
61 _ajax.send(); |
|
62 } |
|
63 </script> |
|
64 |
|
65 <style type="text/css"> |
|
66 form label { display:block; float:left; width:10ex; } |
|
67 </style> |
|
68 |
|
69 </head><body> |
|
70 |
|
71 <? if (@$_REQUEST['action'] == 'add') { ?> |
|
72 [ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ] |
|
73 <? } else { ?> |
|
74 [ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ] |
|
75 <? } ?> |
|
76 |
|
77 <div id=debug> |
|
78 DEBUG |
|
79 </div> |
|
80 <h1>Adressbuch</h1> |
|
81 |
|
82 <? if (@$_REQUEST['action'] == 'add') { ?> |
|
83 <p> |
|
84 <form> |
|
85 <label for=name>Name</label> |
|
86 <input id=name type=text name=name /><br> |
|
87 <label for=tel>Telefon</label> |
|
88 <input id=tel type=text name=tel /><br> |
|
89 <label for=mail>Mail</label> |
|
90 <input id=mail type=text name=mail /><br> |
|
91 <input type=hidden name=action value=add /> |
|
92 <input type=submit /> |
|
93 </form> |
|
94 |
|
95 <? } else { ?> |
|
96 |
|
97 <form autocomplete=off> |
|
98 <input type=text name=pattern |
|
99 onKeyUp="search_entries(this.value)" |
|
100 onChange="submit()") /> |
|
101 <input type=hidden name=action value=search autocomplete=off /> |
|
102 <noscript><input type=submit /></noscript> |
|
103 </form> |
|
104 |
|
105 <div id=result> |
|
106 <? if (isset($entries) and $entries) { ?> |
|
107 <table> |
|
108 <tr><th>Name<th>Telefon<th>Mail</tr> |
|
109 <? foreach ($entries as $entry) { ?> |
|
110 <tr> |
|
111 <td><?=$entry['name']?> |
|
112 <td><?=$entry['tel']?> |
|
113 <td><?=$entry['mail']?> |
|
114 </tr> |
|
115 <? } ?> |
|
116 </table> |
|
117 <? } else { ?> |
|
118 Sorry. |
|
119 <? } ?> |
|
120 </div> |
|
121 |
|
122 <? } ?> |
|
123 |
|
124 </body> |
|