From d9134d810d306cf81513a4c9415a90af4976de76 Mon Sep 17 00:00:00 2001 From: afnanramadhan <13521011@std.stei.itb.ac.id> Date: Thu, 16 Nov 2023 03:37:07 +0700 Subject: [PATCH] feat: connect fact to soap --- app/views/fact/index.php | 23 +- public/css/fact.css | 5 +- public/js/editfact.js | 2 +- public/js/fact.js | 381 +++++++++++----------- server/controller/auth/EditFact.php | 22 ++ server/controller/auth/Fact.php | 478 ++++++++++++++++++++++++++-- 6 files changed, 678 insertions(+), 233 deletions(-) create mode 100644 server/controller/auth/EditFact.php diff --git a/app/views/fact/index.php b/app/views/fact/index.php index 9f09dd6..a01cfaf 100644 --- a/app/views/fact/index.php +++ b/app/views/fact/index.php @@ -5,7 +5,7 @@ <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="../../../public/css/fact.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"> - <!-- <script defer src="../../../public/js/fact.js"></script> --> + <script defer src="../../../public/js/fact.js"></script> <script>window.onload = function() {showAll();};</script> <title>NutriCraft</title> </head> @@ -18,19 +18,32 @@ <i class="fas fa-search"></i> <input class="searchinput" id="searchinput" type="text" placeholder="Search" onkeyup=searchDebounce()> </div> - <div class="sortcontainer"> + <!-- <div class="sortcontainer"> <img src="../../../assets/sort.png" alt=""> <select name="sortby" id="pet-select" onclick=Search()> <option value="Alphabet" class="alpha">Alphabet</option> <option value="Newest">Newest</option> <option value="Oldest">Oldest</option> </select> - </div> + </div> --> </div> <div class="buttons"> - <button type="button" class="all" id="selected" >All</button> - <button type="button" class="subscribed">Subscribed</button> + <button type="button" class="all" id="selected" onclick=Search()>All</button> + <button type="button" class="subscribed" onclick=Search()>Subscribed</button> </div> + <script> + const buttons = document.querySelectorAll('.buttons button'); + console.log(buttons); + buttons.forEach(button => { + button.addEventListener('click', () => { + const currentlySelectedButton = document.querySelector('#selected'); + currentlySelectedButton.removeAttribute('id'); + button.id = 'selected'; + buttons.forEach(btn => btn.classList.remove('selected')); + button.classList.add('selected'); + }); + }); + </script> <div id="factContent" class="factcontent"> <a href="/?detailfact"> <div class="factcard"> diff --git a/public/css/fact.css b/public/css/fact.css index 10d3d72..640d636 100644 --- a/public/css/fact.css +++ b/public/css/fact.css @@ -143,7 +143,10 @@ a { color: #EF4800; } - +.image{ + max-height: 200px; /* Set the maximum height you want */ + width: auto; /* Ensure that the width adjusts proportionally */ +} diff --git a/public/js/editfact.js b/public/js/editfact.js index 69f27b9..9572fe8 100644 --- a/public/js/editfact.js +++ b/public/js/editfact.js @@ -1,7 +1,7 @@ function loadpage(id) { console.log(id); const xhttp = new XMLHttpRequest(); - xhttp.open('POST', "../../server/controller/auth/DetailFact.php", true); + xhttp.open('POST', "../../server/controller/auth/EditFact.php", true); xhttp.onload = function() { let response = this.response; // console.log(response); diff --git a/public/js/fact.js b/public/js/fact.js index 27bb0a2..0685d52 100644 --- a/public/js/fact.js +++ b/public/js/fact.js @@ -1,26 +1,26 @@ -function toggleVideo(card) { - var content = card.querySelector('.video-content'); // Use card parameter to find .video-content within the clicked card - // console.log(content) - if (content && content.style.maxHeight) { // Check if content is not null - content.style.maxHeight = null; - } else if (content) { // Check if content is not null - content.style.maxHeight = "50vw"; - } -} +// function toggleVideo(card) { +// var content = card.querySelector('.video-content'); // Use card parameter to find .video-content within the clicked card +// // console.log(content) +// if (content && content.style.maxHeight) { // Check if content is not null +// content.style.maxHeight = null; +// } else if (content) { // Check if content is not null +// content.style.maxHeight = "50vw"; +// } +// } let Page = 1; let TotalPage; // Add a click event listener to a parent container (e.g., isicontent) -document.getElementById("isicontent").addEventListener("click", function(event) { - const target = event.target; +// document.getElementById("isicontent").addEventListener("click", function(event) { +// const target = event.target; - // Check if the clicked element has the class .video-card - if (target.classList.contains("video-card")) { - toggleVideo(target); - } -}); +// // Check if the clicked element has the class .video-card +// if (target.classList.contains("video-card")) { +// toggleVideo(target); +// } +// }); function capitalizeWords(str) { @@ -29,198 +29,177 @@ function capitalizeWords(str) { // masi salah -const pagination = () => { - const pageNumber = "pageNumber"; - const search = document.getElementById('searchinput').value; - const xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState === 4){ - let response = this.response; - const startIndex = response.indexOf('['); - const jsonStr = response.substring(startIndex); - const jsonObject = JSON.parse(jsonStr); +// const pagination = () => { +// const pageNumber = "pageNumber"; +// const search = document.getElementById('searchinput').value; +// const xhttp = new XMLHttpRequest(); +// xhttp.onreadystatechange = function() { +// if (this.readyState === 4){ +// let response = this.response; +// const startIndex = response.indexOf('['); +// const jsonStr = response.substring(startIndex); +// const jsonObject = JSON.parse(jsonStr); - const numberpage = document.getElementById('numberpage'); - TotalPage=Math.ceil(jsonObject.length / 2); - let html = ""; - for (let i = 1; i <= Math.ceil(jsonObject.length / 2); i++) { - if(i == 1){ - html += `<button type='button' class="page" value=${i} id='selected' onclick='selectPage(); getPage(${i});' ">${i}</button>`; - }else{ - html += `<button type='button' class="page" value=${i} onclick='selectPage(); getPage(${i});' ">${i}</button>`; - } - } - numberpage.innerHTML = html; - } - }; - xhttp.open('GET', `../../server/controller/auth/Fact.php?pageNumber=${pageNumber}&search=${search}`, true); - xhttp.send(); -} +// const numberpage = document.getElementById('numberpage'); +// TotalPage=Math.ceil(jsonObject.length / 2); +// let html = ""; +// for (let i = 1; i <= Math.ceil(jsonObject.length / 2); i++) { +// if(i == 1){ +// html += `<button type='button' class="page" value=${i} id='selected' onclick='selectPage(); getPage(${i});' ">${i}</button>`; +// }else{ +// html += `<button type='button' class="page" value=${i} onclick='selectPage(); getPage(${i});' ">${i}</button>`; +// } +// } +// numberpage.innerHTML = html; +// } +// }; +// xhttp.open('GET', `../../server/controller/auth/Fact.php?pageNumber=${pageNumber}&search=${search}`, true); +// xhttp.send(); +// } -// pagination(); const Search = () => { const search = capitalizeWords(document.getElementById('searchinput').value); - const select = document.getElementById('pet-select').value; + const select = document.getElementById('selected').innerText; + // console.log(select); xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState === 4){ - let response = this.response; - const startIndex = response.indexOf('['); - const jsonStr = response.substring(startIndex); - const jsonObject = JSON.parse(jsonStr); - - // console.log(jsonStr); - + const jsonObject = JSON.parse(response); + // console.log(response); + console.log(jsonObject); - const parentElement = document.getElementById("isicontent"); - + const parentElement = document.getElementById("factContent"); let html = ""; - for (let i = 0; i < jsonObject.length; i++) { - const content = jsonObject[i]; + for (let i = 0; i < jsonObject['data'].length; i++) { + const content = jsonObject['data'][i]; html += ` - <div class="video-card" onclick="toggleVideo(this)"> - <div class="cardcontent"> - <img src="${content.path_photo}" alt=""> - <div class="card-title"> - <h3>${content.title}</h3> + <a href="/?detailfact&id=${content.id}"> + <div class="factcard"> + <img src="${content.path_photo}" alt="" class="image"> + <div class="facttext"> + <h2>${content.title}</h2> <p>${content.highlight}</p> - </div> + <h4>${content.author}</h4> + </div> </div> - <div class="video-content"> - <video src="${content.path_file}" controls></video> - </div> - </div> - `; - } - - parentElement.innerHTML = html; + </a> + `; } - - - pagination(); - }; - xhttp.open('GET', `../../server/controller/auth/Fact.php?search=${search}&select=${select}&page=${Page}`, true); + parentElement.innerHTML = html; + } + }; + xhttp.open('GET', `../../server/controller/auth/Fact.php?search=${search}&select=${select}`, true); xhttp.send(JSON.stringify({search: search, select: select})); } const showAll = () => { const show = "all"; - const select = document.getElementById('pet-select').value; // console.log(Page); xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState === 4){ let response = this.response; - const startIndex = response.indexOf('['); - const jsonStr = response.substring(startIndex); - const jsonObject = JSON.parse(jsonStr); - // console.log(jsonStr); - - - const parentElement = document.getElementById("isicontent"); + const jsonObject = JSON.parse(response); + console.log(jsonObject); + // console.log(response); + const parentElement = document.getElementById("factContent"); let html = ""; - for (let i = 0; i < jsonObject.length; i++) { - const content = jsonObject[i]; - html += ` - <div class="video-card" onclick="toggleVideo(this)"> - <div class="cardcontent"> - <img src="${content.path_photo}" alt=""> - <div class="card-title"> - <h2>${content.title}</h2> - <p>${content.highlight}</p> - </div> - </div> - <div class="video-content"> - <video src="${content.path_file}" controls></video> + for (let i = 0; i < jsonObject['data'].length; i++) { + const content = jsonObject['data'][i]; + html += ` + <a href="/?detailfact&id=${content.id}"> + <div class="factcard"> + <img src="${content.path_photo}" alt="" class="image"> + <div class="facttext"> + <h2>${content.title}</h2> + <p>${content.highlight}</p> + <h4>${content.author}</h4> + </div> </div> - </div> - `; - } - {/* <div class="video-content"> - <iframe src="https://www.youtube.com/embed/l970HoJ7g7o?si=61k4a2ioQf4YfpFF" frameborder="0" allowfullscreen></iframe> - </div> */} + </a> + `; + } parentElement.innerHTML = html; - pagination(); } }; - xhttp.open('GET', `../../server/controller/auth/Fact.php?show=${show}&Select=${select}&page=${Page}`, true); + xhttp.open('GET', `../../server/controller/auth/Fact.php?show=${show}`, true); xhttp.send(); } -const selectpagination = () => { - const show = "all"; - const select = document.getElementById('pet-select').value; - // console.log(Page); - xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState === 4){ - let response = this.response; - const startIndex = response.indexOf('['); - const jsonStr = response.substring(startIndex); - const jsonObject = JSON.parse(jsonStr); - // console.log(jsonStr); +// const selectpagination = () => { +// const show = "all"; +// const select = document.getElementById('pet-select').value; +// // console.log(Page); +// xhttp = new XMLHttpRequest(); +// xhttp.onreadystatechange = function() { +// if (this.readyState === 4){ +// let response = this.response; +// const startIndex = response.indexOf('['); +// const jsonStr = response.substring(startIndex); +// const jsonObject = JSON.parse(jsonStr); +// // console.log(jsonStr); - const parentElement = document.getElementById("isicontent"); - let html = ""; - for (let i = 0; i < jsonObject.length; i++) { - const content = jsonObject[i]; - html += ` - <div class="video-card" onclick="toggleVideo(this)"> - <div class="cardcontent"> - <img src="${content.path_photo}" alt=""> - <div class="card-title"> - <h3>${content.title}</h3> - <p>${content.highlight}</p> - </div> - </div> - <div class="video-content"> - <video src="${content.path_file}" controls></video> - </div> - </div> - `; - } - {/* <div class="video-content"> - <iframe src="https://www.youtube.com/embed/l970HoJ7g7o?si=61k4a2ioQf4YfpFF" frameborder="0" allowfullscreen></iframe> - </div> */} - parentElement.innerHTML = html; - } - }; - xhttp.open('GET', `../../server/controller/auth/Fact.php?show=${show}&Select=${select}&page=${Page}`, true); - xhttp.send(); - -} - - -const prevPage = () =>{ - const search = document.getElementById('searchinput').value; - if(Page>1){ - Page-=1 - if(search==''){ - selectpagination(); - }else{ - Search(); - } - } -} - -const nextPage = () =>{ - const search = document.getElementById('searchinput').value; - if(Page<TotalPage){ - Page+=1 - // console.log(Page); - // console.log("HAI"); - if(search==''){ - showAll(); - }else{ - Search(); - } - } -} +// const parentElement = document.getElementById("isicontent"); +// let html = ""; +// for (let i = 0; i < jsonObject.length; i++) { +// const content = jsonObject[i]; +// html += ` +// <div class="video-card" onclick="toggleVideo(this)"> +// <div class="cardcontent"> +// <img src="${content.path_photo}" alt=""> +// <div class="card-title"> +// <h3>${content.title}</h3> +// <p>${content.highlight}</p> +// </div> +// </div> +// <div class="video-content"> +// <video src="${content.path_file}" controls></video> +// </div> +// </div> +// `; +// } +// {/* <div class="video-content"> +// <iframe src="https://www.youtube.com/embed/l970HoJ7g7o?si=61k4a2ioQf4YfpFF" frameborder="0" allowfullscreen></iframe> +// </div> */} +// parentElement.innerHTML = html; +// } +// }; +// xhttp.open('GET', `../../server/controller/auth/Fact.php?show=${show}&Select=${select}&page=${Page}`, true); +// xhttp.send(); + +// } + + +// const prevPage = () =>{ +// const search = document.getElementById('searchinput').value; +// if(Page>1){ +// Page-=1 +// if(search==''){ +// selectpagination(); +// }else{ +// Search(); +// } +// } +// } + +// const nextPage = () =>{ +// const search = document.getElementById('searchinput').value; +// if(Page<TotalPage){ +// Page+=1 +// // console.log(Page); +// // console.log("HAI"); +// if(search==''){ +// showAll(); +// }else{ +// Search(); +// } +// } +// } function getPage(pa){ Page = pa; @@ -236,36 +215,36 @@ function debounce(func, timeout = 500){ }; } -function selectPage (){ - const buttons = document.querySelectorAll('#numberpage button'); - // console.log(buttons); - // Add click event listeners to each button - buttons.forEach(button => { - button.addEventListener('click', () => { - // Remove the 'selected' id from the currently selected button - const currentlySelectedButton = document.querySelector('#selected'); - currentlySelectedButton.removeAttribute('id'); - - // Add the 'selected' id to the clicked button - button.id = 'selected'; - - // Optionally, you can add a CSS class to style the selected button - buttons.forEach(btn => btn.classList.remove('selected')); - button.classList.add('selected'); - }); - }); -} - - -document.getElementById('numberpage').addEventListener('click',()=>{ - const search = document.getElementById('searchinput').value; - if(search==''){ - console.log("HAI"); - selectpagination(); - }else{ - Search(); - } -}) +// function selectPage (){ +// const buttons = document.querySelectorAll('#numberpage button'); +// // console.log(buttons); +// // Add click event listeners to each button +// buttons.forEach(button => { +// button.addEventListener('click', () => { +// // Remove the 'selected' id from the currently selected button +// const currentlySelectedButton = document.querySelector('#selected'); +// currentlySelectedButton.removeAttribute('id'); + +// // Add the 'selected' id to the clicked button +// button.id = 'selected'; + +// // Optionally, you can add a CSS class to style the selected button +// buttons.forEach(btn => btn.classList.remove('selected')); +// button.classList.add('selected'); +// }); +// }); +// } + + +// document.getElementById('numberpage').addEventListener('click',()=>{ +// const search = document.getElementById('searchinput').value; +// if(search==''){ +// console.log("HAI"); +// selectpagination(); +// }else{ +// Search(); +// } +// }) const searchDebounce = debounce(() => Search()); \ No newline at end of file diff --git a/server/controller/auth/EditFact.php b/server/controller/auth/EditFact.php new file mode 100644 index 0000000..f07d78e --- /dev/null +++ b/server/controller/auth/EditFact.php @@ -0,0 +1,22 @@ +<?php + +use data\Content ; +require_once "../../handler/data/Content.php"; +require_once('../../db/Database.php'); + +$fact = new Content(); +$data = json_decode(file_get_contents("php://input"),true); + +if(isset($data)){ + $id = $data['id']; + echo "<script>console.log($id)</script>"; + $result = $fact -> FindById($id); + + echo json_encode($result); +} + + + + + +?> \ No newline at end of file diff --git a/server/controller/auth/Fact.php b/server/controller/auth/Fact.php index 049fa66..e7bfccd 100644 --- a/server/controller/auth/Fact.php +++ b/server/controller/auth/Fact.php @@ -1,40 +1,468 @@ <?php +$API = $_ENV['USER_KEY']; -use data\Content; -require_once('../../handler/data/Content.php'); -require_once('../../db/Database.php'); +// use data\Content; +// require_once('../../handler/data/Content.php'); +// require_once('../../db/Database.php'); -$content = new Content(); +// $content = new Content(); +// if(isset($_GET['select'])){ +// $select = $_GET['select']; +// $page = $_GET['page']; +// $page = ($page-1)*10; +// if(isset($_GET['search'])){ +// $search = $_GET['search']; +// $result = $content->FindByTitle($search, $select, $page); +// }else{ +// $result = $content->FindAllPaging($select, $page); +// } + +// echo json_encode($result); +// } + +// if(isset($_GET['show'])){ +// $page = $_GET['page']; +// $page = ($page-1)*2; +// $select = $_GET['Select']; +// $result = $content->FindAllPaging($select, $page); +// echo json_encode($result); +// } + +// if(isset($_GET['pageNumber'])){ +// if(isset($_GET['search'])){ +// $search = $_GET['search']; +// $result = $content->FindAllSearch($search); +// }else{ +// $result = $content->FindAll($search); +// } +// echo json_encode($result); + +/////////////////////////// if(isset($_GET['select'])){ $select = $_GET['select']; - $page = $_GET['page']; - $page = ($page-1)*10; - if(isset($_GET['search'])){ - $search = $_GET['search']; - $result = $content->FindByTitle($search, $select, $page); + $search = $_GET['search']; + if($search!=""){ + if($select=="Subscribed"){ + $uuid = getUUID(1); + $post_data = json_encode(array( + 'subscribers' => $uuid, + 'title' => $search, + )); + + $content = curl_init("http://nutricraft-express:8080/content/title/"); + curl_setopt($content, CURLOPT_RETURNTRANSFER, true); + curl_setopt($content, CURLOPT_POST, true); + curl_setopt($content, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($content, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + )); + $response = curl_exec($content); + if (curl_errno($content)) { + echo 'Curl error: ' . curl_error($content); + } + curl_close($content); + $data = json_decode($response, true); + $idPhotoArray = array_map(function($item) { + return $item['id_photo']; + }, $data['data']); + + $idCreatorArray = array_map(function($item) { + return $item['id_creator']; + }, $data['data']); + + $path_photo = array(); + $creatorArr = array(); + + for($i = 0; $i < count($idPhotoArray); $i++) { + $idPhoto = $idPhotoArray[$i]; + $photo = curl_init(); + curl_setopt($photo, CURLOPT_URL, "http://nutricraft-express:8080/image/$idPhoto?APIkey=$API"); + curl_setopt($photo, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($photo); + if (curl_errno($photo)) { + echo 'Curl error: ' . curl_error($photo); + } + curl_close($photo); + $tempResponse = json_decode($tempResponse, true); + $url = $tempResponse['data']['url']; + + $path_photo[$i] = $url; + } + for($i = 0; $i < count($idCreatorArray); $i++) { + $idCreator = $idCreatorArray[$i]; + $creator = curl_init(); + curl_setopt($creator, CURLOPT_URL, "http://nutricraft-express:8080/user/$idCreator?APIkey=$API"); + curl_setopt($creator, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($creator); + if (curl_errno($creator)) { + echo 'Curl error: ' . curl_error($creator); + } + curl_close($creator); + $tempResponse = json_decode($tempResponse, true); + $name = $tempResponse['data']['name']; + $creatorArr[$i] = $name; + } + + for($i = 0; $i < count($data['data']); $i++) { + $data['data'][$i]['path_photo'] = $path_photo[$i]; + $data['data'][$i]['author'] = $creatorArr[$i]; + } + + if ($data) { + echo json_encode($data); + } else { + echo 'API request failed'; + } + }else{ + if($select=="All"){ + $post_data = json_encode(array( + 'title' => $search, + )); + $content = curl_init("http://nutricraft-express:8080/content/title/?APIkey=$API"); + curl_setopt($content, CURLOPT_RETURNTRANSFER, true); + curl_setopt($content, CURLOPT_POST, true); + curl_setopt($content, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($content, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + )); + $response = curl_exec($content); + if (curl_errno($content)) { + echo 'Curl error: ' . curl_error($content); + } + curl_close($content); + $data = json_decode($response, true); + $idPhotoArray = array_map(function($item) { + return $item['id_photo']; + }, $data['data']); + + $idCreatorArray = array_map(function($item) { + return $item['id_creator']; + }, $data['data']); + + $path_photo = array(); + $creatorArr = array(); + + for($i = 0; $i < count($idPhotoArray); $i++) { + $idPhoto = $idPhotoArray[$i]; + $photo = curl_init(); + curl_setopt($photo, CURLOPT_URL, "http://nutricraft-express:8080/image/$idPhoto?APIkey=$API"); + curl_setopt($photo, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($photo); + if (curl_errno($photo)) { + echo 'Curl error: ' . curl_error($photo); + } + curl_close($photo); + $tempResponse = json_decode($tempResponse, true); + $url = $tempResponse['data']['url']; + + $path_photo[$i] = $url; + } + for($i = 0; $i < count($idCreatorArray); $i++) { + $idCreator = $idCreatorArray[$i]; + $creator = curl_init(); + curl_setopt($creator, CURLOPT_URL, "http://nutricraft-express:8080/user/$idCreator?APIkey=$API"); + curl_setopt($creator, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($creator); + if (curl_errno($creator)) { + echo 'Curl error: ' . curl_error($creator); + } + curl_close($creator); + $tempResponse = json_decode($tempResponse, true); + $name = $tempResponse['data']['name']; + $creatorArr[$i] = $name; + } + + for($i = 0; $i < count($data['data']); $i++) { + $data['data'][$i]['path_photo'] = $path_photo[$i]; + $data['data'][$i]['author'] = $creatorArr[$i]; + } + + if ($data) { + echo json_encode($data); + } else { + echo 'API request failed'; + } + } + } }else{ - $result = $content->FindAllPaging($select, $page); - } + if($select=="Subscribed"){ + + }else{ + $content = curl_init(); + curl_setopt($content, CURLOPT_URL, "http://nutricraft-express:8080/content?APIkey=$API"); + curl_setopt($content, CURLOPT_RETURNTRANSFER, 1); + + $response = curl_exec($content); + + if (curl_errno($content)) { + echo 'Curl error: ' . curl_error($content); + } + + curl_close($content); + + $data = json_decode($response, true); + // echo $data; + + $idPhotoArray = array_map(function($item) { + return $item['id_photo']; + }, $data['data']); + + $idCreatorArray = array_map(function($item) { + return $item['id_creator']; + }, $data['data']); + + $path_photo = array(); + $creatorArr = array(); + + for($i = 0; $i < count($idPhotoArray); $i++) { + $idPhoto = $idPhotoArray[$i]; + $photo = curl_init(); + curl_setopt($photo, CURLOPT_URL, "http://nutricraft-express:8080/image/$idPhoto?APIkey=$API"); + curl_setopt($photo, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($photo); + if (curl_errno($photo)) { + echo 'Curl error: ' . curl_error($photo); + } + curl_close($photo); + $tempResponse = json_decode($tempResponse, true); + $url = $tempResponse['data']['url']; + + $path_photo[$i] = $url; + } + for($i = 0; $i < count($idCreatorArray); $i++) { + $idCreator = $idCreatorArray[$i]; + $creator = curl_init(); + curl_setopt($creator, CURLOPT_URL, "http://nutricraft-express:8080/user/$idCreator?APIkey=$API"); + curl_setopt($creator, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($creator); + if (curl_errno($creator)) { + echo 'Curl error: ' . curl_error($creator); + } + curl_close($creator); + $tempResponse = json_decode($tempResponse, true); + $name = $tempResponse['data']['name']; + $creatorArr[$i] = $name; + } + + for($i = 0; $i < count($data['data']); $i++) { + $data['data'][$i]['path_photo'] = $path_photo[$i]; + $data['data'][$i]['author'] = $creatorArr[$i]; + } + + if ($data) { + echo json_encode($data); + } else { + echo 'API request failed'; + } + } + - echo json_encode($result); + } } if(isset($_GET['show'])){ - $page = $_GET['page']; - $page = ($page-1)*2; - $select = $_GET['Select']; - $result = $content->FindAllPaging($select, $page); - echo json_encode($result); + $content = curl_init(); + curl_setopt($content, CURLOPT_URL, "http://nutricraft-express:8080/content?APIkey=$API"); + curl_setopt($content, CURLOPT_RETURNTRANSFER, 1); + + $response = curl_exec($content); + + if (curl_errno($content)) { + echo 'Curl error: ' . curl_error($content); + } + + curl_close($content); + + $data = json_decode($response, true); + // echo $data; + + $idPhotoArray = array_map(function($item) { + return $item['id_photo']; + }, $data['data']); + + $idCreatorArray = array_map(function($item) { + return $item['id_creator']; + }, $data['data']); + + $path_photo = array(); + $creatorArr = array(); + + for($i = 0; $i < count($idPhotoArray); $i++) { + $idPhoto = $idPhotoArray[$i]; + $photo = curl_init(); + curl_setopt($photo, CURLOPT_URL, "http://nutricraft-express:8080/image/$idPhoto?APIkey=$API"); + curl_setopt($photo, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($photo); + if (curl_errno($photo)) { + echo 'Curl error: ' . curl_error($photo); + } + curl_close($photo); + $tempResponse = json_decode($tempResponse, true); + $url = $tempResponse['data']['url']; + + $path_photo[$i] = $url; + } + for($i = 0; $i < count($idCreatorArray); $i++) { + $idCreator = $idCreatorArray[$i]; + $creator = curl_init(); + curl_setopt($creator, CURLOPT_URL, "http://nutricraft-express:8080/user/$idCreator?APIkey=$API"); + curl_setopt($creator, CURLOPT_RETURNTRANSFER, 1); + $tempResponse = curl_exec($creator); + if (curl_errno($creator)) { + echo 'Curl error: ' . curl_error($creator); + } + curl_close($creator); + $tempResponse = json_decode($tempResponse, true); + $name = $tempResponse['data']['name']; + $creatorArr[$i] = $name; + } + + for($i = 0; $i < count($data['data']); $i++) { + $data['data'][$i]['path_photo'] = $path_photo[$i]; + $data['data'][$i]['author'] = $creatorArr[$i]; + } + + if ($data) { + echo json_encode($data); + } else { + echo 'API request failed'; + } } -if(isset($_GET['pageNumber'])){ - if(isset($_GET['search'])){ - $search = $_GET['search']; - $result = $content->FindAllSearch($search); - }else{ - $result = $content->FindAll($search); +function getUUID($idSubs) { + $serviceUrl = $_ENV['SOAP_URL_SUBSCRIBE']."?APIkey=".$_ENV["SOAP_KEY"]; + + $soapRequest = ' + <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> + <Body> + <getCreators xmlns="http://Services.nutricraft.org/"> + <arg0 xmlns="">'.$idSubs.'</arg0> + </getCreators> + </Body> + </Envelope>'; + + $options = [ + CURLOPT_URL => $serviceUrl, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $soapRequest, + CURLOPT_HTTPHEADER => [ + 'Content-Type: text/xml; charset=utf-8', + 'Content-Length: ' . strlen($soapRequest), + ], + ]; + + $curl = curl_init(); + curl_setopt_array($curl, $options); + + $response = curl_exec($curl); + + curl_close($curl); + + if (curl_errno($curl)) { + echo 'cURL Error: ' . curl_error($curl); + } else { + $xml = simplexml_load_string($response); + $ret = array(); + foreach ($xml->xpath('//return') as $value) { + $ret[] = (string)$value; + } + return $ret; } - echo json_encode($result); -} \ No newline at end of file +} + +// if(isset($_GET['search'])){ +// $search = $_GET['search']; +// echo $search; +// $content = curl_init(); +// curl_setopt($content, CURLOPT_URL, "http://nutricraft-express:8080/content/title/$search"); +// curl_setopt($content, CURLOPT_RETURNTRANSFER, 1); + +// $response = curl_exec($content); + +// // if (curl_errno($content)) { +// // echo 'Curl error: ' . curl_error($content); +// // } + +// curl_close($content); + +// echo $response; + +// // $data = json_decode($response, true); + +// // $idPhotoArray = array_map(function($item) { +// // return $item['id_photo']; +// // }, $data['data']); + +// // $path_photo = array(); + +// // for($i = 0; $i < count($idPhotoArray); $i++) { +// // $idPhoto = $idPhotoArray[$i]; +// // $photo = curl_init(); +// // curl_setopt($photo, CURLOPT_URL, "http://nutricraft-express:8080/image/$idPhoto"); +// // curl_setopt($photo, CURLOPT_RETURNTRANSFER, 1); + +// // $tempResponse = curl_exec($photo); + +// // if (curl_errno($photo)) { +// // echo 'Curl error: ' . curl_error($photo); +// // } + +// // curl_close($photo); + +// // $tempResponse = json_decode($tempResponse, true); +// // $url = $tempResponse['data']['url']; + +// // $path_photo[$i] = $url; +// // } + +// // for($i = 0; $i < count($data['data']); $i++) { +// // $data['data'][$i]['path_photo'] = $path_photo[$i]; +// // } + +// // if ($data) { +// // echo json_encode($data); +// // } else { +// // echo 'API request failed'; +// // } +// } +// else{ +// $serviceUrl = $_ENV['SOAP_URL_SUBSCRIBE']."?APIkey=".$_ENV["SOAP_KEY"]; + +// $soapRequest = ' +// <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> +// <Body> +// <getCreators xmlns="http://Services.nutricraft.org/"> +// <arg0 xmlns="">1</arg0> +// </getCreators> +// </Body> +// </Envelope>'; + +// $options = [ +// CURLOPT_URL => $serviceUrl, +// CURLOPT_RETURNTRANSFER => true, +// CURLOPT_POST => true, +// CURLOPT_POSTFIELDS => $soapRequest, +// CURLOPT_HTTPHEADER => [ +// 'Content-Type: text/xml; charset=utf-8', +// 'Content-Length: ' . strlen($soapRequest), +// ], +// ]; + +// $curl = curl_init(); +// curl_setopt_array($curl, $options); + +// $response = curl_exec($curl); + +// curl_close($curl); + +// if (curl_errno($curl)) { +// echo 'cURL Error: ' . curl_error($curl); +// } else { +// $xml = new SimpleXMLElement($response); +// $returnValue = (string)$xml->xpath('//return')[0]; +// echo($returnValue); +// } +// } -- GitLab