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; //Check email enable if (!$this->template && $this->templateName) return false; //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); } //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(); } } }