Mithril as main method
This commit is contained in:
117
App/Models/Pengumuman.php
Normal file
117
App/Models/Pengumuman.php
Normal 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()";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user