| 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 |
* Browser notification displayer handler for reminders. |
| 16 |
* |
| 17 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBONotificationDisplayReminder extends JObject implements VBONotificationDisplayer |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Proxy for immediately getting the object and bind data. |
| 23 |
* |
| 24 |
* @param array|object $data the notification payload data to bind. |
| 25 |
*/ |
| 26 |
public static function getInstance($data = null) |
| 27 |
{ |
| 28 |
return new static($data); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Composes an object with the necessary properties to display |
| 33 |
* the notification in the browser. In case of errors while |
| 34 |
* retrieving the reminder, an Exception will be thrown. |
| 35 |
* |
| 36 |
* @return null|object the notification display data payload. |
| 37 |
* |
| 38 |
* @throws Exception |
| 39 |
*/ |
| 40 |
public function getData() |
| 41 |
{ |
| 42 |
$reminder_id = $this->get('id'); |
| 43 |
if (empty($reminder_id)) { |
| 44 |
throw new Exception('Empty notification reminder id to display', 500); |
| 45 |
} |
| 46 |
|
| 47 |
$helper = VBORemindersHelper::getInstance(); |
| 48 |
|
| 49 |
$reminder = $helper->getReminder($reminder_id); |
| 50 |
if (!$reminder) { |
| 51 |
throw new Exception('Notification reminder id not found', 404); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Flag the reminder as displayed. |
| 56 |
* |
| 57 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 58 |
*/ |
| 59 |
$helper->setDisplayed($reminder->id); |
| 60 |
|
| 61 |
// compose the notification data to display |
| 62 |
$notif_data = new stdClass; |
| 63 |
$notif_data->title = $reminder->title; |
| 64 |
$notif_data->message = empty($reminder->descr) ? $reminder->title : $reminder->descr; |
| 65 |
$notif_data->icon = $this->getIconUrl(); |
| 66 |
|
| 67 |
// check if click handler should be attached |
| 68 |
if (!empty($reminder->idorder)) { |
| 69 |
// register click event callback data |
| 70 |
$notif_data->onclick = 'VBOCore.handleGoto'; |
| 71 |
$notif_data->gotourl = VBOFactory::getPlatform()->getUri()->admin("index.php?option=com_vikbooking&task=editorder&cid[]={$reminder->idorder}", false); |
| 72 |
if (is_object($reminder->payload) && !empty($reminder->payload->airbnb_host_guest_review)) { |
| 73 |
// append callback to trigger the host-to-guest review |
| 74 |
$notif_data->gotourl .= '¬if_action=airbnb_host_guest_review'; |
| 75 |
// attempt to get the proper channel logo |
| 76 |
$vcm_logos = VikBooking::getVcmChannelsLogo('airbnb', $get_istance = true); |
| 77 |
if ($vcm_logos) { |
| 78 |
$channel_logo = $vcm_logos->getSmallLogoURL(); |
| 79 |
if (!empty($channel_logo)) { |
| 80 |
$notif_data->icon = $channel_logo; |
| 81 |
} |
| 82 |
} |
| 83 |
} else { |
| 84 |
// if no actions, overwrite click handler to display the notification within the booking details modal widget |
| 85 |
$notif_data->onclick = 'VBOCore.handleDisplayWidgetNotification'; |
| 86 |
// set additional properties to the (Web, not Push) notification payload |
| 87 |
$notif_data->widget_id = 'booking_details'; |
| 88 |
// data options for a Web notification should NOT set a "type" or this will be overridden |
| 89 |
$notif_data->_options = [ |
| 90 |
'_web' => 1, |
| 91 |
'title' => $notif_data->title, |
| 92 |
'message' => $notif_data->message, |
| 93 |
'bid' => $reminder->idorder, |
| 94 |
]; |
| 95 |
} |
| 96 |
} else { |
| 97 |
$notif_data->onclick = null; |
| 98 |
} |
| 99 |
|
| 100 |
return $notif_data; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns the URL to the default icon for the reminders |
| 105 |
* browser notifications. Custom logos are preferred. |
| 106 |
* |
| 107 |
* @return string|null |
| 108 |
*/ |
| 109 |
private function getIconUrl() |
| 110 |
{ |
| 111 |
$config = VBOFactory::getConfig(); |
| 112 |
|
| 113 |
// back-end custom logo |
| 114 |
$use_logo = $config->get('backlogo'); |
| 115 |
if (empty($use_logo) || !strcasecmp($use_logo, 'vikbooking.png')) { |
| 116 |
// fallback to company (site) logo |
| 117 |
$use_logo = $config->get('sitelogo'); |
| 118 |
} |
| 119 |
|
| 120 |
if (!empty($use_logo) && strcasecmp($use_logo, 'vikbooking.png')) { |
| 121 |
// uploaded logo found |
| 122 |
$use_logo = VBO_ADMIN_URI . 'resources/' . $use_logo; |
| 123 |
} else { |
| 124 |
$use_logo = null; |
| 125 |
} |
| 126 |
|
| 127 |
return $use_logo; |
| 128 |
} |
| 129 |
} |
| 130 |
|