<?php
// Lakukan pemanggilan SOAP apabila ada permintaan berupa form yang disubmit
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
  $get = 0;
  $post = 0;
  $delete = 0;
  $put = 0;
  // Search Quotes
  if (isset($_POST['keywordQuote']) && (trim($_POST['keywordQuote']) != ""))
  {
    $keyword = $_POST['keywordQuote'];
    $result = new HttpRequest("http://lecturer.ukdw.ac.id/yuan/labs/quotes/find/$keyword", HttpRequest::METH_GET);
    $get = 1;
  }
  else if (isset($_POST['idQuote']) && (trim($_POST['idQuote']) != ""))
  {
    $id = $_POST['idQuote'];
    $result = new HttpRequest("http://lecturer.ukdw.ac.id/yuan/labs/quotes/id/$id", HttpRequest::METH_GET);
    $get = 1;
  }
  else if (isset($_POST['authorQuote']) && (trim($_POST['authorQuote']) != ""))
  {
    $author = $_POST['authorQuote'];
    $result = new HttpRequest("http://lecturer.ukdw.ac.id/yuan/labs/quotes/author/$author", HttpRequest::METH_GET);
    $get = 1;
  }
  // Add New Quote
  else if (isset($_POST['authorQuotePost']) && (trim($_POST['authorQuotePost']) != "") && (trim($_POST['quotePost']) != ""))
  {
    $newQuote = array("author", "quote");
    $newQuote['author'] = $_POST['authorQuotePost'];
    $newQuote['quote'] = $_POST['quotePost'];
    $result = new HttpRequest("http://lecturer.ukdw.ac.id/yuan/labs/quotes/", HttpRequest::METH_POST);
    $result->setPostFields($newQuote);
    $post= 1;
  }
  // Delete
  else if (isset($_POST['idQuoteDelete']) && (trim($_POST['idQuoteDelete']) != ""))
  {
    $id = $_POST['idQuoteDelete'];
    $result = new HttpRequest("http://lecturer.ukdw.ac.id/yuan/labs/quotes/$id", HttpRequest::METH_DELETE);
    $delete = 1;
  }
  // Update
  else if (isset($_POST['idQuoteChange']) && (trim($_POST['idQuoteChange']) != "") && (trim($_POST['authorQuoteChange']) != "") && (trim($_POST['quoteChange']) != ""))
  {
    $id = $_POST['idQuoteChange'];
    $author = $_POST['authorQuoteChange'];
    $quote = $_POST['quoteChange'];
    $content = "id=" . $id . "&author=" . $author . "&quote=" . $quote;
    $result = new HttpRequest("http://lecturer.ukdw.ac.id/yuan/labs/quotes/", HttpRequest::METH_PUT);
    $result->setPutData($content);
    $put = 1;
  }

  if ($get) {
    try {
    $result->send();
    if ($result->getResponseCode() == 200) {
	$xml = $result->getResponseBody();
	$xmlResult = simplexml_load_string($xml);
	foreach($xmlResult->quote as $k=>$v) {
	  echo "ID : " . $v->id . "<br/>";;
	  echo "Quote : " .  $v->content . "<br/>";
	  echo "Author : " . $v->author . "<br/>";
	  echo "<hr/>";
	}
      }
    } 
    catch (HttpException $ex) {
      echo $ex;
    }
  }
  else if ($post){
    try {
    $result->send();
    if ($result->getResponseCode() == 200) 
	echo "Quote berhasil ditambahkan";
    else
	echo "Quote gagal ditambahkan";
    }
    catch (HttpException $ex) {
      echo $ex;
    }
  }
  else if ($delete){
    try {
    $result->send();
    if ($result->getResponseCode() == 200) 
	echo "Quote berhasil dihapus";
    else
	echo "Quote gagal dihapus";
    }
    catch (HttpException $ex) {
      echo $ex;
    }
  }
  else if ($put){
    try {
    $result->send();
    if ($result->getResponseCode() == 200) 
	echo "Quote berhasil diupdate";
    else
	echo "Quote gagal diupdate";
    }
    catch (HttpException $ex) {
      echo $ex;
    }
  }
}
else // Tidak ada proses submit, berarti tampilkan form
{
?>
<html>
<head>
<title>Contoh Akses Web Service Berbasis RESTful API</title>
</head>
<body>
<h2>Mencari Quote</h2>
<form action="rest.php" method="post">
    Masukkan Keyword Quotes
    <input id="keywordQuote" name="keywordQuote" size="50" type="text"/>
    <input id="submit" name="submit" type="submit" value="submit" />
  </form>

<form action="rest.php" method="post">
    Masukkan Nomor ID quotes
    <input id="idQuote" name="idQuote" size="50" type="text"/>
    <input id="submit" name="submit" type="submit" value="submit" />
  </form>

<form action="rest.php" method="post">
    Masukkan Nama Pembuat quote
    <input id="authorQuote" name="authorQuote" size="50" type="text"/>
    <input id="submit" name="submit" type="submit" value="submit" />
  </form>

<h2>Menambahkan Quote Baru</h2>
<form action="rest.php" method="post" enctype="multipart/form-data">
    Masukkan Nama Author <input id="authorQuotePost" name="authorQuotePost" size="50" type="text"/><br/>
    Masukkan Quote Anda <input id="quotePost" name="quotePost" size="50" type="text"/>
    <input id="submit" name="submit" type="submit" value="submit" />
  </form>

<h2>Menghapus Quote</h2>
<form action="rest.php" method="post"">
    Masukkan ID Quote <input id="idQuoteDelete" name="idQuoteDelete" size="50" type="text"/>
    <input id="submit" name="submit" type="submit" value="submit" />
  </form>

<h2>Mengganti Quote</h2>
<form action="rest.php" method="post">
    Masukkan ID Quote <input id="idQuoteChange" name="idQuoteChange" size="50" type="text"/><br/>
    Masukkan Nama Author Baru <input id="authorQuoteChange" name="authorQuoteChange" size="50" type="text"/><br/>
    Masukkan Quote Baru <input id="quoteChange" name="quoteChange" size="50" type="text"/>
    <input id="submit" name="submit" type="submit" value="submit" />
  </form>
</body>
</html>
<?php
}
?>
