| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author Alessio Gaggii - E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2022 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Defines an abstract adapter to extend the pax data collection. |
| 16 |
* |
| 17 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 18 |
*/ |
| 19 |
abstract class VBOCheckinAdapter implements VBOCheckinPaxfields |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The ID of the pax data collector class. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $collector_id = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* Tells whether children should be registered. |
| 30 |
* Children registration is disabled by default. |
| 31 |
* |
| 32 |
* @param bool $precheckin true if requested for front-end pre check-in. |
| 33 |
* |
| 34 |
* @return bool true to also register the children. |
| 35 |
* |
| 36 |
* @since 1.16.3 (J) - 1.6.3 (WP) added $precheckin argument. |
| 37 |
*/ |
| 38 |
public function registerChildren($precheckin = false) |
| 39 |
{ |
| 40 |
// disabled by default, unless method gets overridden |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the instance of the given pax field key. |
| 46 |
* |
| 47 |
* @param string $key the field key identifier. |
| 48 |
* |
| 49 |
* @return VBOCheckinPaxfield the requested pax field object. |
| 50 |
*/ |
| 51 |
public function getField($key) |
| 52 |
{ |
| 53 |
// get all the existing field attributes |
| 54 |
$attributes = $this->getAttributes(); |
| 55 |
|
| 56 |
// create a new instance of the field registry object |
| 57 |
$pax_field = new VBOCheckinPaxfield(); |
| 58 |
|
| 59 |
// inject key and type of field |
| 60 |
$field_type = (isset($attributes[$key]) ? $attributes[$key] : 'text'); |
| 61 |
$pax_field->setKey($key); |
| 62 |
$pax_field->setType($field_type); |
| 63 |
|
| 64 |
// return the field registry object |
| 65 |
return $pax_field; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Renders a specific pax field type. |
| 70 |
* |
| 71 |
* @param VBOCheckinPaxfield $field the pax field object to render. |
| 72 |
* |
| 73 |
* @return string the HTML string to display the field. |
| 74 |
*/ |
| 75 |
public function render(VBOCheckinPaxfield $field) |
| 76 |
{ |
| 77 |
// get the field implementor |
| 78 |
$implementor = $this->getFieldTypeImplementor($field); |
| 79 |
|
| 80 |
if ($implementor === null) { |
| 81 |
// could not access the implementor |
| 82 |
return ''; |
| 83 |
} |
| 84 |
|
| 85 |
// let the handler render the field |
| 86 |
return $implementor->render(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Attempts to return an instance of the field-type implementor object being parsed. |
| 91 |
* |
| 92 |
* @param VBOCheckinPaxfield $field the pax field object to render. |
| 93 |
* |
| 94 |
* @return null|object the field implementor class of VBOCheckinPaxfieldType or null. |
| 95 |
*/ |
| 96 |
public function getFieldTypeImplementor(VBOCheckinPaxfield $field) |
| 97 |
{ |
| 98 |
// get the type of field to render |
| 99 |
$field_type = $field->getType(); |
| 100 |
|
| 101 |
if ($field_type === null) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
if (is_array($field_type)) { |
| 106 |
// convert field type to "select" string |
| 107 |
$field_type = 'select'; |
| 108 |
} |
| 109 |
|
| 110 |
if (!is_string($field_type)) { |
| 111 |
// invalid field type |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
// compose dinamically the implementor class name |
| 116 |
$field_class = $this->getFieldTypeClass($field_type); |
| 117 |
|
| 118 |
if (!class_exists($field_class)) { |
| 119 |
// no implementor handler found for this type of field |
| 120 |
return null; |
| 121 |
} |
| 122 |
|
| 123 |
// return the field handler by passing the pax field object and the data collector id |
| 124 |
return new $field_class($field, $this->collector_id); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Builds the list of back-end pax fields for the extended collection type. |
| 129 |
* |
| 130 |
* @return array the list of pax fields to collect in the back-end. |
| 131 |
*/ |
| 132 |
public function listFields() |
| 133 |
{ |
| 134 |
return [$this->getLabels(), $this->getAttributes()]; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Composes the field type class name given its type-string. |
| 139 |
* |
| 140 |
* @param string $field_type the field type-string identifier. |
| 141 |
* |
| 142 |
* @return bool|string the class name to use for the field, or false. |
| 143 |
*/ |
| 144 |
protected function getFieldTypeClass($field_type) |
| 145 |
{ |
| 146 |
if (!is_string($field_type) || empty($field_type)) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
// base class name |
| 151 |
$base_paxf_class = 'VBOCheckinPaxfieldType'; |
| 152 |
|
| 153 |
// compose field type class name |
| 154 |
$field_type = ucwords(str_replace(array('_', '-'), ' ', $field_type)); |
| 155 |
$field_type = preg_replace("/[^a-zA-Z0-9]/", '', $field_type); |
| 156 |
|
| 157 |
return $base_paxf_class . $field_type; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns the name of the current pax data driver. |
| 162 |
* |
| 163 |
* @return string the name of the driver. |
| 164 |
*/ |
| 165 |
abstract public function getName(); |
| 166 |
|
| 167 |
/** |
| 168 |
* Builds the list pax fields labels. |
| 169 |
* |
| 170 |
* @return array the list of pax fields labels. |
| 171 |
*/ |
| 172 |
abstract public function getLabels(); |
| 173 |
|
| 174 |
/** |
| 175 |
* Builds the list pax fields attributes. |
| 176 |
* |
| 177 |
* @return array the list of pax fields attributes. |
| 178 |
*/ |
| 179 |
abstract public function getAttributes(); |
| 180 |
} |
| 181 |
|