Mithril as main method
This commit is contained in:
@@ -1,56 +1,26 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
class Access extends \Core\Model
|
||||
use App\Config;
|
||||
|
||||
class Access
|
||||
{
|
||||
public function __construct()
|
||||
private static $conn;
|
||||
private static function connectDB()
|
||||
{
|
||||
$this->createTable(
|
||||
[
|
||||
'id int(3) NOT NULL AUTO_INCREMENT',
|
||||
'username varchar(25) NOT NULL',
|
||||
'password char(13) NOT NULL',
|
||||
'salt char(23) NOT NULL',
|
||||
'full_name varchar(50) NOT NULL',
|
||||
'registered_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',
|
||||
'privilege tinyint(1) NOT NULL DEFAULT 0',
|
||||
'max_user int(1) NOT NULL DEFAULT 5',
|
||||
'PRIMARY KEY (id)'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
protected function createTable($fields, $table = 'users') {
|
||||
try {
|
||||
if ($fields == []) {
|
||||
return false;
|
||||
if (!self::$conn) {
|
||||
$dsn = 'mysql:host='.Config::DB_HOST.';dbname='.Config::DB_DB;
|
||||
self::$conn = new \PDO($dsn, Config::DB_UNAME, Config::DB_PWD);
|
||||
|
||||
self::$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
$query->execute();
|
||||
return true;
|
||||
return self::$conn;
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
throw new \Exception($e->getMessage, 444);
|
||||
}
|
||||
}
|
||||
|
||||
protected function dropTable($table = 'users') {
|
||||
try {
|
||||
$sql = "DROP TABLE IF EXISTS {$table}";
|
||||
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
$query->execute();
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function showAll($conditions = [], $table = 'users')
|
||||
public static function showAll($conditions = [], $table = 'users')
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
@@ -100,7 +70,7 @@ class Access extends \Core\Model
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($args, $table = 'users')
|
||||
public static function entry($args, $table = 'users')
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = '`'.implode('`, `', array_keys($args)).'`';
|
||||
@@ -168,7 +138,7 @@ class Access extends \Core\Model
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update($args, $id, $table = 'users')
|
||||
public static function update($args, $id, $table = 'users')
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = array_keys($args);
|
||||
@@ -187,7 +157,7 @@ class Access extends \Core\Model
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$result = $this->showAll([
|
||||
$result = self::showAll([
|
||||
['id', '=', $id]
|
||||
]);
|
||||
|
||||
@@ -210,16 +180,15 @@ class Access extends \Core\Model
|
||||
return false;
|
||||
}
|
||||
|
||||
public function delete($id, $status, $table = 'users')
|
||||
public static function delete($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "UPDATE {$table} SET status = ? WHERE id = ?";
|
||||
$sql = "UPDATE `users` SET `flag` = 0 WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(1, $status);
|
||||
$query->bindValue(2, $id);
|
||||
$query->bindValue(1, $id);
|
||||
|
||||
$query->execute();
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user