From 3389d546355c0be1dddd34bec002e31b3f6b8abc Mon Sep 17 00:00:00 2001
From: Addin Munawwar <91366027+moonawar@users.noreply.github.com>
Date: Fri, 17 Nov 2023 13:52:48 +0700
Subject: [PATCH] feat: integrate php-soap page

---
 app/clients/SoapConsumer.php          | 160 ++------------------------
 app/controllers/UserController.php    |  21 +++-
 app/pages/user/MySubscriptionPage.php |  47 +++++---
 3 files changed, 63 insertions(+), 165 deletions(-)

diff --git a/app/clients/SoapConsumer.php b/app/clients/SoapConsumer.php
index 02be5ae..614d026 100644
--- a/app/clients/SoapConsumer.php
+++ b/app/clients/SoapConsumer.php
@@ -12,9 +12,9 @@ class SoapConsumer {
             'exceptions' => true, // Enables exceptions for HTTP errors
             "encoding" => "UTF-8",
             "stream_context" => stream_context_create([
-                'http' => [
-                    'ApiKey' => $_ENV['SOAP_API_KEY']
-                ]
+                'http' => array(
+                    'header' => 'ApiKey: ' . $_ENV['SOAP_API_KEY'],
+                ),
             ])
         ];
 
@@ -22,43 +22,6 @@ class SoapConsumer {
         $this->requestClient = new SoapClient($_ENV['SOAP_URL'] . "request?wsdl", $options);    
 
         $this->apiKey = $_ENV['SOAP_API_KEY'];
-
-        $headerBody = new SoapVar($this->apiKey, XSD_STRING, null, null, 'ApiKey', 'http://ws.soap.com/');
-        $header = new SoapHeader('http://ws.soap.com/', 'ApiKey', $headerBody);
-
-        $this->subscriptionClient->__setSoapHeaders(array(
-            new SoapHeader(
-                'http://ws.soap.com/',
-                'ApiKey',
-                $this->apiKey,
-                false
-            ),
-            new SoapHeader(
-                'ns2',
-                'ApiKey',
-                $this->apiKey,
-                false
-            ),
-            new SoapHeader(
-                'ns1',
-                'ApiKey',
-                $this->apiKey,
-                false
-            ),
-            $header
-        ));
-
-        $this->subscriptionClient->__setSoapHeaders($header);
-
-        $this->requestClient->__setSoapHeaders(
-            new SoapHeader(
-                'http://ws.soap.com/',
-                'ApiKey',
-                $this->apiKey,
-                false
-            )
-        );
-
     }
 
     public function makeRequest($requestBy, $to, $requesterEmail) {
@@ -68,121 +31,22 @@ class SoapConsumer {
             'RequesterEmail' => $requesterEmail,
         ]);
 
-        
-
-        return $response;
-
-        // $apiKey = $this->apiKey;
-
-        // // Construct SOAP request XML
-        // $requestXml = <<<XML
-        // <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
-        //     <Header>
-        //         <ApiKey xmlns="http://ws.soap.com/">$apiKey</ApiKey>
-        //     </Header>
-        //     <Body>
-        //         <MakeRequest xmlns="http://ws.soap.com/">
-        //             <RequestBy xmlns="">$requestBy</RequestBy>
-        //             <To xmlns="">$to</To>
-        //             <RequesterEmail xmlns="">$requesterEmail</RequesterEmail>
-        //         </MakeRequest>
-        //     </Body>
-        // </Envelope>
-        // XML;
-
-        // $endpoint = $_ENV['SOAP_URL'] . "request";
-
-        // $headers = [
-        //     'Content-Type: text/xml; charset=utf-8',
-        //     'Content-Length: ' . strlen($requestXml),
-        //     'SOAPAction: http://ws.soap.com/MakeRequest',
-        // ];
-
-        // $ch = curl_init($endpoint);
-        // curl_setopt($ch, CURLOPT_POST, 1);
-        // curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml);
-        // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        $soapData = json_encode($response);
+        $soapData = json_decode($soapData, true);
 
-        // // Send SOAP request
-        // $responseXml = curl_exec($ch);
-        // curl_close($ch);
-
-        // return $responseXml;
+        return $soapData;
     }
 
     public function getSubscriptionOf($requester) {
-
-        // Define the SOAP request parameters
-        $requestParams = array(
-            'ApiKey' => $this->apiKey,
-            'GetSubscriptionOf' => array(
-                'Username' => $requester,
-            ),
-        );
-
-        $res = $this->subscriptionClient->__soapCall('GetSubscriptionOf', array($requestParams));
-    
-        // Process the result
-        var_dump($res);
-
  
-        // $response = $this->subscriptionClient->GetSubscriptionOf([
-        //     'Username' => $requester,
-        // ]);
-
-        // echo htmlentities( $this->subscriptionClient->__getLastRequest());
-
-        // return $response;
-        // $apiKey = $this->apiKey;
-
-        // // Construct SOAP request XML
-        // $requestXml = <<<XML
-        // <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
-        //     <Header>
-        //         <ApiKey xmlns="http://ws.soap.com/">$apiKey</ApiKey>
-        //     </Header>
-        //     <Body>
-        //         <GetSubscriptionOf xmlns="http://ws.soap.com/">
-        //             <Username xmlns="">$requester</Username>
-        //         </GetSubscriptionOf>
-        //     </Body>
-        // </Envelope>
-        // XML;
-
-        // $endpoint = $_ENV['SOAP_URL'] . "subscription";
-
-        // $headers = [
-        //     'Content-Type: text/xml; charset=utf-8',
-        //     'Content-Length: ' . strlen($requestXml),
-        //     'SOAPAction: http://ws.soap.com/GetSubscriptionOf',
-        // ];
-
-        // $ch = curl_init($endpoint);
-        // curl_setopt($ch, CURLOPT_POST, 1);
-        // curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml);
-        // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        $response = $this->subscriptionClient->GetSubscriptionOf([
+            'Username' => $requester,
+        ]);
 
-        // // Send SOAP request
-        // $dataString = curl_exec($ch);
-        // curl_close($ch);
-        
-        // // Use regular expression to extract usernames
-        // preg_match_all('/\+07:00(.*?)true/', $dataString, $matches);
+        $soapData = json_encode($response);
+        $soapData = json_decode($soapData, true);
 
-        // // Extract the usernames from the matches array
-        // $usernames = $matches[1];
-        // for ($i = 0; $i < count($usernames); $i++) {
-        //     // if the username started with 2023
-        //     if (substr($usernames[$i], 0, 4) == "2023") {
-        //         $usernames[$i] = substr($usernames[$i], 29);
-        //     }
-        // }
-        
-        // // Print the result
-        // echo "usernames = [" . implode(', ', $usernames) . "]\n";
-        // return $dataString;
+        return $soapData;
     }
 }
 ?>
\ No newline at end of file
diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php
index 7898e91..43bcede 100644
--- a/app/controllers/UserController.php
+++ b/app/controllers/UserController.php
@@ -25,11 +25,30 @@ class UserController extends Controller implements ControllerInterface
         }
 
         switch ($_SERVER['REQUEST_METHOD']) {
+            case 'POST':
+                require_once __DIR__ . '/../clients/SOAPConsumer.php';
+                $soap  = new SoapConsumer();
+
+                $user = $this->model->getUserByUsername($_SESSION['username']);
+                $userEmail = $user['email'];
+
+                $soapData = $soap->makeRequest($_SESSION['username'], $_POST['curator'], $userEmail);
+
+                echo $soapData["Response"]["message"];
+
             case 'GET':
                 // show the login page
+                require_once __DIR__ . '/../clients/SOAPConsumer.php';
+                $soap  = new SoapConsumer();
+                $soapData = $soap->getSubscriptionOf($_SESSION['username']);
+
+                $subData = $soapData["Response"]["data"];
                 $data = [
-                    'username' => $_SESSION['username']
+                    'username' => $_SESSION['username'],
+                    'subscription' => $subData
                 ];
+
+
                 $mySubscriptionView = $this->view('user', 'MySubscriptionView', $data);
                 $mySubscriptionView->render();
                 break;
diff --git a/app/pages/user/MySubscriptionPage.php b/app/pages/user/MySubscriptionPage.php
index 1621d8b..b5c2a14 100644
--- a/app/pages/user/MySubscriptionPage.php
+++ b/app/pages/user/MySubscriptionPage.php
@@ -21,26 +21,41 @@
     <!-- Navigation bar -->
     <?php include(dirname(__DIR__) . '../../components/Navbar.php') ?>
     <div class="content">
-        <h1><?php $this->data['username']?>'s Subscription</h1>
+        <h1><?php echo $this->data['username']?>'s Subscription</h1>
         <!-- Todo Routing -->
-        <a href="/premium">++ Add Subscription</a>  
-        <table border="1" class="styled-table">
-            <thead>
-                <tr>
-                    <th>Curator Name</th>
-                    <th>Action</th>
-                </tr>
-            </thead>
-            <?php
-            
-            $authors = $this->data['authors'];
-
-            foreach ($authors as $author) {
+        <a href="/premium">+ Add Subscription</a>  
+        <?php
+            if (count($this->data['subscription']) > 0) {
+                echo "<table border=\"1\" class=\"styled-table\">";
+                echo "<thead>";
                 echo "<tr>";
-                echo "<td>" . $author['full_name'] . "</td>";
-                echo '<td><a href="/author/update/' . $author['author_id'] .'">Pending/Details</a></td>';
+                echo "<th>Curator Name</th>";
+                echo "<th>Action</th>";
                 echo "</tr>";
+                echo "</thead>";
+            } else {
+                echo "<h2>You have no subscription</h2>";
+            }
+        ?>
+  
+            <?php
+            if (count($this->data['subscription']) > 0) {
+                $subs = $this->data['subscription']['item'];
+
+                foreach ($subs as $sub) {
+
+                    $raw_data = file_get_contents('http://host.docker.internal:8040/api/curator/' . $sub['curator']);
+                    $data = json_decode($raw_data, true);
+                    echo var_dump($data);
+                    $colId = $data['collectionId'];
+
+                    echo "<tr>";
+                    echo "<td>" . $sub['curator'] . "</td>";
+                    echo '<td><a href="/premium/detail/' . $colId . '">Details</a></td>';
+                    echo "</tr>";
+                }
             }
+
             ?>
         </table>
     </div>
-- 
GitLab