From 38b7e95b6575e6ca7296c99cd380dc488e9b63eb Mon Sep 17 00:00:00 2001
From: rayhanp1402 <rayhan.hanif14maulana@gmail.com>
Date: Fri, 17 Nov 2023 13:57:41 +0700
Subject: [PATCH] Tembak ke SOAP

---
 src/api/application/index.php            |   4 +-
 src/controller/ApplicationController.php | 151 ++++++++++++++-
 src/public/javascript/gym/application.js | 236 ++++++++++-------------
 3 files changed, 241 insertions(+), 150 deletions(-)

diff --git a/src/api/application/index.php b/src/api/application/index.php
index 768686b..460e960 100644
--- a/src/api/application/index.php
+++ b/src/api/application/index.php
@@ -4,7 +4,5 @@ require_once __DIR__ . "/../../controller/ApplicationController.php";
 require_once __DIR__ . "/../../controller/ControllerWrapper.php";
 
 $controller = new ApplicationController();
-$wrapper = new ControllerWrapper($controller);
-
-$wrapper->respond();
+$controller->respond();
 ?>
\ No newline at end of file
diff --git a/src/controller/ApplicationController.php b/src/controller/ApplicationController.php
index 60063e1..0ee7a63 100644
--- a/src/controller/ApplicationController.php
+++ b/src/controller/ApplicationController.php
@@ -8,23 +8,25 @@ require_once PROJECT_ROOT_PATH . "/services/GymService.php";
 class ApplicationController extends BaseController
 {
 
-    public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader)
+    public function respond()
     {
-
+        $strErrorDesc = '';
+        $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]);
         $arrQueryStringParams = $this->getQueryStringParams();
+        $responseData = [];
+        $id = $arrQueryStringParams['id'];
 
         switch ($requestMethod) {
             case 'GET':
-                $id = $arrQueryStringParams['id'];
                 if (isset($id)) {
-                    $soapurl = "http://localhost:3001/ws/application";
+                    $soapurl = "http://soap-web:3001/ws/application";
                     $soapkey = "sabuntuhbuatmandibukanbuatwebservice_php";
                     $gymId = $id;
 
                     $soapBody = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
                                     <Body>
                                         <getAllAplication xmlns="http://service.gymtracker.com/">
-                                            <gym_id xmlns="">37</gym_id>
+                                            <gym_id xmlns="">'. $gymId .'</gym_id>
                                         </getAllAplication>
                                     </Body>
                                 </Envelope>';
@@ -51,11 +53,6 @@ class ApplicationController extends BaseController
 
                     // Handle the response
                     if ($response) {
-                        // return $response;
-                        // $xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
-                        // $json = json_encode($xml);
-                        // $array = json_decode($json, TRUE);
-                        // $responseData = $array['Body']['getAllAplicationResponse']['return'];
                         $responseData = $response;
                     } else {
                         $responseData = [];
@@ -63,11 +60,145 @@ class ApplicationController extends BaseController
 
                 }
                 break;
+            
+            case 'PUT':
+                $username = $arrQueryStringParams['username'];
+                if (isset($username)) {
+                    $soapurl = "http://soap-web:3001/ws/application";
+                    $soapkey = "sabuntuhbuatmandibukanbuatwebservice_php";
+                    $gymId = $id;
+
+                    $soapBody = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
+                                    <Body>
+                                        <getAplication xmlns="http://service.gymtracker.com/">
+                                            <username xmlns="">' . $username .'</username>
+                                            <gym_id xmlns="">'. $gymId .'</gym_id>
+                                        </getAplication>
+                                    </Body>
+                                </Envelope>';
+
+                    $ch = curl_init();
+
+                    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+                        'Content-Type: text/xml',
+                        'Authorization: ' . $soapkey
+                    ));
+                    curl_setopt($ch, CURLOPT_URL, $soapurl);
+                    curl_setopt($ch, CURLOPT_POST, 1);
+                    curl_setopt($ch, CURLOPT_POSTFIELDS, $soapBody);
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+                    // give timeout of 10 seconds
+
+                    $response = curl_exec($ch);
+
+                    if (curl_errno($ch)) {
+                        echo 'Curl error: ' . curl_error($ch);
+                    }
+
+                    curl_close($ch);
+
+                    // Handle the response
+                    if ($response) {
+                        $responseData = $response;
+                    } else {
+                        $responseData = [];
+                    }
+
+                }
+
+            case 'POST':
+                $username = $arrQueryStringParams['username'];
+                if (isset($username)) {
+                    $soapurl = "http://soap-web:3001/ws/application";
+                    $soapkey = "sabuntuhbuatmandibukanbuatwebservice_php";
+                    $gymId = $id;
+
+                    $soapBody = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
+                                    <Body>
+                                        <accept xmlns="http://service.gymtracker.com/">
+                                            <username xmlns="">'. $username .'</username>
+                                            <gym_id xmlns="">'. $gymId .'</gym_id>
+                                        </accept>
+                                    </Body>
+                                </Envelope>';
+
+                    $ch = curl_init();
+
+                    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+                        'Content-Type: text/xml',
+                        'Authorization: ' . $soapkey
+                    ));
+                    curl_setopt($ch, CURLOPT_URL, $soapurl);
+                    curl_setopt($ch, CURLOPT_POST, 1);
+                    curl_setopt($ch, CURLOPT_POSTFIELDS, $soapBody);
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+                    // give timeout of 10 seconds
+
+                    $response = curl_exec($ch);
+
+                    if (curl_errno($ch)) {
+                        echo 'Curl error: ' . curl_error($ch);
+                    }
+
+                    curl_close($ch);
+
+                    // Handle the response
+                    if ($response) {
+                        $responseData = $response;
+                    } else {
+                        $responseData = [];
+                    }
+                }
+            case 'PATCH':
+                $username = $arrQueryStringParams['username'];
+                if (isset($username)) {
+                    $soapurl = "http://soap-web:3001/ws/application";
+                    $soapkey = "sabuntuhbuatmandibukanbuatwebservice_php";
+                    $gymId = $id;
+
+                    $soapBody = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
+                                    <Body>
+                                        <reject xmlns="http://service.gymtracker.com/">
+                                            <username xmlns="">'. $username .'</username>
+                                            <gym_id xmlns="">'. $gymId .'</gym_id>
+                                        </reject>
+                                    </Body>
+                                </Envelope>';
+
+                    $ch = curl_init();
+
+                    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+                        'Content-Type: text/xml',
+                        'Authorization: ' . $soapkey
+                    ));
+                    curl_setopt($ch, CURLOPT_URL, $soapurl);
+                    curl_setopt($ch, CURLOPT_POST, 1);
+                    curl_setopt($ch, CURLOPT_POSTFIELDS, $soapBody);
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+                    // give timeout of 10 seconds
+
+                    $response = curl_exec($ch);
+
+                    if (curl_errno($ch)) {
+                        echo 'Curl error: ' . curl_error($ch);
+                    }
+
+                    curl_close($ch);
+
+                    // Handle the response
+                    if ($response) {
+                        $responseData = $response;
+                    } else {
+                        $responseData = [];
+                    }
+                }
 
             default:
                 $strErrorDesc = 'Method not supported';
                 $strErrorHeader = 'HTTP/1.1 400 Bad Request';
                 break;
         }
+        return $responseData;
     }
+
 }
\ No newline at end of file
diff --git a/src/public/javascript/gym/application.js b/src/public/javascript/gym/application.js
index a4dc01a..8833952 100644
--- a/src/public/javascript/gym/application.js
+++ b/src/public/javascript/gym/application.js
@@ -16,159 +16,121 @@ const response = `<?xml version="1.0"?>
 </S:Envelope>`
 
 function getGymApplication(gymId) {
-  // modifyGymApplicationList(filterApplication(parseXmlResponseList(response), 0));
-  // showGymTrainers(filterApplication(parseXmlResponseList(response), 1));
-
   const xhr = new XMLHttpRequest();
   xhr.onreadystatechange = function () {
-      if (this.readyState === 4) {
-          if (this.status === 200) {
-              const applications = parseXmlResponseList(this.responseText);
-              // alert("trainer : " + applications);
-              modifyGymApplicationList(filterApplication(applications, 0));
-              showGymTrainers(filterApplication(applications, 1));
-          } else {
-              const xml = this.responseXML;
-              console.log("error status : " + this.status + " error text : " + xml);
-          }
+    if (this.readyState === 4) {
+      if (this.status === 200) {
+        alert("application : " + this.responseText);
+        // modifyGymRateList(JSON.parse(this.responseText)["ratings"], username);
+      } else {
+        const json = JSON.parse(this.responseText);
+        alert(json["error"]);
       }
+    }
   };
 
-  const soapBody = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
-                      <Body>
-                          <getAllAplication xmlns="http://service.gymtracker.com/">
-                              <gym_id xmlns="">${gymId}</gym_id>
-                          </getAllAplication>
-                      </Body>
-                  </Envelope>`;
-  // alert("soapbody : " + soapBody);
-  xhr.open("POST", soapurl, true);
-  // no need to to make options method for cors because we use soap
-  // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
-  // xhr.setRequestHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
-  xhr.setRequestHeader("Content-Type", "text/xml");
-  xhr.setRequestHeader("Authorization", soapkey);
-  xhr.send(soapBody);
-  // const xhr = new XMLHttpRequest();
-  // xhr.onreadystatechange = function () {
-  //   if (this.readyState === 4) {
-  //     if (this.status === 200) {
-  //       alert("application : " + this.responseText);
-  //       // modifyGymRateList(JSON.parse(this.responseText)["ratings"], username);
-  //     } else {
-  //       const json = JSON.parse(this.responseText);
-  //       alert(json["error"]);
-  //     }
-  //   }
-  // };
-
-  // xhr.open("GET", `/api/application?id=${gymId}`, true);
-  // xhr.setRequestHeader("X-API-KEY", apikey);
-  // xhr.send();
+  xhr.open("GET", `/api/application?id=${gymId}`, true);
+  xhr.setRequestHeader("X-API-KEY", apikey);
+  xhr.send();
 }
 
 function getApplication(username, gymId) {
-  const xhr = new XMLHttpRequest();
-  xhr.onreadystatechange = function () {
+    const xhr = new XMLHttpRequest();
+    xhr.onreadystatechange = function () {
       if (this.readyState === 4) {
-          if (this.status === 200) {
-              const applications = parseXmlResponseList(this.responseText);
-              // alert("trainer : " + applications);
-              modifyApplication(applications[0]);
-          } else {
-              const xml = this.responseXML;
-              console.log("error status : " + this.status + " error text : " + xml);
-          }
+        if (this.status === 200) {
+          alert("application : " + this.responseText);
+          // modifyGymRateList(JSON.parse(this.responseText)["ratings"], username);
+        } else {
+          const json = JSON.parse(this.responseText);
+          alert(json["error"]);
+        }
       }
-  };
-
-  const soapBody = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
-                      <Body>
-                          <getAplication xmlns="http://service.gymtracker.com/">
-                              <username xmlns="">${username}</username>
-                              <gym_id xmlns="">${gymId}</gym_id>
-                          </getAplication>
-                      </Body>
-                  </Envelope>`;
-
-  xhr.open("POST", soapurl, true);
-  xhr.setRequestHeader("Content-Type", "text/xml");
-  xhr.setRequestHeader("Authorization", soapkey);
-  xhr.send(soapBody);
+    };
+  
+    xhr.open("PUT", `/api/application?id=${gymId}`, true);
+    xhr.setRequestHeader("X-API-KEY", apikey);
+    xhr.send();
 }
 
 function acceptApplication(gymId, username) {
-  hidePopupAcc()
-  const button = document.getElementById("button-acc-rej-trainer");
-  // hide button
-  button.style.display = "none";
-  const xhr = new XMLHttpRequest();
-  xhr.onreadystatechange = function () {
-      if (this.readyState === 4) {
-          if (this.status === 200) {
-              alert("accept with username: " + username + " and gym id: " + gymId + "\n" + "Email has been sent to the trainer");
-              // redirect to gym page /admin/gym/?gym_id=${gymId}
-              window.location.href = `/admin/gym/?gym_id=${gymId}`;
-          } else {
-              const xml = this.responseXML;
-              console.log("error status : " + this.status + " error text : " + xml);
-              alert("error status : " + this.status + " error text : " + xml);
-              // show button
-              button.style.display = "flex";
-          }
-      }
-  };
-
-  const soapBody = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
-                      <Body>
-                          <accept xmlns="http://service.gymtracker.com/">
-                              <username xmlns="">${username}</username>
-                              <gym_id xmlns="">${gymId}</gym_id>
-                          </accept>
-                      </Body>
-                  </Envelope>`;
-
-  xhr.open("POST", soapurl, true);
-  xhr.setRequestHeader("Content-Type", "text/xml");
-  xhr.setRequestHeader("Authorization", soapkey);
-  xhr.send(soapBody);
+//   hidePopupAcc()
+//   const button = document.getElementById("button-acc-rej-trainer");
+//   // hide button
+//   button.style.display = "none";
+//   const xhr = new XMLHttpRequest();
+//   xhr.onreadystatechange = function () {
+//       if (this.readyState === 4) {
+//           if (this.status === 200) {
+//               alert("accept with username: " + username + " and gym id: " + gymId + "\n" + "Email has been sent to the trainer");
+//               // redirect to gym page /admin/gym/?gym_id=${gymId}
+//               window.location.href = `/admin/gym/?gym_id=${gymId}`;
+//           } else {
+//               const xml = this.responseXML;
+//               console.log("error status : " + this.status + " error text : " + xml);
+//               alert("error status : " + this.status + " error text : " + xml);
+//               // show button
+//               button.style.display = "flex";
+//           }
+//       }
+//   };
+
+    const xhr = new XMLHttpRequest();
+    xhr.onreadystatechange = function () {
+    if (this.readyState === 4) {
+        if (this.status === 200) {
+        alert("application : " + this.responseText);
+        // modifyGymRateList(JSON.parse(this.responseText)["ratings"], username);
+        } else {
+        const json = JSON.parse(this.responseText);
+        alert(json["error"]);
+        }
+    }
+    };
+
+    xhr.open("POST", `/api/application?id=${gymId}`, true);
+    xhr.setRequestHeader("X-API-KEY", apikey);
+    xhr.send();
 }
 
 function rejectApplication(gymId, username) {
-  hidePopupRej()
-  const button = document.getElementById("button-acc-rej-trainer");
-  // hide button
-  button.style.display = "none";
-  const xhr = new XMLHttpRequest();
-  xhr.onreadystatechange = function () {
-      if (this.readyState === 4) {
-          if (this.status === 200) {
-              alert("reject with username: " + username + " and gym id: " + gymId + "\n" + "Email has been sent to the trainer");
-              // redirect to gym page /admin/gym/?gym_id=${gymId}
-              window.location.href = `/admin/gym/?gym_id=${gymId}`;
-          } else {
-              const xml = this.responseXML;
-              console.log("error status : " + this.status + " error text : " + xml);
-              alert("error status : " + this.status + " error text : " + xml);
-              // show button
-              button.style.display = "flex";
-          }
-      }
-  };
-
-  const soapBody = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
-                      <Body>
-                          <reject xmlns="http://service.gymtracker.com/">
-                              <username xmlns="">${username}</username>
-                              <gym_id xmlns="">${gymId}</gym_id>
-                          </reject>
-                      </Body>
-                  </Envelope>`;
-
-  xhr.open("POST", soapurl, true);
-  xhr.setRequestHeader("Content-Type", "text/xml");
-  xhr.setRequestHeader("Authorization", soapkey);
-  xhr.send(soapBody);
+//   hidePopupRej()
+//   const button = document.getElementById("button-acc-rej-trainer");
+//   // hide button
+//   button.style.display = "none";
+//   const xhr = new XMLHttpRequest();
+//   xhr.onreadystatechange = function () {
+//       if (this.readyState === 4) {
+//           if (this.status === 200) {
+//               alert("reject with username: " + username + " and gym id: " + gymId + "\n" + "Email has been sent to the trainer");
+//               // redirect to gym page /admin/gym/?gym_id=${gymId}
+//               window.location.href = `/admin/gym/?gym_id=${gymId}`;
+//           } else {
+//               const xml = this.responseXML;
+//               console.log("error status : " + this.status + " error text : " + xml);
+//               alert("error status : " + this.status + " error text : " + xml);
+//               // show button
+//               button.style.display = "flex";
+//           }
+//       }
+//   };
+
+    const xhr = new XMLHttpRequest();
+        xhr.onreadystatechange = function () {
+        if (this.readyState === 4) {
+            if (this.status === 200) {
+            alert("application : " + this.responseText);
+            // modifyGymRateList(JSON.parse(this.responseText)["ratings"], username);
+            } else {
+            const json = JSON.parse(this.responseText);
+            alert(json["error"]);
+            }
+        }
+        };
+    
+        xhr.open("PATCH", `/api/application?id=${gymId}`, true);
+        xhr.setRequestHeader("X-API-KEY", apikey);
+        xhr.send();
 }
 
 function modifyGymApplicationList(data) {
-- 
GitLab