File size: 6,413 Bytes
52d9fb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | <?php
namespace App\Notify;
use App\Constants\Status;
use App\Models\AdminNotification;
use App\Models\NotificationLog;
use App\Models\NotificationTemplate;
class NotifyProcess{
/*
|--------------------------------------------------------------------------
| Notification Process
|--------------------------------------------------------------------------
|
| This is the core processor to send a notification to receiver. In this
| class, find the notification template from database and build the final
| message replacing the short codes and provide this to the method to send
| the notification. Also notification log and error is creating here.
|
*/
/**
* Template name, which contain the short codes and messages
*
* @var string
*/
public $templateName;
/**
* Short Codes, which will be replaced
*
* @var array
*/
public $shortCodes;
/**
* Instance of user, who will get the notification
*
* @var object
*/
public $user;
/**
* Status field name in database of notification template
*
* @var string
*/
protected $statusField;
/**
* Global template field name in database of notification method
*
* @var string
*/
protected $globalTemplate;
/**
* Message body field name in database of notification
*
* @var string
*/
protected $body;
/**
* Notification template instance
*
* @var object
*/
public $template;
/**
* Message, if the email template doesn't exists
*
* @var string|null
*/
public $message;
/**
* Notification log will be created or not
*
* @var bool
*/
public $createLog;
/**
* Method configuration field name in database
*
* @var string
*/
public $notifyConfig;
/**
* Subject of notification
*
* @var string
*/
public $subject;
/**
* Name of receiver
*
* @var string
*/
public $receiverName;
/**
* The relational field name like user_id, agent_id
*
* @var string
*/
public $userColumn;
/**
* Address of receiver, like email, mobile number etc
*
* @var string
*/
protected $toAddress;
/**
* Final message of notification
*
* @var string
*/
protected $finalMessage;
/**
* Notification sent from
*
* @var string
*/
protected $sentFrom = null;
/**
* Get the final message after replacing the short code.
*
* Also custom message will be return from here if notification template doesn't exist.
*
* @return string
*/
protected function getMessage(){
$this->prevConfiguration();
$body = $this->body;
$user = $this->user;
$globalTemplate = $this->globalTemplate;
//finding the notification template
$template = NotificationTemplate::where('act', $this->templateName)->where($this->statusField, Status::ENABLE)->first();
$this->template = $template;
//Getting the notification message from database if use and template exist
//If not exist, get the message which have sent via method
if ($user && $template) {
$message = $this->replaceShortCode($user->fullname,$user->username,gs($globalTemplate),$template->$body);
if (empty($message)) {
$message = $template->$body;
}
}else{
$message = $this->replaceShortCode($this->receiverName,$this->toAddress,gs($globalTemplate),$this->message);
}
//replace the all short code of template
if ($this->shortCodes) {
$message = $this->replaceTemplateShortCode($message);
}
//Check email enable
if (!$this->template && $this->templateName) return false;
//set subject to property
$this->getSubject();
$this->finalMessage = $message;
//return the final message
return $message;
}
/**
* Replace the short code of global template
*
* @return string
*/
protected function replaceShortCode($name,$username,$template,$body){
if(is_array($username)){
$username = implode(',',$username);
}
$message = str_replace("{{fullname}}", $name, $template);
$message = str_replace("{{username}}", $username, $message);
$message = str_replace("{{message}}", $body, $message);
return $message;
}
/**
* Replace the short code of the template
*
* @return string
*/
protected function replaceTemplateShortCode($content){
foreach ($this->shortCodes ?? [] as $code => $value) {
$content = str_replace('{{' . $code . '}}', $value, $content);
}
return $content;
}
/**
* Set the subject with replaced the short codes
*
* @return void
*/
protected function getSubject(){
if ($this->template) {
$subject = $this->template->subject;
if ($this->shortCodes) {
foreach ($this->shortCodes as $code => $value) {
$subject = str_replace('{{' . $code . '}}', $value, $subject);
}
}
$this->subject = $subject;
}
}
/**
* Create the notification log
*
* @return void
*/
public function createErrorLog($message){
$adminNotification = new AdminNotification();
$adminNotification->user_id = 0;
$adminNotification->title = $message;
$adminNotification->click_url = '#';
$adminNotification->save();
}
/**
* Create the error log
*
* @return void
*/
public function createLog($type){
$userColumn = $this->userColumn;
if ($this->user && $this->createLog) {
$notifyConfig = $this->notifyConfig;
$config = gs($notifyConfig);
$notificationLog = new NotificationLog();
if (@$this->user->id) {
$notificationLog->$userColumn = $this->user->id;
}
$notificationLog->notification_type = $type;
$notificationLog->sender = @$config->name ?? 'firebase';
$notificationLog->sent_from = $this->sentFrom;
$notificationLog->sent_to = $type == 'push' ? 'Firebase Token' : $this->toAddress;
$notificationLog->subject = $this->subject;
$notificationLog->image = @$this->pushImage ?? null;
$notificationLog->message = $type == 'email' ? $this->finalMessage : strip_tags($this->finalMessage);
$notificationLog->save();
}
}
}
|