| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 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 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* Class handler for conditional rule "QRCode". |
| 15 |
* This Conditional Text Rule will include a QR Code image with the booking link. |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
class VikBookingConditionalRuleQrcode extends VikBookingConditionalRule |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $qrcode_base_path; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $qrcode_base_uri; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor will define the rule name, description and identifier. |
| 33 |
*/ |
| 34 |
public function __construct() |
| 35 |
{ |
| 36 |
// call parent constructor |
| 37 |
parent::__construct(); |
| 38 |
|
| 39 |
$this->ruleName = 'QR Code'; |
| 40 |
$this->ruleDescr = 'Generate a QR Code with the booking link.'; |
| 41 |
$this->ruleId = basename(__FILE__); |
| 42 |
|
| 43 |
$this->qrcode_base_path = VBO_MEDIA_PATH; |
| 44 |
$this->qrcode_base_uri = VBO_MEDIA_URI; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Displays the rule parameters. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function renderParams() |
| 53 |
{ |
| 54 |
// build the list of rule params to collect during the configuration |
| 55 |
$params = [ |
| 56 |
'qr_code_width' => [ |
| 57 |
'type' => 'number', |
| 58 |
'label' => 'QR Code width (px)', |
| 59 |
'help' => 'You can optionally use the tag <strong onclick="vboQrcodeCtrAddContentEditor(\'{qrcode_img}\');" style="cursor: pointer;">{qrcode_img}</strong> if you would like to place the <i>QR code image</i> on a specific section of the message. Suggested width: 128px.', |
| 60 |
'min' => 1, |
| 61 |
'max' => 999, |
| 62 |
], |
| 63 |
]; |
| 64 |
|
| 65 |
// build the list of current rule settings that were saved |
| 66 |
$settings = (array) $this->getParams(); |
| 67 |
|
| 68 |
// render all params/settings for this custom rule |
| 69 |
echo VBOParamsRendering::getInstance( |
| 70 |
$params, |
| 71 |
$settings |
| 72 |
)->setInputName(basename($this->ruleId, '.php'))->getHtml(); |
| 73 |
|
| 74 |
?> |
| 75 |
|
| 76 |
<script type="text/javascript"> |
| 77 |
function vboQrcodeCtrAddContentEditor(str) { |
| 78 |
if (!str) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
try { |
| 83 |
// "msg" is the name of the WYSIWYG editor of the conditional text |
| 84 |
Joomla.editors.instances.msg.replaceSelection(str); |
| 85 |
} catch(e) { |
| 86 |
// do nothing |
| 87 |
} |
| 88 |
} |
| 89 |
</script> |
| 90 |
<?php |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Tells whether the rule is compliant. |
| 95 |
* |
| 96 |
* @return bool True on success, false otherwise. |
| 97 |
*/ |
| 98 |
public function isCompliant() |
| 99 |
{ |
| 100 |
$booking_id = (int) $this->getPropVal('booking', 'id', 0); |
| 101 |
$qrcode_width = (int) $this->getParam('qr_code_width', 0); |
| 102 |
|
| 103 |
return !empty($booking_id) && $qrcode_width > 0; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Allows to manipulate the message of the conditional text with dynamic contents. |
| 108 |
* |
| 109 |
* @override |
| 110 |
* |
| 111 |
* @param string $msg The configured conditional text message. |
| 112 |
* |
| 113 |
* @return string The manipulated conditional text message. |
| 114 |
*/ |
| 115 |
public function manipulateMessage($msg) |
| 116 |
{ |
| 117 |
$qrcode_width = (int) $this->getParam('qr_code_width', 128); |
| 118 |
|
| 119 |
$qrcode_uri = $this->getQRCodeUri(); |
| 120 |
|
| 121 |
if (!$qrcode_uri) { |
| 122 |
// an error occurred |
| 123 |
return $msg; |
| 124 |
} |
| 125 |
|
| 126 |
// build the HTML content for the QR Code PNG image |
| 127 |
$qrcode_html = '<img src="' . $qrcode_uri . '" width="' . $qrcode_width . '" alt="Reservation QR Code" />'; |
| 128 |
|
| 129 |
if (strpos($msg, '{qrcode_img}') !== false) { |
| 130 |
// exact placeholder tag found |
| 131 |
$msg = str_replace('{qrcode_img}', $qrcode_html, $msg); |
| 132 |
} else { |
| 133 |
// append image tag to message |
| 134 |
$msg .= $qrcode_html; |
| 135 |
} |
| 136 |
|
| 137 |
// return the manipulated message |
| 138 |
return $msg; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Obtains the QR Code image URI by checking if the file exists for the current |
| 143 |
* reservation, or by creating the PNG file at runtime and by saving it on disk. |
| 144 |
* |
| 145 |
* @return string QR Code URI or empty string in case of errors. |
| 146 |
*/ |
| 147 |
protected function getQRCodeUri() |
| 148 |
{ |
| 149 |
// gather booking details |
| 150 |
$booking_id = (int) $this->getPropVal('booking', 'id', 0); |
| 151 |
$booking_ts = (int) $this->getPropVal('booking', 'ts', 0); |
| 152 |
$booking_sid = (string) $this->getPropVal('booking', 'sid', 0); |
| 153 |
$booking_idota = (string) $this->getPropVal('booking', 'idorderota', ''); |
| 154 |
$booking_channel = (string) $this->getPropVal('booking', 'channel', ''); |
| 155 |
$booking_lang = (string) $this->getPropVal('booking', 'lang', ''); |
| 156 |
$use_sid = !empty($booking_idota) && !empty($booking_channel) ? $booking_idota : $booking_sid; |
| 157 |
|
| 158 |
if (empty($booking_id) || empty($booking_ts)) { |
| 159 |
return ''; |
| 160 |
} |
| 161 |
|
| 162 |
$qrcode_file_name = 'qr-booking-' . $use_sid . '-' . $booking_ts . '.png'; |
| 163 |
$final_qrcode_path = implode(DIRECTORY_SEPARATOR, [$this->qrcode_base_path, $qrcode_file_name]); |
| 164 |
|
| 165 |
if (is_file($final_qrcode_path)) { |
| 166 |
// QR code file exists, return the URI |
| 167 |
return $this->qrcode_base_uri . $qrcode_file_name; |
| 168 |
} |
| 169 |
|
| 170 |
// route booking details URL |
| 171 |
$lang_link = !empty($booking_lang) ? "&lang={$booking_lang}" : ''; |
| 172 |
$book_link = VikBooking::externalroute("index.php?option=com_vikbooking&view=booking&sid={$use_sid}&ts={$booking_ts}{$lang_link}", false); |
| 173 |
|
| 174 |
// access the model for shortening URLs |
| 175 |
$model = VBOModelShortenurl::getInstance($onlyRouted = true)->setBooking((array) $this->getProperty('booking', [])); |
| 176 |
|
| 177 |
// get the final booking link URL for the QR code content |
| 178 |
$book_link = $model->getShortUrl($book_link); |
| 179 |
|
| 180 |
try { |
| 181 |
// require the TCPDF 2D Barcode library |
| 182 |
require_once VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'tcpdf' . DIRECTORY_SEPARATOR . 'tcpdf_barcodes_2d.php'; |
| 183 |
|
| 184 |
// set the barcode content and type |
| 185 |
$barCode = new TCPDF2DBarcode($book_link, 'QRCODE,H'); |
| 186 |
|
| 187 |
// generate the QR code as PNG image |
| 188 |
$qr = $barCode->getBarcodePngData( |
| 189 |
/** |
| 190 |
* QR Code PNG default width (points indicating pixels per cell). |
| 191 |
* 1 ~= 49px/69px. |
| 192 |
*/ |
| 193 |
4, |
| 194 |
/** |
| 195 |
* QR Code PNG default height (points indicating pixels per cell). |
| 196 |
* 1 ~= 49px/69px. |
| 197 |
*/ |
| 198 |
4, |
| 199 |
/** |
| 200 |
* QR Code PNG default color (RGB) (black) |
| 201 |
*/ |
| 202 |
explode(',', preg_replace("/[^0-9\.\,]/", '', '0, 0, 0')) |
| 203 |
); |
| 204 |
|
| 205 |
// write the image on disk |
| 206 |
if (JFile::write($final_qrcode_path, $qr)) { |
| 207 |
// QR code file created, return the URI |
| 208 |
return $this->qrcode_base_uri . $qrcode_file_name; |
| 209 |
} |
| 210 |
} catch (Throwable $t) { |
| 211 |
// do nothing in case of errors |
| 212 |
} |
| 213 |
|
| 214 |
// an error occurred |
| 215 |
return ''; |
| 216 |
} |
| 217 |
} |
| 218 |
|