commit 8fc550007416ce1ac7c42e722b8711c41d96716c
Author: Milan Vacek <vacekm@students.zcu.cz>
Date:   Mon Apr 19 23:15:53 2021 +0200

    Bug #8639 Metoda insert

diff --git a/application/model/DB.php b/application/model/DB.php
index 6dc07c3..4cd6a61 100644
--- a/application/model/DB.php
+++ b/application/model/DB.php
@@ -65,6 +65,40 @@ class DB {
         }
     }
 
+    function getUser($username) {
+        $this->stmt = $this->pdo->prepare('SELECT id, username, password, role FROM users WHERE username = :username');
+        $this->stmt->bindParam(':username', $username);
+        $this->stmt->execute();
+        return $this->stmt->fetchAll();
+    }
+
+    function createUser($userDetails) {
+        $this->stmt = $this->pdo->prepare('INSERT INTO users (username, password, role) VALUES (:username, :password, :role)');
+        $password = password_hash($userDetails['username'], PASSWORD_DEFAULT);
+        $this->stmt->bindParam(':username', $userDetails['username']);
+        $this->stmt->bindParam(':password',$password);
+        $this->stmt->bindParam(':role',$userDetails['role']);
+        $this->stmt->execute();
+        return $this->stmt->fetchAll();
+    }
+
+    function updateUserRole($userId, $role) {
+        $this->stmt = $this->pdo->prepare("UPDATE users SET role = :role WHERE id = :userId");
+        $this->stmt->bindParam(':role', $role);
+        $this->stmt->bindParam(':userId',$userId);
+        $this->stmt->execute();
+        return $this->stmt->fetchAll();
+    }
+
+    function deleteUser($userId) {
+        $this->stmt = $this->pdo->prepare("DELETE from users WHERE id = :userId");
+        $this->stmt->bindParam(':userId',$userId);
+        $this->stmt->execute();
+        return $this->stmt->fetchAll();
+    }
+
+
+
 function update(){
     $query = "SELECT id FROM dd_lemma
               WHERE lemma = :lemma;";
@@ -382,6 +416,7 @@ function update(){
 }
 
 function insert(){
+  print_r($_POST);
 // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
     $query = "SELECT id FROM dd_lemma
               WHERE lemma = :lemma;";
@@ -606,45 +641,47 @@ function insert(){
     if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
         $query = "SELECT * FROM dd_manuscript WHERE ";
         $query .= " wordform_id = :wordform_id ;";
-    }
 
-    $this->stmt = $this->pdo->prepare($query);
-    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
-        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
-    }
-    $this->stmt->execute();
-    $result = $this->stmt->fetchAll();
+        $this->stmt = $this->pdo->prepare($query);
+            if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
+                $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
+            }
+            $this->stmt->execute();
+            $result = $this->stmt->fetchAll();
 
-    $to_insert = [];
-    $contained = [];
-    $found = false;
-    $integerIDs = [];
+            $to_insert = [];
+            $contained = [];
+            $found = false;
+            $integerIDs = [];
+
+            foreach ($result as $res) {
+                $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
+                foreach ($integerIDs as $new_value){
+                    if($new_value == $res['manuscript']){
+                        $found = true;
+                        array_push($contained, $new_value);
+                    }
+                }
+                if($found == false){
+                    array_push($to_delete, $res);
+                }
+                $found = false;
+            }
+            $to_insert = array_diff($integerIDs, $contained);
+            foreach ($to_insert as $id_to_insert){
+                $query = "INSERT INTO dd_manuscript VALUES ( ";
+                $query .= " :wordform_id , ";
+                $query .= " " . $id_to_insert . " ); ";
 
-    foreach ($result as $res) {
-        $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
-        foreach ($integerIDs as $new_value){
-            if($new_value == $res['manuscript']){
-                $found = true;
-                array_push($contained, $new_value);
+                $this->stmt = $this->pdo->prepare($query);
+                if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
+                    $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
+                }
+                $this->stmt->execute();
             }
-        }
-        if($found == false){
-            array_push($to_delete, $res);
-        }
-        $found = false;
     }
-    $to_insert = array_diff($integerIDs, $contained);
-    foreach ($to_insert as $id_to_insert){
-        $query = "INSERT INTO dd_manuscript VALUES ( ";
-        $query .= " :wordform_id , ";
-        $query .= " " . $id_to_insert . " ); ";
 
-        $this->stmt = $this->pdo->prepare($query);
-        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
-            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
-        }
-        $this->stmt->execute();
-    }
+
 }
 
 function remove(){
@@ -690,7 +727,7 @@ function remove(){
 
 // (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
 define('DB_HOST', 'localhost');
-define('DB_NAME', 'dalimil2');
+define('DB_NAME', 'dalim2');
 define('DB_CHARSET', 'utf8');
 define('DB_USER', 'postgres');
-define('DB_PASSWORD', 'ahavole');
\ No newline at end of file
+define('DB_PASSWORD', 'a');
\ No newline at end of file
