# HG changeset patch # User Heiko Schlittermann (JUMPER) # Date 1319727386 -7200 # Node ID 2d22262da8e04ff9911b2efb59a128de03733d67 # Parent 52fb6408b86ab05811194eb864197008992a6107 restructured layout, select mode (DB, FILE) via PATH_INFO diff -r 52fb6408b86a -r 2d22262da8e0 abook.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/abook.php Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,85 @@ +add_entry($_REQUEST)) { + header("Location: $_SERVER[PHP_SELF]?action=add"); + exit(0); + } + echo $twig->render("add.html", array()); + exit; + break; + + // Suchen… + case 'search': $entries = null; + $error = null; + try { + $entries = $abook->search_entries($_REQUEST['pattern']); + } + catch (Address_Book_Exception $e) { + $error = $e->getMessage(); + } + + 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, 'error' => $error)); + exit; + } + echo $twig->render("search.html", + array('entries' => $entries, 'error' => $error)); + exit; + } + break; + + } + catch (Address_Book_Exception $e) { + header("Content-Type: text/plain; charset=utf-8"); + print "Address Book Exception: " . $e->getMessage(); + exit; + } + catch (Exception $e) { + header("Content-Type: text/plain; charset=utf-8"); + print "Ohoh\n\n" . $e; + exit; + } +} + +echo $twig->render("search.html", array()); diff -r 52fb6408b86a -r 2d22262da8e0 class.Address_Book_DB.php --- a/class.Address_Book_DB.php Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,99 +0,0 @@ -dbh = new PDO($dsn, $user, $pass, - array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); - - $this->dbh->exec("CREATE TABLE IF NOT EXISTS data - (name TEXT, tel TEXT, mail TEXT)"); - - $this->dbh->beginTransaction(); - - $sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data"); - $sth->execute(); - $r = $sth->fetch(); - - if ($r['COUNT'] == 0) - $this->insert(array("Hans Hanson", "0815", "hans@hanson.de")); - $this->dbh->commit(); - } - - private function insert($entry) { - static $sth = null; - - if ($sth === null) - $sth = $this->dbh->prepare(self::INSERT_ENTRY); - - $sth->execute(array("name" => $entry[0], - "tel" => $entry[1], - "mail" => $entry[2])); - } - - public function get_all_entries() { - $entries = array(); - fseek($this->fh, 0, SEEK_SET); - while($line = stream_get_line($this->fh, 0, self::RS)) { - if ($line === FALSE) break; - $entries[] = explode(self::FS, $line); - } - return $entries; - } - - public function add_entry($entry) { - $fields = array('name', 'tel', 'mail'); - $new = array(); - foreach($fields as $key) { - if (!isset($entry[$key])) return; - $new[$key] = $entry[$key]; - trim($new[$key]); - preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); - if (empty($new[$key])) return; - } - - flock($this->fh, LOCK_EX); - fputs($this->fh, join(self::FS, $new) . self::RS); - flock($this->fh, LOCK_UN); - header("Location: $_SERVER[PHP_SELF]?action=add"); - exit; - } - - public function search_entries($pattern) { - static $sth = null; - - if ($sth === null) - $sth = $this->dbh->prepare(self::SELECT_ENTRY); - - $pattern = trim($pattern); - if (empty($pattern)) return; - - $pattern = trim($pattern, '%'); - $pattern = "%$pattern%"; - - $sth->execute(array("name" => $pattern, - "tel" => $pattern, - "mail" => $pattern)); - $entries = array(); - while ($r = $sth->fetch()) { - $entries[] = array('name' => $r['NAME'], - 'tel' => $r['TEL'], - 'mail' => $r['MAIL']); - } - return $entries; - } - - public function delete_entries($ids) { } -} - -?> diff -r 52fb6408b86a -r 2d22262da8e0 class.Address_Book_File.php --- a/class.Address_Book_File.php Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -fh = fopen($file, "a+b"); - if (!$this->fh) { - throw new Exception("Oooops, Problem mit $file"); - } - flock($this->fh, LOCK_EX); - $stat = fstat($this->fh); - if ($stat['size'] == 0) { - fputs($this->fh, join(self::FS, array("Hans Hanson", "0815", "hans@hanson.de")) - . self::RS); - } - flock($this->fh, LOCK_SH); - fseek($this->fh, 0, SEEK_SET); - } - - public function get_all_entries() { - $entries = array(); - fseek($this->fh, 0, SEEK_SET); - while($line = stream_get_line($this->fh, 0, self::RS)) { - if ($line === FALSE) break; - $entries[] = explode(self::FS, $line); - } - return $entries; - } - - public function add_entry($entry) { - $fields = array('name', 'tel', 'mail'); - $new = array(); - foreach($fields as $key) { - if (!isset($entry[$key])) return; - $new[$key] = trim($entry[$key]); - $new[$key] = preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); - if (empty($new[$key])) return; - } - - flock($this->fh, LOCK_EX); - fputs($this->fh, join(self::FS, $new) . self::RS); - flock($this->fh, LOCK_UN); - } - - public function search_entries($pattern) { - $pattern = trim($pattern); - if (empty($pattern)) return array(); - fseek($this->fh, 0, SEEK_SET); - $pattern = preg_replace('|/|', '\/', $pattern); - $entries = array(); - while ($line = stream_get_line($this->fh, 0, self::RS)) { - if ($line === FALSE) break; - if (!preg_match("/$pattern/i", $line)) continue; - $entry = explode(self::FS, $line); - $entries[] = array('name' => $entry[0], - 'tel' => $entry[1], - 'mail' => $entry[2]); - } - return $entries; - } - - public function delete_entries($ids) { } -} - -?> diff -r 52fb6408b86a -r 2d22262da8e0 index.php --- a/index.php Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -add_entry($_REQUEST)) { - header("Location: $_SERVER[PHP_SELF]?action=add"); - exit(0); - } - echo $twig->render("add.html", array()); - exit; - break; - - // Suchen… - case 'search': $entries = null; - $error = null; - try { - $entries = $abook->search_entries($_REQUEST['pattern']); - } - catch (Address_Book_Exception $e) { - $error = $e->getMessage(); - } - - 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, 'error' => $error)); - exit; - } - echo $twig->render("search.html", - array('entries' => $entries, 'error' => $error)); - exit; - } - break; - - } - catch (Address_Book_Exception $e) { - header("Content-Type: text/plain; charset=utf-8"); - print "Address Book Exception: " . $e->getMessage(); - exit; - } - catch (Exception $e) { - header("Content-Type: text/plain; charset=utf-8"); - print "Ohoh\n\n" . $e; - exit; - } -} - -echo $twig->render("search.html", array()); diff -r 52fb6408b86a -r 2d22262da8e0 interface.Address_Book.php --- a/interface.Address_Book.php Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ - diff -r 52fb6408b86a -r 2d22262da8e0 php/class.Address_Book_DB.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/class.Address_Book_DB.php Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,99 @@ +dbh = new PDO($dsn, $user, $pass, + array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); + + $this->dbh->exec("CREATE TABLE IF NOT EXISTS data + (name TEXT, tel TEXT, mail TEXT)"); + + $this->dbh->beginTransaction(); + + $sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data"); + $sth->execute(); + $r = $sth->fetch(); + + if ($r['COUNT'] == 0) + $this->insert(array("Hans Hanson", "0815", "hans@hanson.de")); + $this->dbh->commit(); + } + + private function insert($entry) { + static $sth = null; + + if ($sth === null) + $sth = $this->dbh->prepare(self::INSERT_ENTRY); + + $sth->execute(array("name" => $entry[0], + "tel" => $entry[1], + "mail" => $entry[2])); + } + + public function get_all_entries() { + $entries = array(); + fseek($this->fh, 0, SEEK_SET); + while($line = stream_get_line($this->fh, 0, self::RS)) { + if ($line === FALSE) break; + $entries[] = explode(self::FS, $line); + } + return $entries; + } + + public function add_entry($entry) { + $fields = array('name', 'tel', 'mail'); + $new = array(); + foreach($fields as $key) { + if (!isset($entry[$key])) return; + $new[$key] = $entry[$key]; + trim($new[$key]); + preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); + if (empty($new[$key])) return; + } + + flock($this->fh, LOCK_EX); + fputs($this->fh, join(self::FS, $new) . self::RS); + flock($this->fh, LOCK_UN); + header("Location: $_SERVER[PHP_SELF]?action=add"); + exit; + } + + public function search_entries($pattern) { + static $sth = null; + + if ($sth === null) + $sth = $this->dbh->prepare(self::SELECT_ENTRY); + + $pattern = trim($pattern); + if (empty($pattern)) return; + + $pattern = trim($pattern, '%'); + $pattern = "%$pattern%"; + + $sth->execute(array("name" => $pattern, + "tel" => $pattern, + "mail" => $pattern)); + $entries = array(); + while ($r = $sth->fetch()) { + $entries[] = array('name' => $r['NAME'], + 'tel' => $r['TEL'], + 'mail' => $r['MAIL']); + } + return $entries; + } + + public function delete_entries($ids) { } +} + +?> diff -r 52fb6408b86a -r 2d22262da8e0 php/class.Address_Book_File.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/class.Address_Book_File.php Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,70 @@ +fh = fopen($file, "a+b"); + if (!$this->fh) { + throw new Exception("Oooops, Problem mit $file"); + } + flock($this->fh, LOCK_EX); + $stat = fstat($this->fh); + if ($stat['size'] == 0) { + fputs($this->fh, join(self::FS, array("Hans Hanson", "0815", "hans@hanson.de")) + . self::RS); + } + flock($this->fh, LOCK_SH); + fseek($this->fh, 0, SEEK_SET); + } + + public function get_all_entries() { + $entries = array(); + fseek($this->fh, 0, SEEK_SET); + while($line = stream_get_line($this->fh, 0, self::RS)) { + if ($line === FALSE) break; + $entries[] = explode(self::FS, $line); + } + return $entries; + } + + public function add_entry($entry) { + $fields = array('name', 'tel', 'mail'); + $new = array(); + foreach($fields as $key) { + if (!isset($entry[$key])) return; + $new[$key] = trim($entry[$key]); + $new[$key] = preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); + if (empty($new[$key])) return; + } + + flock($this->fh, LOCK_EX); + fputs($this->fh, join(self::FS, $new) . self::RS); + flock($this->fh, LOCK_UN); + } + + public function search_entries($pattern) { + $pattern = trim($pattern); + if (empty($pattern)) return array(); + fseek($this->fh, 0, SEEK_SET); + $pattern = preg_replace('|/|', '\/', $pattern); + $entries = array(); + while ($line = stream_get_line($this->fh, 0, self::RS)) { + if ($line === FALSE) break; + if (!preg_match("/$pattern/i", $line)) continue; + $entry = explode(self::FS, $line); + $entries[] = array('name' => $entry[0], + 'tel' => $entry[1], + 'mail' => $entry[2]); + } + return $entries; + } + + public function delete_entries($ids) { } +} + +?> diff -r 52fb6408b86a -r 2d22262da8e0 php/interface.Address_Book.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/interface.Address_Book.php Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,8 @@ + diff -r 52fb6408b86a -r 2d22262da8e0 templates/add.html --- a/templates/add.html Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ - - - - - - - - -[ Home ] - -

Adressbuch

- -

-

- -
- -
- -
- - -
- - diff -r 52fb6408b86a -r 2d22262da8e0 templates/results.html --- a/templates/results.html Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ - - - - -{% if error %} - {{ error }} -{% else %} -{% for entry in entries %} - - {% if loop.first %} - - - {% endif %} - - - - - - - {% if loop.last %} -
NameTelefonMail
{{entry.name}}{{entry.tel}}{{entry.mail}}
- {{ left ? left ~ ' weitere Einträge' : '' }} - {% endif %} -{% else %} - Sorry, keine Einträge gefunden. -{% endfor %} -{% endif %} - - diff -r 52fb6408b86a -r 2d22262da8e0 templates/search.html --- a/templates/search.html Wed Oct 26 21:46:50 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ - - - - - - - - - - -[ Add Entries ] - -

Adressbuch

- -
- - - - -
- -
- {% include "results.html" %} -
- - diff -r 52fb6408b86a -r 2d22262da8e0 templates/twig/add.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/twig/add.html Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,27 @@ + + + + + + + + +[ Home ] + +

Adressbuch

+ +

+

+ +
+ +
+ +
+ + +
+ + diff -r 52fb6408b86a -r 2d22262da8e0 templates/twig/results.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/twig/results.html Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,29 @@ + + + + +{% if error %} + {{ error }} +{% else %} +{% for entry in entries %} + + {% if loop.first %} + + + {% endif %} + + + + + + + {% if loop.last %} +
NameTelefonMail
{{entry.name}}{{entry.tel}}{{entry.mail}}
+ {{ left ? left ~ ' weitere Einträge' : '' }} + {% endif %} +{% else %} + Sorry, keine Einträge gefunden. +{% endfor %} +{% endif %} + + diff -r 52fb6408b86a -r 2d22262da8e0 templates/twig/search.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/twig/search.html Thu Oct 27 16:56:26 2011 +0200 @@ -0,0 +1,48 @@ + + + + + + + + + + +[ Add Entries ] + +

Adressbuch

+ +
+ + + + +
+ +
+ {% include "results.html" %} +
+ +