Booked Scheduler Community Support
July 25, 2017, 04:43:22 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Booked is proud to recommend Shift Capsule in the employee shift scheduling space www.ShiftCapsule.com
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Let users add the number of "non registered users" to reservation - API example  (Read 572 times)
blacktea
Jr. Member
**

Karma: 0
Posts: 95

http://www.bitsfromitaly.it


WWW
« on: April 21, 2017, 06:41:40 PM »

Hi, using booked (ver. 2.6.5) for a company that organizes shuttle trips has arisen the need to add the following function:

A registered Booked user can join a reservation and can book for a number of un-registered friends for the same reservation.

For example: I am a registered user on Booked. I want to join a reservation and a friend of mine wants to come with me only once and he is not interested in registering in Booked. I join a reservation and at that time Booked asks me if i wanna "add" some friend. I choose "+1 friend" from a drop down menu and click on a JOIN button. On Booked this information will be added on a custom attribute like (name surname +1). At the moment this +1 is not added to the number of participants (working on it) so pay attention if you are using the function "max participants".

What i did is to add an HTML form in /booked/tpl/reservation/view.tpl (after line 163) ... this will appear only after user join reservation.
The code is:

Code:
<br>
<br>
<div class="w3-row">
<div class="w3-half w3-container">
<i class="fa fa-user-plus"></i> {translate key=InviteFriends}  {* <-- remember to add the string to any language file *}
</div>
<div class="w3-half w3-container">
<form method="post" action="/booked/addGuests.php">
<input type="hidden" name="usernameLogged" value="{$UserName}">
<input type="hidden" name="referenceNumber" value="{$ReferenceNumber}">
<select name="bikers">
<option value="1" selected>+1 biker</option>
<option value="2">+2 bikers</option>
<option value="3">+3 bikers</option>
<option value="4">+4 bikers</option>
<option value="5">+5 bikers</option>
<option value="6">+6 bikers</option>
<option value="7">+7 bikers</option>
<option value="8">+8 bikers</option>
<option value="9">+9 bikers</option>
<option value="10">+10 bikers</option>
<option value="11">+11 bikers</option>
<option value="12">+12 bikers</option>
<option value="13">+13 bikers</option>
<option value="14">+14 bikers</option>
<option value="15">+15 bikers</option>
<option value="16">+16 bikers</option>
<option value="17">+17 bikers</option>
<option value="18">+18 bikers</option>
</select>
<input type="submit" value="{translate key=Join}">
</form>
</div>
</div>

Then i have uploaded the file addGuests.php that contains the API calls

Code:
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
    <style type="text/css">
        body {
            font-family: sans-serif;
            font-size: 12pt;
margin: 20px;
        }

        #loading {
            display: none;
            color: #636363;
            font-size: 16pt;
            background-color: #fafafa;
            -moz-border-radius: 2px;
            -webkit-border-radius: 2px;
            -khtml-border-radius: 2px;
            border-radius: 2px;

            -webkit-box-shadow: 0px 0px 2px 0px #eee;
            -moz-box-shadow: 0px 0px 2px 0px #eee;
            box-shadow: 0px 0px 2px 0px #eee;
            margin-bottom: 15px;
        }

        .reservation-item {
            border: solid 1px #eeeeee;
            background-color: #fafafa;
            padding: 5px;

            -moz-border-radius: 2px;
            -webkit-border-radius: 2px;
            -khtml-border-radius: 2px;
            border-radius: 2px;

            -webkit-box-shadow: 0px 0px 2px 0px #eee;
            -moz-box-shadow: 0px 0px 2px 0px #eee;
            box-shadow: 0px 0px 2px 0px #eee;
            margin-bottom: 5px;
        }

        .reservation-item p {
            margin: 2px;
        }

        .reservation-item p span {
            vertical-align: top;
        }

        .error {
            background-color: #F78B83;
            padding: 3px;
            font-size: 9pt;
        }

    </style>
</head>
<body>
Prova
<?php

$userLogin 
'admin'// user who can update reservation, can be an admin
$pass 'type here your password'// user's password
$server_url "http://www.example.com/booked"// url of your server - for example http://www.example.com/booked
$userLogged = [$userId];

// create login/auth info, need username and password in an array to do json_encode
$body = array('username' => $userLogin'password' => $pass);

// make sure it is utf-8 so json is happy (you may not need this step, but a good idea...)
$body array_map(utf8_encode$body );

// create the post data for the auth
$body =  json_encode($body );

// set the url for accessing the call you want
$url $server_url "/Web/Services/Authentication/Authenticate";

// make the call to the API
$ch curl_init();
curl_setopt($chCURLOPT_URL$url );
curl_setopt($chCURLOPT_HEADERfalse);
curl_setopt($chCURLOPT_POSTtrue);
curl_setopt($chCURLOPT_POSTFIELDS$body );
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// grab URL and pass it to the browser, retaining the reply
$reply curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

// see what we got
echo $reply "<br>";

//decode id and token - setup array
$auth_data json_decode($replytrue);

$auth_header = array(
    
'X-Booked-SessionToken: ' .$auth_data["sessionToken"],
    
'X-Booked-UserId: ' .$auth_data["userId"]    
);

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

$usernameLogged htmlspecialchars($_POST['usernameLogged']);

echo 
$usernameLogged;

// set the link to get Reservation data
$url1a $server_url "/Web/Services/Users/?username=".$usernameLogged;

// make the call to the API
$ch1a curl_init();
curl_setopt($ch1aCURLOPT_URL$url1a );
curl_setopt($ch1aCURLOPT_HEADERfalse);
curl_setopt($ch1aCURLOPT_HTTPHEADER$auth_header);
curl_setopt($ch1aCURLOPT_POSTfalse);
curl_setopt($ch1aCURLOPT_RETURNTRANSFERtrue);

// grab URL and pass it to the browser, retaining the reply
$reply1acurl_exec($ch1a);

// close cURL resource, and free up system resources
curl_close($ch1a);

$auth_data1a json_decode($reply1atrue);
$auth_custom2 $auth_data1a['users'];
$innerArray2 $auth_custom2[0];
$firstNameUser $innerArray2['firstName'];
$lastNameUser $innerArray2['lastName'];

echo 
"Name: ". $firstNameUser  "<br>";
echo 
"Surname: ". $lastNameUser  "<br>";

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// get variables coming from FORM
$referenceNumber htmlspecialchars($_POST['referenceNumber']);
$referenceNumber trim($referenceNumber"'");
$bikers htmlspecialchars($_POST['bikers']);


// set the link to get Reservation data
$url2 $server_url "/Web/Services/Reservations/".$referenceNumber;

// make the call to the API
$ch2 curl_init();
curl_setopt($ch2CURLOPT_URL$url2 );
curl_setopt($ch2CURLOPT_HEADERfalse);
curl_setopt($ch2CURLOPT_HTTPHEADER$auth_header);
curl_setopt($ch2CURLOPT_POSTfalse);
curl_setopt($ch2CURLOPT_RETURNTRANSFERtrue);

// grab URL and pass it to the browser, retaining the reply
$reply2 curl_exec($ch2);

// close cURL resource, and free up system resources
curl_close($ch2);

// set the variables taken from Reservation

$auth_data2 json_decode($reply2true);
$startDateTime $auth_data2['startDate'];
$endDateTime $auth_data2['endDate'];
$resourceId $auth_data2['resourceId'];
$participants $auth_data2['participants'];
$invitees $auth_data2['invitees'];
$allowParticipation $auth_data2['allowParticipation'];
if (
$allowParticipation){
    
$allowParticipation2 "true";
}
if (!
$allowParticipation){
    
$allowParticipation2 "false";
}

// manage first nested array to extract userId of Reservation's owner

$auth_user $auth_data2['owner'];
$userId $auth_user['userId'];

// manage second nested array to extract participants and set the list of them

$participants $auth_data2['participants'];
foreach(
$participants as $row) {
//echo $row['userId'].",";
$participants2 .= ",".$row['userId'];
}
$participants2 ltrim($participants2',');

// manage third nested array to extract invitees and set the list of them

$auth_invitees $auth_data2['invitees'];
foreach(
$invitees as $row) {
//echo $row['userId'].",";
$invitees2 .= ",".$row['userId'];
}
$invitees2 ltrim($invitees2',');

// manage forth nested array to extract custom attributes and set the list of them

$auth_custom $auth_data2['customAttributes'];
$innerArray $auth_custom[0];
$id $innerArray["id"];
$value $innerArray["value"];
$value .= "(".$firstNameUser." ".$lastNameUser."+".$bikers.")";



// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// set the url to update reservation-item
$url3 $server_url "/Web/Services/Reservations/".$referenceNumber;

// compile the API data to send to update reservation
$body3 '{"startDateTime":"'.$startDateTime.'","endDateTime":"'.$endDateTime.'","customAttributes":[{"attributeId":"'.$id.'","attributeValue":"'.$value.'"}],"description":"","invitees":['.$invitees2.'],"participants":['.$participants2.'],"resourceId":"'.$resourceId.'","title":"","userId":"'.$userId.'","allowParticipation":'.$allowParticipation2.'}';

// make the call to the API
$ch3 curl_init();
curl_setopt($ch3CURLOPT_URL$url3 );
curl_setopt($ch3CURLOPT_HEADERfalse);
curl_setopt($ch3CURLOPT_HTTPHEADER$auth_header);
curl_setopt($ch3CURLOPT_POSTtrue);
curl_setopt($ch3CURLOPT_POSTFIELDS$body3);
curl_setopt($ch3CURLOPT_RETURNTRANSFERtrue);

// grab URL and pass it to the browser, retaining the reply
$reply3 curl_exec($ch3);

// close cURL resource, and free up system resources
curl_close($ch3);

// when all it's done i redirect to home page

header("Location:http://www.example.com/booked/Web/dashboard.php");
exit;
?>


</body></html>

I hope this may help someone ... just customize at your needs.
Thanks forum's users for inspiration, especially Nick and Techcoder (i have found your previous posts).
« Last Edit: April 26, 2017, 06:16:27 AM by blacktea » Logged
energysteven
Newbie
*

Karma: 0
Posts: 2


« Reply #1 on: June 12, 2017, 03:34:28 PM »

Thank you for posting your code.
I had been struggling with using the API but now I can Authenticate okay.
It's been more than a few years since I have done any PHP or web cgi code.
Logged
blacktea
Jr. Member
**

Karma: 0
Posts: 95

http://www.bitsfromitaly.it


WWW
« Reply #2 on: June 17, 2017, 07:11:19 PM »

You are welcome energysteven, i'm happy to help  Smiley
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.20 | SMF © 2006-2007, Simple Machines Valid XHTML 1.0! Valid CSS!