lepisi-pengumuman/App/Models/Post.php
Gregorio Chiko Putra 71fb5e4933 - Added database validation
- Added valid date for posts
- Fix passing data to htmls conflicts
2017-09-06 10:40:05 +07:00

106 lines
2.7 KiB
PHP

<?php
namespace App\Models;
class Post extends \Core\Model
{
public function __construct()
{
// Create table for posts
$this->createTable(
'pengumuman',
[
'id int(3) NOT NULL AUTO_INCREMENT',
'category int(3) NOT NULL',
'created_at date NOT NULL DEFAULT CURRENT_TIMESTAMP',
'valid_at date NOT NULL DEFAULT CURRENT_TIMESTAMP',
'expired_at date NOT NULL',
'creator int(3) NOT NULL',
'edited_at date',
'editor date',
'content varchar(255) NOT NULL',
'status tinyint NOT NULL DEFAULT 1',
'PRIMARY KEY (id)'
]
);
// Create table for categories
$this->createTable(
'kategori',
[
'id int(3) NOT NULL AUTO_INCREMENT',
'category varchar(20) NOT NULL',
'PRIMARY KEY (id)'
]
);
}
public function showAll($key = '', $operator = '', $cond = '')
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM pengumuman";
if ($key && $operator && $cond) {
$sql .= " WHERE {$key} {$operator} ?";
}
$query = $db->prepare($sql);
if ($key && $operator && $cond) {
$query->bindValue(1, $cond);
}
if ($query->execute()) {
if ($query->rowCount() != 0) {
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
return $result;
}
}
return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function showSingle($id)
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM pengumuman WHERE id = ?";
$query = $db->prepare($sql);
if ($query->execute([$id])) {
if ($query->rowCount() === 1) {
$result = $query->fetch(\PDO::FETCH_ASSOC);
return $result;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function showCategories()
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM kategori";
$query = $db->prepare($sql);
if ($query->execute()) {
if ($query->rowCount() > 1) {
$results = $query->fetchAll(\PDO::FETCH_ASSOC);
return $results;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}