Skip to content

Commit

Permalink
revamped php sample with embedconfig file
Browse files Browse the repository at this point in the history
  • Loading branch information
anuabarnab4 committed Jan 7, 2025
1 parent c8ef513 commit 55f6fe4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
57 changes: 38 additions & 19 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@

<?php
// ServerURL, DashboardPath and AuthorizeSeverURL to embed widget
$serverUrl = "http://localhost:53623/bi/site/site1";
$dashboardId ="42d69d1a-dcd8-41b0-93a4-d4cdd2c53241";
$authorizeServerUrl = "http://localhost:3000/rest/authorizeserver.php";
$apiHost = "http://localhost:3000";
$authorizeServerUrl = $apiHost . "/rest/authorizeserver.php";
$getDataUrl = $apiHost . "/rest/getData.php";
?>

<html>

<head>
<script type="text/javascript" src="https://cdn.boldbi.com/embedded-sdk/latest/boldbi-embed.js"></script>
<script type="text/javascript" src="https://cdn.boldbi.com/embedded-sdk/latest/boldbi-embed.js"></script>
</head>
<body onload="embedSample();">
<div id="dashboard"></div>

<body onload="Init();">
<div id="dashboard"></div>
<script>
function embedSample() {
var dashboardemb = BoldBI.create({
serverUrl: '<?php echo $serverUrl;?>',
dashboardId: '<?php echo $dashboardId;?>',
embedContainerId: "dashboard",// This should be the container id where you want to embed the dashboard
embedType: BoldBI.EmbedType.Component,
environment: BoldBI.Environment.Enterprise,
height: "700px",
width: "1500px",
async function Init() {
try {
// Fetch data from the PHP backend
const response = await fetch('<?php echo $getDataUrl; ?>');
console.log("Response ", response);
// Check if the response is okay
if (!response.ok) {
throw new Error("Network response was not ok");
}

// Parse the JSON data
const data = await response.json();
// Call the function to render the dashboard with the fetched data
renderDashboard(data);
} catch (error) {
console.error("Error fetching the embed configuration:", error);
}
}

function renderDashboard(data) {
this.dashboard = BoldBI.create({
serverUrl: data.ServerUrl + "/" + data.SiteIdentifier,
dashboardId: data.DashboardId,
embedContainerId: "dashboard",
width: "100%",
height: window.innerHeight + 'px',
authorizationServer: {
url: '<?php echo $authorizeServerUrl;?>'
url: '<?php echo $authorizeServerUrl; ?>'
}
});
dashboardemb.loadDashboard();

this.dashboard.loadDashboard();
}
</script>
</body>

</html>
25 changes: 18 additions & 7 deletions rest/authorizeserver.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<?php
//// Embed Properties ////
$secretCode = "Enter your embed secret"; // Use your SecretCode here
$userEmail = "demo@gmail.com"; // Email address of the user
$jsonData = file_get_contents('embedConfig.json');

if ($jsonData === false) {
echo 'Error: embedConfig.json file not found.';
exit(1); // Exit the program with a non-zero exit code to indicate an error
}

$appConfig = json_decode($jsonData, true);

$secretCode = $appConfig['EmbedSecret'];
$userEmail = $appConfig['UserEmail'];

$serverTimeStamp=time();
$data = json_decode(file_get_contents('php://input'), true);

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
Expand All @@ -16,14 +26,15 @@

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

// Getting embedQuerString and dashboardServerApiUrl from BoldBI wrapper
if ($data != null && $data["embedQuerString"] !="" && $data["dashboardServerApiUrl"]!="") {
$embedQuerString = $data["embedQuerString"];
$dashboardServerApiUrl= $data["dashboardServerApiUrl"];
$dashdetails = GetEmbedDetails($embedQuerString, $dashboardServerApiUrl);
header('Content-type: application/json');
echo json_encode($dashdetails);
}
}

// This function used to get dashboard details from Bold BI Server
function GetEmbedDetails($embedQuerString, $dashboardServerApiUrl){
Expand All @@ -33,6 +44,7 @@ function GetEmbedDetails($embedQuerString, $dashboardServerApiUrl){
$embedQuerString = $embedQuerString . "&embed_server_timestamp=" . $serverTimeStamp;
$embedSignature = "&embed_signature=" . getSignatureUrl($embedQuerString);
$embedDetailsUrl = "/embed/authorize?" . $embedQuerString . $embedSignature;

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $dashboardServerApiUrl . $embedDetailsUrl,
Expand All @@ -46,15 +58,14 @@ function GetEmbedDetails($embedQuerString, $dashboardServerApiUrl){
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

return $response;
}

//// Prepare embed_Signature by encrypting with secretCode ////
// Prepare embed_Signature by encrypting with secretCode
function getSignatureUrl($embedQuerString) {
global $secretCode;
$keyBytes = mb_convert_encoding($secretCode, 'UTF-8');
Expand Down
38 changes: 38 additions & 0 deletions rest/getData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

// Rest of your PHP script
header("Content-Type: application/json");

// Read the JSON file
$data = file_get_contents('embedConfig.json');

if ($data === false) {
http_response_code(500); // Internal Server Error
echo "embedConfig.json file not found!";
exit();
}

// Parse the JSON data
$dataArray = json_decode($data, true);

if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(500); // Internal Server Error
echo json_encode(array("error" => "Could not parse the JSON data."));
exit();
}

// Extract specific values
$clientEmbedConfigData = array(
"DashboardId" => $dataArray["DashboardId"],
"ServerUrl" => $dataArray["ServerUrl"],
"SiteIdentifier" => $dataArray["SiteIdentifier"],
"EmbedType" => $dataArray["EmbedType"],
"Environment" => $dataArray["Environment"],
);

// Return the specific values of parsed data as JSON response
echo json_encode($clientEmbedConfigData, JSON_PRETTY_PRINT);
?>

0 comments on commit 55f6fe4

Please sign in to comment.