From 5fbe8020c655c09e684c19c5d8f2c85ae2b402ab Mon Sep 17 00:00:00 2001
From: nathaniacalista01 <nathania.calista01@gmail.com>
Date: Tue, 7 Nov 2023 09:18:01 +0700
Subject: [PATCH] fix : tampilan profile

---
 .env                                  |  3 ++-
 api/subscribe/subscribe.php           | 17 ++++++++--------
 app/controllers/ProfileController.php | 28 +++++++++++++++++++++++++-
 app/views/profile/index.php           |  9 +++++++--
 docker-compose.yml                    |  8 +++++++-
 public/css/profile/profile.css        |  6 ++++++
 public/js/subscribe.js                | 29 ---------------------------
 7 files changed, 58 insertions(+), 42 deletions(-)
 delete mode 100644 public/js/subscribe.js

diff --git a/.env b/.env
index 4f70e3d..60d18f2 100644
--- a/.env
+++ b/.env
@@ -1,4 +1,5 @@
 POSTGRES_USER=postgres
 POSTGRES_PASSWORD=postgres
 POSTGRES_DB=labpro
-POSTGRES_HOST=db
\ No newline at end of file
+POSTGRES_HOST=db
+SOAP_URL=http://host.docker.internal:8080/premium
\ No newline at end of file
diff --git a/api/subscribe/subscribe.php b/api/subscribe/subscribe.php
index 8c5240e..77e7b79 100644
--- a/api/subscribe/subscribe.php
+++ b/api/subscribe/subscribe.php
@@ -1,4 +1,5 @@
 <?php
+    require_once("../../config/config.php");
     if(session_status() === PHP_SESSION_NONE){
         session_start();
     }
@@ -11,30 +12,30 @@
         xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
         xmlns:tns="http://service.LMS.com/">
         <soap:Body>
-            <tns:subscribe>
+            <tns:upgrade>
                 <user_id>' . $user_id . '</user_id>
-            </tns:subscribe>
+            </tns:upgrade>
         </soap:Body>
     </soap:Envelope>';
     $headers = array(
         "Content-Type: text/xml;charset=\"utf-8\"",
       );
-      $url = "http://host.docker.internal:8080/subscription";
+      $url = $_ENV["SOAP_URL"];
       $ch = curl_init();
-      var_dump($ch);
-    //   var_dump(curl_error($ch));
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $request_param);
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-    
     $response = curl_exec($ch);
     if($response === FALSE){
         $_SESSION["error"] = "Subscription request has failed!";
     }else{
-        $_SESSION["success"] = "Request has been submitted!";
+        if($response == "Request failed"){
+            $_SESSION["error"] = "Subscritpion request has failed!";
+        }else{
+            $_SESSION["success"] = $response;
+        }
     }
     header("Location: /profile");
-
 ?>
\ No newline at end of file
diff --git a/app/controllers/ProfileController.php b/app/controllers/ProfileController.php
index f8cdb97..3691e6d 100644
--- a/app/controllers/ProfileController.php
+++ b/app/controllers/ProfileController.php
@@ -3,7 +3,33 @@
         public function index(){
             $middleware = $this->middleware('LoginMiddleware');
             $middleware->hasLoggedIn();
-            return $this->view('profile','index',[]);
+            $user_id = $_SESSION["user_id"];
+            $request_param = '<?xml version="1.0" encoding="utf-8" ?>
+            <soap:Envelope
+                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
+                xmlns:tns="http://service.LMS.com/">
+                <soap:Body>
+                    <tns:getPremiumStatus>
+                        <user_id>' . $user_id . '</user_id>
+                    </tns:getPremiumStatus>
+                </soap:Body>
+            </soap:Envelope>';
+            $headers = array(
+                "Content-Type: text/xml;charset=\"utf-8\"",
+            );
+            $url = $_ENV["SOAP_URL"];
+            $ch = curl_init();
+            curl_setopt($ch, CURLOPT_POST, true);
+            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+            curl_setopt($ch, CURLOPT_POSTFIELDS, $request_param);
+            curl_setopt($ch, CURLOPT_URL, $url);
+            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+            $response = curl_exec($ch);
+            // if($response === FALSE){
+            //     var_dump("Gagal request");
+            // }
+            // var_dump($response);
+            return $this->view('profile','index',["premium_status" => $response]);
         }
     }
 ?>
\ No newline at end of file
diff --git a/app/views/profile/index.php b/app/views/profile/index.php
index 5321d46..0d0a121 100644
--- a/app/views/profile/index.php
+++ b/app/views/profile/index.php
@@ -103,7 +103,10 @@
                         />
                 </div>
                 <p id='username-alert'></p>
-
+                <div class='profile-box' id='username-box'>
+                    <p>Premium status :<span class="premium-status"> <?php echo $data['premium_status'] ?></span> </p>
+                    
+                </div>
                 <div class='profile-toggle-change'>
                     <div class='profile-box' id='password-box'>
                         <p>Password&nbsp; :</p>
@@ -129,7 +132,9 @@
                     <button type='button' onclick='toggle()' class='edit-button' id='profile-button'>Edit</button>
                 </div>
                 <div>
-                    <button id="subscribe-button" type="button" class="edit-button" ><a href="/api/subscribe/subscribe.php">Subscribe</a></button>
+                    <?php if ($data["premium_status"] === 'REJECTED' || $data["premium_status"] === "NOT PREMIUM"){ ?>
+                        <button id="subscribe-button" type="button" class="edit-button" ><a href="/api/subscribe/subscribe.php">Subscribe</a></button>
+                    <?php } ?>
                 </div>
             </form>
         </div>
diff --git a/docker-compose.yml b/docker-compose.yml
index b136ce0..fa2a736 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -13,6 +13,8 @@ services:
       context: .
       dockerfile: Dockerfile
     restart: unless-stopped
+    networks:
+      - php-network
   db:
     image: postgres:latest
     container_name: db
@@ -25,5 +27,9 @@ services:
       POSTGRES_DB: ${POSTGRES_DB}
     volumes:
       - ./mysql:/var/lib/mysql
+    networks:
+      - php-network
 volumes:
-  mysql:
\ No newline at end of file
+  mysql:
+networks:
+  php-network:
\ No newline at end of file
diff --git a/public/css/profile/profile.css b/public/css/profile/profile.css
index 4e36c9d..1142b01 100644
--- a/public/css/profile/profile.css
+++ b/public/css/profile/profile.css
@@ -152,6 +152,12 @@ a {
   color: var(--first-color-light);
 }
 
+.premium-status{
+  font-weight: 700;
+  color: var(--first-color-dark);
+  font-size: var(--normal-font-size);
+}
+
 .change-buttons {
   display: flex;
   margin: 3px;
diff --git a/public/js/subscribe.js b/public/js/subscribe.js
deleted file mode 100644
index c7b02fb..0000000
--- a/public/js/subscribe.js
+++ /dev/null
@@ -1,29 +0,0 @@
-const SOAP_URL = "http://localhost:8080/subscription";
-
-const subscribe = async () => {
-    let user_id = 2;
-    const syntax = `
-    <?xml vresion="1.0" encoding="utf-8" ?>
-    <soapenv:Envelope
-    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
-    xmlns:tns="http://service.LMS.com/">
-    <soapenv:Body>
-        <tns:subscribe>
-            <user_id>${user_id}</user_id>
-        </tns:subscribe>
-    </soapenv:Body>
-</soapenv:Envelope>`
-    
-    const xhr = new XMLHttpRequest();
-    xhr.open("POST",SOAP_URL, true);
-    xhr.setRequestHeader("Content-Type","text/xml");
-    xhr.onload = function(){
-        console.log(this)
-        if(this.status === 200){
-            console.log(this.responseText)
-        }else{
-            console.log(this);
-        }
-    }
-    xhr.send(syntax)
-};
-- 
GitLab