Mithril as main method

This commit is contained in:
2017-10-25 12:08:41 +07:00
parent 6040809710
commit d1d5ee1b0c
157 changed files with 19593 additions and 716 deletions

View File

@@ -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;

View File

@@ -22,7 +22,49 @@ class ApiModel
}
}
public function showAll($conditions = [], $table)
public static function fetch($table, $conditions = [])
{
$sql = "SELECT * FROM {$table}";
if ($conditions) {
$sql .= " WHERE";
foreach ($conditions as $condition) {
$keys[] = $condition[0];
$operators[] = $condition[1];
$values[] = $condition[2];
}
$x = 0;
foreach ($keys as $key) {
$sql .= " $key $operators[$x] ?";
$x++;
if ($x < count($keys)) {
$sql .= " AND";
}
}
}
try {
$con = static::connectDB();
$query = $con->prepare($sql);
if (count($conditions)) {
$x = 1;
foreach ($values as $value) {
$query->bindValue($x, $value);
$x++;
}
}
$query->execute();
return $query->fetch(\PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error: $e->getMessage()";
}
}
public static function showAll($table, $conditions = [])
{
$sql = "SELECT * FROM {$table}";
@@ -64,7 +106,7 @@ class ApiModel
}
}
public function update($table, $args)
public static function update($table, $args)
{
$sql = "UPDATE {$table} SET";
@@ -96,15 +138,15 @@ class ApiModel
$query->execute();
return $this->showAll([
return self::showAll($table, [
['id', '=', $id]
], $table);
]);
} catch (PDOException $e) {
echo "Error: $e->getMessage()";
}
}
public function entry($table, $args)
public static function entry($table, $args)
{
$sql = "INSERT INTO {$table}";
@@ -141,7 +183,7 @@ class ApiModel
}
}
public function remove($table, $id)
public static function remove($table, $id)
{
$sql = "UPDATE {$table} SET `status` = 0 WHERE `id` = ?";
try {
@@ -151,7 +193,9 @@ class ApiModel
$query->bindValue(1, $id);
$query->execute();
return true;
return self::showAll($table, [
['id', '=', $id]
]);
} catch (PDOException $e) {
echo "Error: $e->getMessage()";
}

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Models;
use App\Config;
class ClientSession
{
protected static $conn = null;
protected static function connectDB()
{
try {
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);
}
return self::$conn;
} catch (PDOException $e) {
throw new \Exception($e->getMessage, 444);
}
}
public static function fetch($args)
{
$sql = "SELECT * FROM `client_session` WHERE ";
$x = 0;
foreach ($args as $key => $value) {
$sql .= "$key=?";
if ($x < count($args)-1) {
$sql .= " AND ";
}
$x++;
}
try {
$db = static::connectDB();
$query = $db->prepare($sql);
$x = 1;
foreach ($args as $value) {
$query->bindValue($x, $value);
$x++;
}
$query->execute();
$result = $query->fetch(\PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$result = $e->getMessage();
}
return $result;
}
public static function entry($args)
{
$sql = "INSERT INTO `client_session` (`ip_address`, `uid`) VALUES (?, ?)";
try {
$db = static::connectDB();
$query = $db->prepare($sql);
$query->bindValue(1, $args['ip_address']);
$query->bindValue(2, $args['uid']);
$query->execute();
$last_entry = "SELECT LAST_INSERT_ID()";
$last_entry = $db->prepare($last_entry);
$last_entry->execute();
$result = $last_entry->fetch(\PDO::FETCH_ASSOC);
return true;
} catch (PDOException $e) {
$result = $e->getMessage();
}
return $result;
}
public static function remove($id)
{
$sql = "DELETE FROM `client_session` WHERE `uid` = ?";
try {
$db = static::connectDB();
$query = $db->prepare($sql);
$query->bindValue(1, $id);
$query->execute();
$result = self::fetch(['uid' => $id]);
} catch (PDOException $e) {
$result = $e->getMessage();
}
return $result;
}
}

117
App/Models/Pengumuman.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
namespace App\Models;
use App\Config;
class Pengumuman
{
protected static $conn = null;
protected static function connectDB()
{
try {
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);
}
return self::$conn;
} catch (PDOException $e) {
throw new \Exception($e->getMessage, 444);
}
}
public static function fetch($table, $conditions = [])
{
$sql = "SELECT
pengumuman.id, pengumuman.category as category,
kategori.category as categoryName, kategori.background as background, kategori.foreground as foreground,
pengumuman.created_at, pengumuman.valid_at, pengumuman.expired_at, pengumuman.creator, pengumuman.edited_at, pengumuman.content, pengumuman.status, pengumuman.delay
FROM pengumuman INNER JOIN kategori ON pengumuman.category = kategori.id";
if ($conditions) {
$sql .= " WHERE";
foreach ($conditions as $condition) {
$keys[] = $condition[0];
$operators[] = $condition[1];
$values[] = $condition[2];
}
$x = 0;
foreach ($keys as $key) {
$sql .= " $key $operators[$x] ?";
$x++;
if ($x < count($keys)) {
$sql .= " AND";
}
}
}
try {
$con = static::connectDB();
$query = $con->prepare($sql);
if (count($conditions)) {
$x = 1;
foreach ($values as $value) {
$query->bindValue($x, $value);
$x++;
}
}
$query->execute();
return $query->fetch(\PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error: $e->getMessage()";
}
}
public static function showAll($table, $conditions = [])
{
$sql = "SELECT
pengumuman.id, pengumuman.category as category,
kategori.background as background, kategori.foreground as foreground,
created_at, valid_at, expired_at, creator, edited_at, content, pengumuman.status, delay,
users.full_name as creatorName
FROM pengumuman INNER JOIN kategori ON pengumuman.category = kategori.id INNER JOIN users ON pengumuman.creator = users.id";
if ($conditions) {
$sql .= " WHERE";
foreach ($conditions as $condition) {
$keys[] = $condition[0];
$operators[] = $condition[1];
$values[] = $condition[2];
}
$x = 0;
foreach ($keys as $key) {
$sql .= " $key $operators[$x] ?";
$x++;
if ($x < count($keys)) {
$sql .= " AND";
}
}
}
try {
$con = static::connectDB();
$query = $con->prepare($sql);
if (count($conditions)) {
$x = 1;
foreach ($values as $value) {
$query->bindValue($x, $value);
$x++;
}
}
$query->execute();
return $query->fetchAll(\PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error: $e->getMessage()";
}
}
}

View File

@@ -174,12 +174,12 @@ class Post extends \Core\Model
$values = '';
// This is if want to insert multiple rows
foreach ($args as $key => $val) {
if (preg_match('/,/', $val)) {
$val = explode(',', $val);
$args[$key] = $val;
}
}
// foreach ($args as $key => $val) {
// if (preg_match('/,/', $val)) {
// $val = explode(',', $val);
// $args[$key] = $val;
// }
// }
$x = 1;
foreach ($args as $field) {
@@ -198,6 +198,7 @@ class Post extends \Core\Model
try {
$sql = "INSERT INTO {$table} ({$keys}) VALUES {$values}";
$db = static::connectDB();
$query = $db->prepare($sql);
@@ -214,6 +215,8 @@ class Post extends \Core\Model
$x++;
}
}
// var_dump($x);die();
$query->execute();
return true;