Data Collection Driver File
The custom plugin should declare at least one driver for the guests data collection of the back-end. Such drivers are meant to define the list of fields (information) to be collected from each guest. A list of fields is composed of key-attribute pairs to define every single field to be collected from the various guests.
A driver file is a custom PHP Class that extends the default framework of VikBooking and that implements the various methods to define the labels (keys) and attributes of each field. Here's an example of a driver file that the main plugin should load (file "mycountry.php").
<?php
/**
* @package VikBooking
* @subpackage checkin_paxfields_mycountry
*/
// No direct access
defined('ABSPATH') or die('No script kiddies please!');
// require the class file for declaring a custom type of field called "myinfo"
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'myinfo.php';
/**
* Helper class to support custom pax fields data collection types for "My Country" (filename my_country.php).
*/
final class VBOCheckinPaxfieldsMyCountry extends VBOCheckinAdapter
{
/**
* The ID of this pax data collector class.
*
* @var string
*/
protected $collector_id = 'mycountry';
/**
* Returns the name of the current pax data driver.
*
* @return string the name of this driver.
*/
public function getName()
{
return '"My Country"';
}
/**
* Tells whether children should be registered.
*
* @param bool $precheckin true if requested for front-end pre check-in.
*
* @return bool true to also register the children.
*/
public function registerChildren($precheckin = false)
{
/**
* Return true if your driver requires children to be registered.
* The return value can also change depending on the registration type:
* during back-end check-in registration or during front-end pre check-in.
*/
return false;
}
/**
* Returns the list of field labels. The count and keys
* of the labels should match with the attributes.
*
* @return array associative list of field labels.
*/
public function getLabels()
{
return [
'first_name' => __('First Name', 'vbocustomcheckindriver'),
'middle_name' => __('Middle Name', 'vbocustomcheckindriver'),
'last_name' => __('Last Name', 'vbocustomcheckindriver'),
'date_birth' => __('Date of birth', 'vbocustomcheckindriver'),
'country' => __('Country', 'vbocustomcheckindriver'),
'myinfo' => __('Custom Information', 'vbocustomcheckindriver'),
'notes' => __('Comments', 'vbocustomcheckindriver'),
];
}
/**
* Returns the list of field attributes. The count and keys
* of the attributes should match with the labels.
*
* @return array associative list of field attributes.
*
* @see the attribute of type "mycountry_myinfo" is a custom field
* that must be defined through a specific class file.
*/
public function getAttributes()
{
return [
'first_name' => 'text',
'middle_name' => 'text',
'last_name' => 'text',
'date_birth' => 'calendar',
'country' => 'country',
'myinfo' => 'mycountry_myinfo',
'notes' => 'textarea',
];
}
}
In the above example, our driver "MyCountry" is declaring a field of type "mycountry_myinfo", which is not one of the default and existing types of field to collect specific details from each guest. For this reason, this particular type of field must be defined through a specific class file (which has been previously loaded/required by the main driver file).
This is how a custom type of field for the guests data collection should be defined.
<?php
/**
* @package VikBooking
* @subpackage checkin_paxfield_type_mycountry_myinfo
*/
// No direct access
defined('ABSPATH') or die('No script kiddies please!');
/**
* Defines the handler for a pax field of type "mycountry_myinfo" (filename myinfo.php).
*/
final class VBOCheckinPaxfieldTypeMycountryMyinfo extends VBOCheckinPaxfieldType
{
/**
* Renders the current pax field HTML.
*
* @return string the HTML string to render the field.
*/
public function render()
{
// get the field unique ID
$field_id = $this->getFieldIdAttr();
// get the guest number (fields could be displayed only to a specific guest number)
$guest_number = $this->field->getGuestNumber();
// get the field class attribute
$pax_field_class = $this->getFieldClassAttr();
// get field name attribute
$name = $this->getFieldNameAttr();
// get the field value attribute
$value = $this->getFieldValueAttr();
// build list of our "myinfo" custom values
$customtypes_opt = '';
foreach ($this->loadMyCustomValues() as $custom_code => $custom_type) {
$sel_status = $custom_code == $value ? ' selected="selected"' : '';
$customtypes_opt .= '<option value="' . $custom_code . '"' . $sel_status . '>' . $custom_type . '</option>' . "\n";
}
// compose HTML content for the field
$field_html = <<<HTML
<select id="$field_id" data-gind="$guest_number" class="$pax_field_class" name="$name">
<option></option>
$customtypes_opt
</select>
HTML;
// return the necessary HTML string to display the field
return $field_html;
}
/**
* Helper method that returns a list of custom values.
*
* @return array
*/
private function loadMyCustomValues()
{
return [
'VGT' => __('I am vegetarian', 'vbocustomcheckindriver'),
'VGN' => __('I am vegan', 'vbocustomcheckindriver'),
'ALL' => __('I have no food preference', 'vbocustomcheckindriver'),
];
}
}
This is how guests will be registered (checked-in) in the back-end section of Vik Booking. Thanks to our custom driver we are collecting the desired information from each guest, including our custom field "myinfo" (called "Custom Information"):