bouffeatulm/inc/Storage.class.php

130 lines
3.1 KiB
PHP
Raw Normal View History

2013-08-07 23:29:57 +02:00
<?php
require_once('config.php');
class Storage {
private $host, $login, $password, $db;
private $connection = null;
public function __construct() {
$this->connect();
2013-08-07 23:29:57 +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) {
$this->db = $db;
2013-08-07 23:29:57 +02:00
}
public function typeToSQL($type) {
$return = false;
switch($type) {
case 'key':
$return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY';
2013-08-07 23:29:57 +02:00
break;
case 'string':
$return = 'VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci';
2013-08-07 23:29:57 +02:00
break;
case 'bool':
$return = 'TINYINT(1)';
break;
case 'password':
$return = 'VARCHAR(130)';
2013-08-07 23:29:57 +02:00
break;
default:
$return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
2013-08-07 23:29:57 +02:00
break;
}
}
public function save() {
if(!empty($this->id)) {
$query = 'UPDATE `'.MYSQL_PREFIX.$this->TABLE_NAME.'` SET ';
2013-08-07 23:29:57 +02:00
$i = false;
foreach($this->fields as $field=>$type) {
if($i) { $query .= ','; } else { $i = true; }
$id = $this->$field;
$query .= '`'.$field.'` = "'.$this($id).'"';
}
2013-08-07 23:29:57 +02:00
$query .= 'WHERE `id`="'.$this->id.'"';
}
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) {
if($i) { $query .= ','; } else { $i = true; }
$query .= ':'.$field;
}
$query .= ')';
}
$query = $this->connection->prepare($query);
foreach($this->fields as $field=>$type) {
$query->bindParam(':'.$field, $this->$field);
}
$query->execute();
$this->id = (!isset($this->id) ? $this->connection->lastInsertId() : $this->id);
}
2013-08-07 23:29:57 +02:00
}