servicesearch.php
346 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 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 | * VikAppointments Service Search view contents handler. |
| 16 | * This class provides helpful tools to enhance the |
| 17 | * <head> of the servicesearch pages. |
| 18 | * |
| 19 | * @since 1.6 |
| 20 | */ |
| 21 | class VAPViewContentsServiceSearch extends VAPViewContents |
| 22 | { |
| 23 | /** |
| 24 | * A registry containing the service information. |
| 25 | * |
| 26 | * @var JRegistry |
| 27 | */ |
| 28 | protected $service; |
| 29 | |
| 30 | /** |
| 31 | * The service description. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | protected $description = null; |
| 36 | |
| 37 | /** |
| 38 | * The maximum number of characters to display |
| 39 | * for the service description. |
| 40 | * |
| 41 | * @var integer |
| 42 | */ |
| 43 | protected $descLength = 512; |
| 44 | |
| 45 | /** |
| 46 | * Class constructor. |
| 47 | * |
| 48 | * @param mixed $page The view object. |
| 49 | * @param string $type The view file/type. If not provided |
| 50 | * it will be calculated from the $page class name. |
| 51 | */ |
| 52 | public function __construct($page, $type = null) |
| 53 | { |
| 54 | parent::__construct($page, $type); |
| 55 | |
| 56 | if (isset($page->service)) |
| 57 | { |
| 58 | $tmp = $page->service; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | $tmp = array(); |
| 63 | } |
| 64 | |
| 65 | $this->service = new JRegistry($tmp); |
| 66 | |
| 67 | /** |
| 68 | * Create service metadata registry. |
| 69 | * |
| 70 | * @since 1.6.1 |
| 71 | */ |
| 72 | $meta = (array) json_decode($this->service->get('metadata', ''), true); |
| 73 | |
| 74 | $this->service->set('metadata', new JRegistry($meta)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @override |
| 79 | * Sets the meta description according to the settings of the page. |
| 80 | * |
| 81 | * @return boolean True if set, otherwise false. |
| 82 | */ |
| 83 | public function setMetaDescription() |
| 84 | { |
| 85 | /** |
| 86 | * If provided, use the service meta description. |
| 87 | * |
| 88 | * @since 1.6.1 |
| 89 | */ |
| 90 | if ($desc = $this->service->get('metadata')->get('description')) |
| 91 | { |
| 92 | $this->page->document->setDescription($desc); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // before all, set the description of the current menu item |
| 98 | $set = parent::setMetaDescription(); |
| 99 | |
| 100 | // if the description was empty or the current menu item is |
| 101 | // not equals to the view set in the request, try to use |
| 102 | // the description related to the service |
| 103 | if (!$set || $this->activeView != $this->currentView) |
| 104 | { |
| 105 | // get service description |
| 106 | $desc = strip_tags($this->service->get('description', '')); |
| 107 | |
| 108 | if ($desc) |
| 109 | { |
| 110 | $this->page->document->setDescription($desc); |
| 111 | |
| 112 | $set = true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return $set; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @override |
| 121 | * Sets the meta keywords according to the settings of the page. |
| 122 | * |
| 123 | * @return self This object to support chaining. |
| 124 | * |
| 125 | * @return boolean True if set, otherwise false. |
| 126 | * |
| 127 | * @since 1.6.1 |
| 128 | */ |
| 129 | public function setMetaKeywords() |
| 130 | { |
| 131 | // if provided, use the service meta keywords |
| 132 | if ($keys = $this->service->get('metadata')->get('keywords')) |
| 133 | { |
| 134 | $this->page->document->setMetaData('keywords', $keys); |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | // otherwise use default method |
| 140 | return parent::setMetaKeywords(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @override |
| 145 | * Sets the browser page title. |
| 146 | * |
| 147 | * @return boolean True if set, otherwise false. |
| 148 | * |
| 149 | * @since 1.6.1 |
| 150 | */ |
| 151 | public function setPageTitle() |
| 152 | { |
| 153 | // if provided, use the service meta title |
| 154 | if ($title = $this->service->get('metadata')->get('title')) |
| 155 | { |
| 156 | $this->page->document->setTitle($title); |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | // if the current menu item is not equals to the view set |
| 162 | // in the request, try to use the title related to the service |
| 163 | if ($this->activeView != $this->currentView) |
| 164 | { |
| 165 | $title = $this->page->document->getTitle() . ' - ' . $this->service->get('name'); |
| 166 | |
| 167 | $this->page->document->setTitle($title); |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns the service description. |
| 177 | * |
| 178 | * @return string A short version of the description. |
| 179 | */ |
| 180 | protected function getServiceDescription() |
| 181 | { |
| 182 | if ($this->description === null) |
| 183 | { |
| 184 | // get service description |
| 185 | $desc = $this->service->get('description', ''); |
| 186 | |
| 187 | if ($desc) |
| 188 | { |
| 189 | // render HTML description to grab plugin contents and short description |
| 190 | VikAppointments::renderHtmlDescription($desc, 'microdata'); |
| 191 | |
| 192 | // strip HTML tags from description |
| 193 | $desc = strip_tags($desc); |
| 194 | |
| 195 | // check if the description length exceeds the limit |
| 196 | if (strlen($desc) > $this->descLength) |
| 197 | { |
| 198 | // use the first N characters of the service description |
| 199 | $desc = mb_substr(strip_tags($desc), 0, $this->descLength, 'UTF-8') . '...'; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // cache description |
| 204 | $this->description = $desc; |
| 205 | } |
| 206 | |
| 207 | return $this->description; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @override |
| 212 | * Creates the OPEN GRAPH protocol according to the |
| 213 | * entity of the current page. |
| 214 | * |
| 215 | * @return self This object to support chaining. |
| 216 | * |
| 217 | * @uses getServiceDescription() |
| 218 | */ |
| 219 | public function buildOpenGraph() |
| 220 | { |
| 221 | $doc = $this->page->document; |
| 222 | |
| 223 | // basic metadata |
| 224 | $doc->setMetaData('og:title' , $this->service->get('name')); |
| 225 | $doc->setMetaData('og:description', $this->getServiceDescription()); |
| 226 | $doc->setMetaData('og:type' , 'website'); |
| 227 | |
| 228 | if ($this->service->get('image')) |
| 229 | { |
| 230 | $doc->setMetaData('og:image', VAPMEDIA_URI . $this->service->get('image')); |
| 231 | } |
| 232 | |
| 233 | // invoke parent to include generic metadata |
| 234 | return parent::buildOpenGraph(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @override |
| 239 | * Returns the microdata object to include within the head of the page. |
| 240 | * Inherits this method to dispatch the attachment of the properties to |
| 241 | * children classes. |
| 242 | * |
| 243 | * @param object &$json The root object used to attach microdata. |
| 244 | * |
| 245 | * @return boolean True in case the object should be attached, otherwise false. |
| 246 | */ |
| 247 | protected function getMicrodataObject(&$json) |
| 248 | { |
| 249 | /** |
| 250 | * Service schema type. |
| 251 | * |
| 252 | * @link https://schema.org/Service |
| 253 | */ |
| 254 | |
| 255 | $config = VAPFactory::getConfig(); |
| 256 | |
| 257 | // define schema type |
| 258 | $json->{"@context"} = 'http://schema.org'; |
| 259 | $json->{"@type"} = 'Service'; |
| 260 | |
| 261 | // add service information |
| 262 | $json->name = $this->service->get('name'); |
| 263 | $json->description = $this->getServiceDescription(); |
| 264 | $json->url = JUri::root(); |
| 265 | |
| 266 | if ($this->service->get('image')) |
| 267 | { |
| 268 | $json->image = VAPMEDIA_URI . $this->service->get('image'); |
| 269 | } |
| 270 | |
| 271 | if ($config->get('companylogo')) |
| 272 | { |
| 273 | $json->logo = VAPMEDIA_URI . $config->get('companylogo'); |
| 274 | } |
| 275 | |
| 276 | // get reviews |
| 277 | $reviews = $this->service->get('reviews'); |
| 278 | |
| 279 | // add reviews |
| 280 | if ($reviews && $reviews->size > 0) |
| 281 | { |
| 282 | /** |
| 283 | * AggregateRating schema type. |
| 284 | * |
| 285 | * @link https://schema.org/AggregateRating |
| 286 | */ |
| 287 | $json->aggregateRating = new stdClass; |
| 288 | $json->aggregateRating->{"@type"} = 'AggregateRating'; |
| 289 | $json->aggregateRating->ratingValue = $this->service->get('rating'); |
| 290 | $json->aggregateRating->reviewCount = $reviews->size; |
| 291 | |
| 292 | // add latest 2 reviews |
| 293 | $json->review = array(); |
| 294 | |
| 295 | // number of reviews to show |
| 296 | $lim = min(array((int) $reviews->size, 2)); |
| 297 | |
| 298 | for ($i = 0; $i < $lim; $i++) |
| 299 | { |
| 300 | $tmp = $reviews->rows[$i]; |
| 301 | |
| 302 | /** |
| 303 | * Review schema type. |
| 304 | * |
| 305 | * @link https://schema.org/Review |
| 306 | */ |
| 307 | $review = new stdClass; |
| 308 | $review->{"@type"} = 'Review'; |
| 309 | $review->author = $tmp->name; |
| 310 | $review->datePublished = JFactory::getDate($tmp->timestamp)->format('Y-m-d'); |
| 311 | $review->description = $tmp->comment; |
| 312 | $review->name = $tmp->title; |
| 313 | |
| 314 | /** |
| 315 | * Rating schema type. |
| 316 | * |
| 317 | * @link https://schema.org/Rating |
| 318 | */ |
| 319 | $review->reviewRating = new stdClass; |
| 320 | $review->reviewRating->{"@type"} = 'Rating'; |
| 321 | $review->reviewRating->bestRating = 5; |
| 322 | $review->reviewRating->ratingValue = $tmp->rating; |
| 323 | $review->reviewRating->worstRating = 1; |
| 324 | |
| 325 | $json->review[] = $review; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // add offer |
| 330 | if ($this->service->get('price', 0) > 0) |
| 331 | { |
| 332 | /** |
| 333 | * Offer schema type. |
| 334 | * |
| 335 | * @link https://schema.org/Offer |
| 336 | */ |
| 337 | $json->offers = new stdClass; |
| 338 | $json->offers->{"@type"} = 'Offer'; |
| 339 | $json->offers->price = $this->service->get('price'); |
| 340 | $json->offers->priceCurrency = $config->get('currencyname'); |
| 341 | } |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | } |
| 346 |