| @@ -1,0 +1,168 @@ | ||
| 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 browser notification data model. | |
| 16 | + * | |
| 17 | + * @since 1.15.0 (J) - 1.5.0 (WP) | |
| 18 | + */ | |
| 19 | +abstract class VBONotificationAdapter extends JObject implements VBONotificationData | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * The type of the scheduled notification. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + protected $_notification_type = ''; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * The default admin endpoint for displaying (building) a notification. Those notification | |
| 30 | + * types that must be built through AJAX will have to convert this relative URL to an AJAX URL. | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + protected $_notif_display_url = 'index.php?option=com_vikbooking&task=notification_displayer'; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Proxy to immediately access the object. | |
| 38 | + * | |
| 39 | + * @param object|array $data optional data to bind. | |
| 40 | + * | |
| 41 | + * @return self | |
| 42 | + */ | |
| 43 | + public static function getInstance($data = []) | |
| 44 | + { | |
| 45 | + return new static($data); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Gets the type of notification. | |
| 50 | + * | |
| 51 | + * @return string | |
| 52 | + */ | |
| 53 | + public function getType() | |
| 54 | + { | |
| 55 | + return $this->_notification_type; | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Gets the notification expiration date time. | |
| 60 | + * | |
| 61 | + * @return string "now" by default for no scheduling. | |
| 62 | + */ | |
| 63 | + public function getDateTime() | |
| 64 | + { | |
| 65 | + return (string)$this->get('_date_time', 'now'); | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Sets the notification expiration date time. | |
| 70 | + * | |
| 71 | + * @param string|DateTime $dtime | |
| 72 | + * | |
| 73 | + * @return self | |
| 74 | + */ | |
| 75 | + public function setDateTime($dtime) | |
| 76 | + { | |
| 77 | + if ($dtime instanceof DateTime) { | |
| 78 | + $dtime = $dtime->format('Y-m-d H:i:s'); | |
| 79 | + } | |
| 80 | + // this sets a "reserved" property starting with "_" | |
| 81 | + $this->set('_date_time', $dtime); | |
| 82 | + | |
| 83 | + return $this; | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Accesses the flag to declare a date as with no time. | |
| 88 | + * | |
| 89 | + * @return bool true if time is disabled. | |
| 90 | + */ | |
| 91 | + public function getNoTime() | |
| 92 | + { | |
| 93 | + return (bool)$this->get('_no_time', false); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Controls the flag to declare a date as with no time. | |
| 98 | + * | |
| 99 | + * @param bool $set true to disable time. | |
| 100 | + * | |
| 101 | + * @return self | |
| 102 | + */ | |
| 103 | + public function setNoTime($set = true) | |
| 104 | + { | |
| 105 | + // this sets a "reserved" property starting with "_" | |
| 106 | + $this->set('_no_time', (bool)$set); | |
| 107 | + | |
| 108 | + return $this; | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * @inheritDoc | |
| 113 | + * | |
| 114 | + * @since 1.16.5 (J) - 1.6.5 (WP) | |
| 115 | + */ | |
| 116 | + public function getOptions() | |
| 117 | + { | |
| 118 | + return (array)$this->get('_options', []); | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Returns the notification data object to be encoded. | |
| 123 | + * | |
| 124 | + * @return object the notification data object for the document. | |
| 125 | + */ | |
| 126 | + public function toDataObject() | |
| 127 | + { | |
| 128 | + // get all data options set | |
| 129 | + $options = $this->getOptions(); | |
| 130 | + | |
| 131 | + // access all "public" (not reserved) properties | |
| 132 | + $props = $this->getProperties($public = true); | |
| 133 | + | |
| 134 | + // merge public properties with data options, if any | |
| 135 | + if ($options) { | |
| 136 | + $props['_options'] = $options; | |
| 137 | + } | |
| 138 | + | |
| 139 | + // build data object | |
| 140 | + $data = (object)$props; | |
| 141 | + $data->type = $this->getType(); | |
| 142 | + $data->dtime = $this->getDateTime(); | |
| 143 | + if ($this->getNoTime()) { | |
| 144 | + $data->no_time = 1; | |
| 145 | + } | |
| 146 | + $data->build_url = $this->generateBuildUrl(); | |
| 147 | + | |
| 148 | + return $data; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Extending classes can override this method when a notification does | |
| 153 | + * NOT require scheduling or building, but rather instant display data. | |
| 154 | + * | |
| 155 | + * @return bool true if display data were built. | |
| 156 | + */ | |
| 157 | + public function buildDisplayData() | |
| 158 | + { | |
| 159 | + return false; | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Returns the URL to build the notification display data. | |
| 164 | + * | |
| 165 | + * @return null|string the url to build the notification display data. | |
| 166 | + */ | |
| 167 | + abstract protected function generateBuildUrl(); | |
| 168 | +} | |