2013-08-07 23:29:57 +02:00
|
|
|
<?php
|
2013-08-12 09:52:50 +02:00
|
|
|
require_once('data/config.php');
|
2013-11-24 22:16:32 +01:00
|
|
|
require_once('inc/MysqlConnector.php');
|
2013-08-07 23:29:57 +02:00
|
|
|
|
|
|
|
class Storage {
|
|
|
|
private $connection = null;
|
2013-11-24 22:16:32 +01:00
|
|
|
private $mysql_instance = null;
|
2013-08-07 23:29:57 +02:00
|
|
|
|
2013-08-08 22:55:12 +02:00
|
|
|
public function __construct() {
|
2013-11-24 22:16:32 +01:00
|
|
|
$this->mysql_instance = MysqlConnector::getInstance();
|
|
|
|
$this->connection = $this->mysql_instance->getConnection();
|
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
|
|
|
}
|
|
|
|
|
2013-09-08 00:41:49 +02:00
|
|
|
public function getConnection() {
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
2013-08-26 09:52:04 +02:00
|
|
|
// Translates types in class to SQL types
|
|
|
|
// ======================================
|
2013-08-07 23:29:57 +02:00
|
|
|
public function typeToSQL($type) {
|
|
|
|
$return = false;
|
|
|
|
switch($type) {
|
2013-08-13 19:37:11 +02:00
|
|
|
case 'int':
|
2013-08-30 20:07:52 +02:00
|
|
|
$return = 'INT(11)';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'key':
|
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;
|
|
|
|
|
2013-08-30 20:07:52 +02:00
|
|
|
case 'float':
|
|
|
|
$return = 'FLOAT';
|
|
|
|
break;
|
|
|
|
|
2013-08-07 23:29:57 +02:00
|
|
|
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;
|
|
|
|
|
2013-08-22 23:14:14 +02:00
|
|
|
case 'date':
|
|
|
|
$return = 'DATETIME NOT NULL';
|
|
|
|
break;
|
|
|
|
|
2013-08-07 23:29:57 +02:00
|
|
|
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-26 09:52:04 +02:00
|
|
|
// Load function
|
|
|
|
// =============
|
2013-09-11 00:51:08 +02:00
|
|
|
public function load($fields = NULL, $first_only = false, $key_array = 'id') {
|
2013-08-09 00:44:43 +02:00
|
|
|
$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 '; }
|
|
|
|
|
2013-09-05 23:07:40 +02:00
|
|
|
if(!is_array($value)) {
|
|
|
|
$value = array($value);
|
|
|
|
}
|
|
|
|
|
2013-09-15 15:28:27 +02:00
|
|
|
foreach($value as $key=>$value_array) {
|
2013-09-06 18:09:19 +02:00
|
|
|
if($value_array == 'AND' || $value_array == 'OR') {
|
2013-09-05 23:07:40 +02:00
|
|
|
$query .= ' '.$value_array.' ';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-15 15:28:27 +02:00
|
|
|
if(substr($value_array, 0, 2) == "<=")
|
|
|
|
$query .= $field.'<=:'.$field.$key;
|
|
|
|
elseif(substr($value_array, 0, 1) == "<")
|
|
|
|
$query .= $field.'<:'.$field.$key;
|
|
|
|
elseif(substr($value_array, 0, 2) == ">=")
|
|
|
|
$query .= $field.'>=:'.$field.$key;
|
2013-09-05 23:07:40 +02:00
|
|
|
elseif(substr($value_array, 0, 1) == ">")
|
2013-09-15 15:28:27 +02:00
|
|
|
$query .= $field.'>:'.$field.$key;
|
2013-09-05 23:07:40 +02:00
|
|
|
else
|
2013-09-15 15:28:27 +02:00
|
|
|
$query .= $field.'=:'.$field.$key;
|
2013-09-05 23:07:40 +02:00
|
|
|
}
|
2013-08-09 00:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->prepare($query);
|
|
|
|
|
|
|
|
if(!empty($fields) && is_array($fields)) {
|
|
|
|
foreach($fields as $field=>$value) {
|
2013-09-05 23:07:40 +02:00
|
|
|
if(!is_array($value))
|
|
|
|
$value = array($value);
|
|
|
|
|
2013-08-30 20:07:52 +02:00
|
|
|
if($fields[$field] == 'date')
|
|
|
|
$value = $value->format('Y-m-d H:i:s');
|
|
|
|
|
2013-09-15 15:28:27 +02:00
|
|
|
foreach($value as $key=>$value_array) {
|
2013-09-05 23:07:40 +02:00
|
|
|
if($value_array == 'AND' || $value_array == 'OR')
|
|
|
|
continue;
|
|
|
|
|
2013-09-15 15:28:27 +02:00
|
|
|
if(substr($value_array, 0, 2) == ">=" || substr($value_array, 0, 2) == "<=")
|
|
|
|
$value_array = substr($value_array, 2);
|
|
|
|
elseif(substr($value_array, 0, 1) == ">" || substr($value_array, 0, 1) == "<")
|
|
|
|
$value_array = substr($value_array, 1);
|
|
|
|
|
|
|
|
$query->bindValue(':'.$field.$key, $value_array);
|
2013-09-05 23:07:40 +02:00
|
|
|
}
|
2013-08-09 00:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute();
|
2013-09-13 19:18:49 +02:00
|
|
|
|
2013-08-27 15:51:04 +02:00
|
|
|
$results = $query->fetchAll();
|
|
|
|
|
|
|
|
if(count($results) > 0) {
|
|
|
|
$return = array();
|
|
|
|
$class = get_class($this);
|
|
|
|
|
|
|
|
foreach($results as $result) {
|
2013-09-11 00:51:08 +02:00
|
|
|
$return[$result[$key_array]] = new $class();
|
|
|
|
$return[$result[$key_array]]->sessionRestore($result);
|
2013-08-27 15:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($first_only)
|
2013-09-11 00:51:08 +02:00
|
|
|
return $return[$result[$key_array]];
|
2013-08-27 15:51:04 +02:00
|
|
|
else
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-09 00:44:43 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 09:52:04 +02:00
|
|
|
// Storing function
|
|
|
|
// ================
|
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-09-11 00:51:08 +02:00
|
|
|
if($i) { $query .= ','; } else { $i = true; }
|
2013-08-08 22:55:12 +02:00
|
|
|
|
2013-09-11 00:51:08 +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-09-11 00:51:08 +02:00
|
|
|
if($i) { $query .= ','; } else { $i = true; }
|
2013-08-08 22:55:12 +02:00
|
|
|
|
2013-09-11 00:51:08 +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-09-11 00:51:08 +02:00
|
|
|
if($type == 'date')
|
|
|
|
$value = $this->$field->format('Y-m-d H:i:s');
|
|
|
|
else
|
|
|
|
$value = $this->$field;
|
2013-08-30 20:07:52 +02:00
|
|
|
|
2013-09-11 00:51:08 +02:00
|
|
|
$query->bindValue(':'.$field, $value);
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
|
2013-09-08 15:54:44 +02:00
|
|
|
(empty($this->id) ? $this->setId($this->connection->lastInsertId()) : $this->setId($this->id));
|
2013-08-08 22:55:12 +02:00
|
|
|
}
|
2013-08-11 22:34:39 +02:00
|
|
|
|
2013-08-26 09:52:04 +02:00
|
|
|
// Delete function
|
|
|
|
// ===============
|
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)) {
|
2013-09-08 18:36:59 +02:00
|
|
|
if($this->fields[$field] == 'date')
|
|
|
|
$value = $this->$field->format('Y-m-d H:i:s');
|
|
|
|
else
|
|
|
|
$value = $this->$field;
|
2013-08-30 20:07:52 +02:00
|
|
|
|
2013-09-08 18:36:59 +02:00
|
|
|
$query->bindValue(':'.$field, $value);
|
2013-08-11 22:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
2013-08-07 23:29:57 +02:00
|
|
|
}
|