diff --git a/App/Models/Access.php b/App/Models/Access.php index 9e5a2c6..09a9942 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -54,93 +54,4 @@ class Access extends \Core\Model echo $e->getMessage(); } } - - public function entry($args) - { - if (count($args)) { - $keys = '`'.implode('`, `', array_keys($args)).'`'; - - $x = 1; - $values = ''; - foreach ($args as $field) { - $values .= '?'; - if ($x < count($args)) { - $values .= ', '; - } - $x++; - } - - try { - $db = static::connectDB(); - - $sql = "INSERT INTO user ({$keys}) VALUES ({$values})"; - - $query = $db->prepare($sql); - - $x = 1; - foreach ($args as $value) { - $query->bindValue($x, $value); - $x++; - } - - if ($query->execute()) { - return true; - } - return false; - } catch (PDOException $e) { - echo $e->getMessage(); - } - } - } - - public function update($args, $id) - { - if (count($args)) { - $keys = array_keys($args); - - $fields = []; - foreach ($keys as $key) { - $fields[] = $key.' = ?'; - } - - try { - $db = static::connectDB(); - - $sql = "UPDATE user SET ".implode(', ', $fields)." WHERE id = ?"; - - $query = $db->prepare($sql); - $x = 1; - foreach ($args as $value) { - $query->bindValue($x, $value); - $x++; - } - $query->bindValue($x, $id); - - if ($query->execute()) { - return true; - } - return false; - } catch (PDOException $e) { - echo $e->getMessage(); - } - } - } - - public function delete($id) - { - try { - $db = static::connectDB(); - - $sql = "DELETE FROM user WHERE id = ?"; - - $query = $db->prepare($sql); - - if ($query->execute([$id])) { - return true; - } - return false; - } catch (PDOException $e) { - echo $e->getMessage(); - } - } } diff --git a/App/Models/Post.php b/App/Models/Post.php index 3594b38..e5e0da5 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -5,6 +5,7 @@ class Post extends \Core\Model { public function __construct() { + // Create table for posts $this->createTable( 'pengumuman', [ @@ -19,6 +20,16 @@ class Post extends \Core\Model '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() @@ -60,95 +71,4 @@ class Post extends \Core\Model echo $e->getMessage(); } } - - public function entry($args) - { - if (count($args)) { - $keys = '`'.implode('`, `', array_keys($args)).'`'; - - $x = 1; - $values = ''; - foreach ($args as $field) { - $values .= '?'; - if ($x < count($args)) { - $values .= ', '; - } - $x++; - } - - try { - $db = static::connectDB(); - - $sql = "INSERT INTO pengumuman ({$keys}) VALUES ({$values})"; - - $query = $db->prepare($sql); - - $x = 1; - - foreach ($args as $val) { - $query->bindValue($x, urldecode($val)); - $x++; - } - - if ($query->execute()) { - return true; - } - return false; - } catch (PDOException $e) { - echo $e->getMessage(); - } - } - } - - public function update($args, $id) - { - if (count($args)) { - $keys = array_keys($args); - - $fields = []; - foreach ($keys as $key) { - $fields[] = $key.' = ?'; - } - - try { - $db = static::connectDB(); - - $sql = "UPDATE pengumuman SET ".implode(', ', $fields)." WHERE id = ?"; - - $query = $db->prepare($sql); - $x = 1; - foreach ($args as $value) { - $query->bindValue($x, $value); - $x++; - } - $query->bindValue($x, $id); - - if ($query->execute()) { - return true; - } - return false; - } catch (PDOException $e) { - echo $e->getMessage(); - } - } - } - - public function delete($id) - { - try { - $db = static::connectDB(); - - $sql = "DELETE FROM pengumuman WHERE id = ?"; - - $query = $db->prepare($sql); - - if ($query->execute([$id])) { - return true; - } - return false; - } catch (PDOException $e) { - echo $e->getMessage(); - } - - } } diff --git a/Core/Model.php b/Core/Model.php index 9d330b7..f041d48 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -7,6 +7,10 @@ abstract class Model { protected static $conn = null; + abstract public function showAll(); + + abstract public function showSingle($id); + protected static function connectDB() { try { @@ -37,6 +41,11 @@ abstract class Model public function dropTable($table) { + if (is_array($table)) { + if (count($table)) { + $table = implode(', ', $table); + } + } $sql = "DROP TABLE IF EXISTS {$table}"; $db = static::connectDB(); @@ -48,13 +57,128 @@ abstract class Model return false; } - abstract public function showAll(); + public function entry($table, $args, $values = '') + { + if (count($args)) { + $keys = '`'.implode('`, `', array_keys($args)).'`'; - abstract public function showSingle($id); + // This is if want to insert multiple rows + foreach ($args as $key => $val) { + if (preg_match('/,/', $val)) { + $val = explode(',', $val); + $args[$key] = $val; + } + } - abstract public function entry($args); + $x = 1; + foreach ($args as $field) { + // Setting the query for multiple rows + if (is_array($field)) { + foreach ($field as $fields) { + $values .= '(?)'; + if ($x < count($field)) { + $values .= ', '; + } + $x++; + } + } else { + if ($x === 1) { + $values .= '('; + } + $values .= '?'; + if ($x < count($args)) { + $values .= ', '; + } else { + $values .= ')'; + } + $x++; + } + } - abstract public function update($args, $id); + try { + $sql = "INSERT INTO {$table} ({$keys}) VALUES {$values}"; - abstract public function delete($id); + $db = static::connectDB(); + + $query = $db->prepare($sql); + + $x = 1; + foreach ($args as $value) { + if (is_array($value)) { + foreach ($value as $vals) { + $query->bindValue($x, $vals); + $x++; + } + } else { + $query->bindValue($x, $value); + $x++; + } + } + + if ($query->execute()) { + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + } + + public function update($table, $args, $id) + { + if (count($args)) { + $keys = array_keys($args); + + $fields = []; + foreach ($keys as $key) { + $fields[] = $key.' = ?'; + } + + if (count($fields) > 1) { + $fields = implode(', ', $fields); + } else { + $fields = implode('', $fields); + } + + try { + $db = static::connectDB(); + + $sql = "UPDATE {$table} SET {$fields} WHERE id = ?"; + + $query = $db->prepare($sql); + $x = 1; + foreach ($args as $value) { + $query->bindValue($x, $value); + $x++; + } + $query->bindValue($x, $id); + + if ($query->execute()) { + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + } + + public function delete($table, $id) + { + try { + $db = static::connectDB(); + + $sql = "DELETE FROM {$table} WHERE id = ?"; + + $query = $db->prepare($sql); + + if ($query->execute([$id])) { + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } } diff --git a/_tests/unit/AccessTest.php b/_tests/unit/AccessTest.php index 0e0c49d..0d8f622 100644 --- a/_tests/unit/AccessTest.php +++ b/_tests/unit/AccessTest.php @@ -13,23 +13,23 @@ class AccessTest extends \PHPUnit\Framework\TestCase $this->assertTrue($access->showAll()); - $args = [ + $user_entry = [ 'username' => 'gregorio', 'password' => 'iniada13charc', ]; - $this->assertTrue($access->entry($args)); + $this->assertTrue($access->entry('user', $user_entry)); - $args = [ + $user_update = [ 'username' => 'masihgregorio', 'password' => 'inijuga3belas' ]; - $this->assertTrue($access->update($args, 1)); + $this->assertTrue($access->update('user', $user_update, 1)); $this->assertTrue($access->showSingle(1)); - $this->assertTrue($access->delete(1)); + $this->assertTrue($access->delete('user', 1)); $this->assertTrue($access->dropTable('user')); } diff --git a/_tests/unit/PostTest.php b/_tests/unit/PostTest.php index 8a3bd07..cf02599 100644 --- a/_tests/unit/PostTest.php +++ b/_tests/unit/PostTest.php @@ -13,26 +13,38 @@ class PostTest extends \PHPUnit\Framework\TestCase $this->assertTrue($post->showAll()); - $args = [ + $category_entry = [ + 'category' => 'Akademik,Jadwal,Dosen' + ]; + + $this->assertTrue($post->entry('kategori', $category_entry)); + + $category_update = [ + 'category' => 'Akakademik' + ]; + + $this->assertTrue($post->update('kategori', $category_update, 1)); + + $post_entry = [ 'category' => 3, 'expired_at' => '2017-09-02', 'creator' => 2, 'content' => 'barbarbarbarbarbarbar!' ]; - $this->assertTrue($post->entry($args)); + $this->assertTrue($post->entry('pengumuman', $post_entry)); - $args = [ - 'category' => 5, + $post_update = [ + 'category' => 2, 'content' => 'foofoofoofoo!' ]; - $this->assertTrue($post->update($args, 1)); - + $this->assertTrue($post->update('pengumuman', $post_update, 1)); + $this->assertTrue($post->showSingle(1)); - $this->assertTrue($post->delete(1)); + $this->assertTrue($post->delete('pengumuman', 1)); - $this->assertTrue($post->dropTable('pengumuman')); + $this->assertTrue($post->dropTable(['pengumuman', 'kategori'])); } }