2013-08-07 23:29:57 +02:00
|
|
|
<?php
|
2013-08-12 09:52:50 +02:00
|
|
|
require_once('data/config.php');
|
2013-08-07 23:29:57 +02:00
|
|
|
|
|
|
|
class Storage {
|
|
|
|
private $connection = null;
|
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->connect();
|
2013-08-07 23:29:57 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
public function __destruct() {
|
2013-08-07 23:29:57 +02:00
|
|
|
$this->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Connect / Disconnect functions
|
|
|
|
public function connect() {
|
|
|
|
$this->connection = new PDO('mysql:host='.MYSQL_HOST.';dbname='.MYSQL_DB, MYSQL_LOGIN, MYSQL_PASSWORD);
|
|
|
|
$this->connection->query('SET NAMES utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function disconnect() {
|
|
|
|
$this->connection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Function to get and set vars
|
|
|
|
public function getHost() {
|
|
|
|
return $this->host;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLogin() {
|
|
|
|
return $this->login;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPassword() {
|
|
|
|
return $this->password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDb() {
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHost($host) {
|
|
|
|
$this->host = host;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLogin($login) {
|
|
|
|
$this->login = $login;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPassword($password) {
|
|
|
|
$this->password = $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDb($db) {
|
2013-08-08 22:55:12 +02:00
|
|
|
$this->db = $db;
|
2013-08-07 23:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function typeToSQL($type) {
|
|
|
|
$return = false;
|
|
|
|
switch($type) {
|
|
|
|
case 'key':
|
2013-08-13 19:37:11 +02:00
|
|
|
case 'int':
|
2013-08-08 22:55:12 +02:00
|
|
|
$return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY';
|
2013-08-07 23:29:57 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'string':
|
2013-08-08 22:55:12 +02:00
|
|
|
$return = 'VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci';
|
2013-08-07 23:29:57 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'bool':
|
2013-08-08 22:55:12 +02:00
|
|
|
$return = 'TINYINT(1)';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'password':
|
|
|
|
$return = 'VARCHAR(130)';
|
2013-08-07 23:29:57 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-13 19:37:11 +02:00
|
|
|
case 'text':
|
2013-08-07 23:29:57 +02:00
|
|
|
default:
|
2013-08-08 22:55:12 +02:00
|
|
|
$return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
|
2013-08-07 23:29:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:44:43 +02:00
|
|
|
public function load($fields = NULL) {
|
|
|
|
$query = 'SELECT ';
|
|
|
|
$i = false;
|
|
|
|
foreach($this->fields as $field=>$type) {
|
|
|
|
if($i) { $query .= ','; } else { $i = true; }
|
|
|
|
|
|
|
|
$query .= $field;
|
|
|
|
}
|
|
|
|
$query .= ' FROM '.MYSQL_PREFIX.$this->TABLE_NAME;
|
|
|
|
|
|
|
|
if(!empty($fields) && is_array($fields)) {
|
|
|
|
$i = true;
|
|
|
|
foreach($fields as $field=>$value) {
|
|
|
|
if($i) { $query .= ' WHERE '; $i = false; } else { $query .= ' AND '; }
|
|
|
|
|
|
|
|
$query .= $field.'=:'.$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->prepare($query);
|
|
|
|
|
|
|
|
if(!empty($fields) && is_array($fields)) {
|
|
|
|
foreach($fields as $field=>$value) {
|
|
|
|
$query->bindParam(':'.$field, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
return $query->fetchAll();
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
public function save() {
|
|
|
|
if(!empty($this->id)) {
|
2013-08-09 00:44:43 +02:00
|
|
|
$query = 'UPDATE '.MYSQL_PREFIX.$this->TABLE_NAME.' SET ';
|
2013-08-07 23:29:57 +02:00
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
$i = false;
|
|
|
|
foreach($this->fields as $field=>$type) {
|
2013-08-11 22:25:25 +02:00
|
|
|
if(isset($this->$field))
|
|
|
|
{
|
|
|
|
if($i) { $query .= ','; } else { $i = true; }
|
2013-08-08 22:55:12 +02:00
|
|
|
|
2013-08-11 22:25:25 +02:00
|
|
|
$query .= $field.'=:'.$field;
|
|
|
|
}
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
2013-08-07 23:29:57 +02:00
|
|
|
|
2013-08-10 22:33:39 +02:00
|
|
|
$query .= ' WHERE id='.$this->id;
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$query = 'INSERT INTO '.MYSQL_PREFIX.$this->TABLE_NAME.'(';
|
|
|
|
|
|
|
|
$i = false;
|
|
|
|
foreach($this->fields as $field=>$type) {
|
|
|
|
if($i) { $query .= ','; } else { $i = true; }
|
|
|
|
|
|
|
|
$query .= $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ') VALUES(';
|
|
|
|
|
|
|
|
$i = false;
|
|
|
|
foreach($this->fields as $field=>$type) {
|
2013-08-11 22:25:25 +02:00
|
|
|
if(isset($this->$field)) {
|
|
|
|
if($i) { $query .= ','; } else { $i = true; }
|
2013-08-08 22:55:12 +02:00
|
|
|
|
2013-08-11 22:25:25 +02:00
|
|
|
$query .= ':'.$field;
|
|
|
|
}
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ')';
|
|
|
|
}
|
2013-08-09 00:44:43 +02:00
|
|
|
|
2013-08-10 22:33:39 +02:00
|
|
|
$this->connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
|
2013-08-08 22:55:12 +02:00
|
|
|
$query = $this->connection->prepare($query);
|
|
|
|
|
|
|
|
foreach($this->fields as $field=>$type) {
|
2013-08-17 19:16:16 +02:00
|
|
|
if(isset($this->$field)) {
|
2013-08-11 22:25:25 +02:00
|
|
|
$query->bindParam(':'.$field, $this->$field);
|
|
|
|
}
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$this->id = (!isset($this->id) ? $this->connection->lastInsertId() : $this->id);
|
|
|
|
}
|
2013-08-11 22:34:39 +02:00
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
$query = 'DELETE FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE ';
|
|
|
|
|
|
|
|
$i = false;
|
|
|
|
foreach($this->fields as $field=>$type) {
|
|
|
|
if(!empty($this->$field)) {
|
|
|
|
if($i) { $query .= ' AND '; } else { $i = true; }
|
|
|
|
|
|
|
|
$query .= $field.'=:'.$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->prepare($query);
|
|
|
|
|
|
|
|
foreach($this->fields as $field=>$type) {
|
|
|
|
if(!empty($this->$field)) {
|
|
|
|
$query->bindParam(':'.$field, $this->$field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
2013-08-07 23:29:57 +02:00
|
|
|
}
|