From 8ae0a2d2caf136d388e039f856e3b6aaee49a1a7 Mon Sep 17 00:00:00 2001
From: Fadhil Imam Kurnia <fadhilimamk@gmail.com>
Date: Thu, 5 Oct 2017 21:54:27 +0700
Subject: [PATCH] Make edit profile handler

---
 src/controller/Controller.php       | 10 +++-
 src/controller/ProfilController.php | 73 ++++++++++++++++++++++++++++-
 src/view/profil_edit.php            |  2 +-
 3 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/src/controller/Controller.php b/src/controller/Controller.php
index d417e61..205cb79 100644
--- a/src/controller/Controller.php
+++ b/src/controller/Controller.php
@@ -13,8 +13,14 @@ class DB {
         $dbpass = 'superadmin';
         $dbname = 'db_dagojek';
 
-        $this->_db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
-        $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        try {
+            $this->_db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
+            $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+            $this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+        } catch (PDOException $e) {
+            echo "Connection fail :".$e->getMessage();
+        }
+
     }
 
     private function __clone(){}
diff --git a/src/controller/ProfilController.php b/src/controller/ProfilController.php
index 194a90d..6c8e96a 100644
--- a/src/controller/ProfilController.php
+++ b/src/controller/ProfilController.php
@@ -57,7 +57,78 @@ class ProfilController {
     }
 
     public static function SaveProfil() {
-        var_dump($_POST);
+
+        // Check sending data
+        if (!isset($_POST["name"]) || !isset($_POST["phone"]) || !isset($_POST["isDriver"]) || !isset($_GET['u'])) {
+            echo "Invalid data";
+            return;
+        }
+
+        $isPhotoUploaded = false;
+        if (isset($_FILES["photo"]) && strlen($_FILES["photo"]["name"]) != 0) {
+            $isPhotoUploaded = true;
+        }
+
+        $conn = DB::getInstance();
+
+        $user_id = simpleCrypt($_GET['u'], 'd');
+        $user_name = $_POST["name"];
+        $user_phone = $_POST["phone"];
+        $user_driver = $_POST["isDriver"] == "yes" ? 1 : 0;
+        $user_photo = isset($_FILES["photo"]) ? $_FILES["photo"]["name"] : null;
+
+        // Saving image if available
+        $stmt = $isPhotoUploaded ?
+            $conn->prepare('
+                UPDATE user
+                SET
+                  name = :name,
+                  phone = :phone,
+                  is_driver = :isDriver,
+                  photo = :photo
+                WHERE
+                  id = :id
+            ') :
+            $conn->prepare('
+                UPDATE user
+                SET
+                  name = :name,
+                  phone = :phone,
+                  is_driver = :isDriver
+                WHERE
+                  id = :id
+            ');
+
+        if (!$stmt) {
+            print_r($conn->errorInfo());
+        }
+
+        $data = array(
+            ':name' => $user_name,
+            ':phone' => $user_phone,
+            ':isDriver' => $user_driver,
+            ':id' => $user_id,
+        );
+
+        if ($isPhotoUploaded) {
+            $data += array(
+              ':photo' => $user_photo,
+            );
+        }
+
+        $stmt->execute($data);
+        if ($stmt === false) {
+            echo "Fail :";
+            echo $stmt->errorCode(). "<br>";
+            print_r($stmt->errorInfo());
+        } else {
+            header('Location: /main/profil?u='.$_GET['u']);
+        }
+
+        // Close connection
+        $stmt = null;
+        $conn = null;
+
     }
 
 }
\ No newline at end of file
diff --git a/src/view/profil_edit.php b/src/view/profil_edit.php
index 3ab9ac7..5a7caf6 100644
--- a/src/view/profil_edit.php
+++ b/src/view/profil_edit.php
@@ -15,7 +15,7 @@
         </div>
     </div>
     <div class="row">
-        <form action="/main/profil/edit/save" method="post">
+        <form action="/main/profil/edit/save?u=<?=$id?>" method="post" enctype="multipart/form-data">
             <div class="container">
                 <div class="row">
                     <div class="col-6">
-- 
GitLab