class.Address_Book_DB.php
changeset 14 2a78703d3c2d
child 18 846026b8422b
equal deleted inserted replaced
13:6216181a7668 14:2a78703d3c2d
       
     1 <?
       
     2 require "interface.Address_Book.php";
       
     3 
       
     4 class Address_Book_DB implements Address_Book {
       
     5 	const INSERT_ENTRY = 'INSERT INTO data (name, tel, email)
       
     6 	 		      VALUES(:name, :tel, :email)';
       
     7 	const SELECT_ENTRY = 'SELECT name AS NAME, tel AS TEL, email AS MAIL
       
     8 			      FROM data WHERE name LIKE :name
       
     9 			      	           OR tel LIKE :tel
       
    10 					   OR email LIKE :email';
       
    11 		
       
    12 
       
    13 	private $dbh;
       
    14 
       
    15 	public function __construct($dsn, $user = null, $pass = null) {
       
    16 		$this->dbh = new PDO($dsn, $user, $pass,
       
    17 		array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
       
    18 
       
    19 		$this->dbh->exec("CREATE TABLE IF NOT EXISTS data
       
    20 			(name TEXT, tel TEXT, email TEXT)");
       
    21 
       
    22 		$this->dbh->beginTransaction();
       
    23 
       
    24 		$sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data");
       
    25 		$sth->execute();
       
    26 		$r = $sth->fetch();
       
    27 
       
    28 		print_r($r);
       
    29 
       
    30 		if ($r['COUNT'] == 0) 
       
    31 			$this->insert(array("Hans Hanson", "0815", "hans@hanson.de"));
       
    32 		$this->dbh->commit();
       
    33 	}
       
    34 
       
    35 	private function insert($entry) {
       
    36 		static $sth = null;
       
    37 
       
    38 		if ($sth === null)
       
    39 			$sth = $this->dbh->prepare(self::INSERT_ENTRY);
       
    40 		
       
    41 		$sth->execute(array("name" => $entry[0],
       
    42 				    "tel"  => $entry[1],
       
    43 				    "email" => $entry[2]));
       
    44 	}
       
    45 			
       
    46 	public function get_all_entries() {
       
    47 		$entries = array();
       
    48 		fseek($this->fh, 0, SEEK_SET);
       
    49 		while($line = stream_get_line($this->fh, 0, self::RS)) {
       
    50 			if ($line === FALSE) break;
       
    51 			$entries[] = explode(self::FS, $line);
       
    52 		}
       
    53 		return $entries;
       
    54 	}
       
    55 
       
    56 	public function add_entry($entry) {
       
    57 		$fields = array('name', 'tel', 'mail');
       
    58 		$new = array();
       
    59 		foreach($fields as $key) {
       
    60 			if (!isset($entry[$key])) return;
       
    61 			$new[$key] = $entry[$key];
       
    62 			trim($new[$key]);
       
    63 			preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]);
       
    64 			if (empty($new[$key])) return;
       
    65 		}
       
    66 
       
    67 		flock($this->fh, LOCK_EX);
       
    68 		fputs($this->fh, join(self::FS, $new) . self::RS);
       
    69 		flock($this->fh, LOCK_UN);
       
    70 		header("Location: $_SERVER[PHP_SELF]?action=add");
       
    71 		exit;
       
    72 	}
       
    73 
       
    74 	public function search_entries($pattern) {
       
    75 		static $sth = null;
       
    76 
       
    77 		if ($sth === null)
       
    78 			$sth = $this->dbh->prepare(self::SELECT_ENTRY);
       
    79 
       
    80 		// $pattern = "%" . trim(trim($pattern), "%") . "%";
       
    81 
       
    82 		$pattern = trim($pattern);
       
    83 		$pattern = trim($pattern, '%');
       
    84 		$pattern = "%$pattern%";
       
    85 
       
    86 		$sth->execute(array("name" => $pattern,
       
    87 				    "tel" => $pattern,
       
    88 				    "email" => $pattern));
       
    89 		$entries = array();
       
    90 		while ($r = $sth->fetch()) {
       
    91 			$entries[] = array('name' => $r['NAME'],
       
    92 					   'tel' => $r['TEL'],
       
    93 					   'mail' => $r['EMAIL']);
       
    94 		}
       
    95 		return $entries;
       
    96 	}
       
    97 
       
    98 	public function delete_entries($ids) { }
       
    99 }
       
   100 
       
   101 ?>