| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2026 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 |
* Chat user operator wrapper. |
| 16 |
* |
| 17 |
* @since 1.8 |
| 18 |
*/ |
| 19 |
class VBOChatUserOperator extends VBOChatUseraware implements VBOChatNotifiable |
| 20 |
{ |
| 21 |
use VBOChatNotificationEmail; |
| 22 |
use VBOChatNotificationApp; |
| 23 |
|
| 24 |
/** @var object */ |
| 25 |
protected $operator; |
| 26 |
|
| 27 |
/** |
| 28 |
* Class constructor. |
| 29 |
* |
| 30 |
* @param mixed The operator details. |
| 31 |
*/ |
| 32 |
public function __construct($operator = null) |
| 33 |
{ |
| 34 |
if (!$operator) { |
| 35 |
// fetch details of the logged in operator |
| 36 |
$operator = VikBooking::getOperatorInstance()->getOperatorAccount(); |
| 37 |
} |
| 38 |
|
| 39 |
if (is_numeric($operator)) { |
| 40 |
// fetch details of the specified operator |
| 41 |
$operator = VikBooking::getOperatorInstance()->getOne((int) $operator); |
| 42 |
} |
| 43 |
|
| 44 |
if (!$operator) { |
| 45 |
throw new RuntimeException('The logged in user is not an operator.', 403); |
| 46 |
} |
| 47 |
|
| 48 |
$this->operator = (object) $operator; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @inheritDoc |
| 53 |
* |
| 54 |
* @see VBOChatUser |
| 55 |
*/ |
| 56 |
public function getID() |
| 57 |
{ |
| 58 |
return (int) $this->operator->id; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @inheritDoc |
| 63 |
* |
| 64 |
* @see VBOChatUser |
| 65 |
*/ |
| 66 |
public function getName() |
| 67 |
{ |
| 68 |
return trim($this->operator->first_name . ' ' . $this->operator->last_name); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @inheritDoc |
| 73 |
* |
| 74 |
* @see VBOChatUser |
| 75 |
*/ |
| 76 |
public function getAvatar() |
| 77 |
{ |
| 78 |
$image = $this->operator->pic ?? ''; |
| 79 |
|
| 80 |
if ($image) { |
| 81 |
// check whether we have an image name or a full URI |
| 82 |
$image = preg_match("/^https?:\/\//i", $image) ? $image : VBO_SITE_URI . 'resources/uploads/' . $image; |
| 83 |
} |
| 84 |
|
| 85 |
return $image; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @inheritDoc |
| 90 |
* |
| 91 |
* @see VBOChatUser |
| 92 |
*/ |
| 93 |
public function can(string $scope, ?VBOChatContext $context = null) |
| 94 |
{ |
| 95 |
if ($context) { |
| 96 |
// delegate the validation to the context |
| 97 |
return $context->can($scope, $this); |
| 98 |
} |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @inheritDoc |
| 105 |
* |
| 106 |
* @see VBOChatNotifiable |
| 107 |
*/ |
| 108 |
public function scheduleNotification(VBOChatMessage $message, VBOChatUser $user) |
| 109 |
{ |
| 110 |
if (!empty($this->operator->email)) { |
| 111 |
/** |
| 112 |
* If the e-mail address exists, send a notification to this operator. |
| 113 |
* |
| 114 |
* @see VBOChatNotificationEmail |
| 115 |
*/ |
| 116 |
$this->sendEmailNotification($message, $this->operator->email, $this->getName()); |
| 117 |
|
| 118 |
/** |
| 119 |
* Send notification to the mobile APP as well. |
| 120 |
* |
| 121 |
* @since 1.8.15 |
| 122 |
* @see VBOChatNotificationApp |
| 123 |
*/ |
| 124 |
$this->scheduleAppNotification($message, $this->operator->email); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|