| Current Path : /var/www/html/ebookingdbkl/backend/modules/serviceApplication/models/ |
| Current File : /var/www/html/ebookingdbkl/backend/modules/serviceApplication/models/Notification.php |
<?php
namespace backend\modules\serviceApplication\models;
use Yii;
use common\components\messaging\MySMS;
/**
* This is the model class for table "notification".
*
* @property int $notification_id
* @property int|null $reference_id
* @property string|null $reference_code R_BOOK,R_APP,Q_APP,Q_CHECKIN,Q_CHECKOUT
* @property string|null $subject
* @property string|null $body
* @property string|null $type SMS,EMAIL
* @property string|null $recipient
* @property string|null $status SUCCESS,FAILED
*/
class Notification extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'notification';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['reference_id'], 'integer'],
[['reference_code', 'recipient'], 'string', 'max' => 500],
[['subject'], 'string', 'max' => 250],
[['body'], 'string', 'max' => 80000],
[['type', 'status'], 'string', 'max' => 12],
];
}
public static function email(int $ref_no, $ref_code, array $body, $recipient, $recipient_cc=null, $ignore=false) {
try {
if(empty($body['subject']) || empty($body['content'])) {
return false;
}
$recipient = self::mail_to($recipient, $ignore);
if(!empty($recipient_cc)) {
$recipient_cc = self::mail_to($recipient_cc, $ignore);
}
$mail = Yii::$app->mailer->compose()
->setFrom(Yii::$app->params['adminEmail'])
->setTo($recipient);
if(!empty($recipient_cc)) {
$mail->setCc($recipient_cc);
}
$mail->setSubject($body['subject'])
->setHtmlBody($body['content'])
->send();
$noti = new self();
$noti->reference_id = $ref_no;
$noti->reference_code = $ref_code;
$noti->subject = $body['subject'];
$noti->body = json_encode($body);
$noti->recipient = is_array($recipient) ? json_encode($recipient) : $recipient;
$noti->recipient .= is_array($recipient_cc) ? ' CC ' . json_encode($recipient_cc) : ' CC ' . $recipient_cc;
$noti->type = 'EMAIL';
$noti->status = 'SUCCESS';
if(!$noti->save()) {
throw new \Exception("Error Saving Email notification status" . json_encode($noti->errors));
}
} catch(\Throwable $t) {
$noti = new self();
$noti->reference_id = $ref_no;
$noti->reference_code = $ref_code;
$noti->subject = $body['subject'];
$noti->body = $t->getMessage() . ' - LINE: ' . $t->getLine();
$noti->recipient = is_array($recipient) ? json_encode($recipient) : $recipient;
$noti->type = 'EMAIL';
$noti->status = 'FAILED';
if(!$noti->save()) {
throw new \Exception("Error Saving Email notification status :-" . json_encode($noti->errors));
}
}
return true;
}
public static function db($ref_no, $ref_code, array $body, $recipient) {
if(empty($body['subject']) || empty($body['content'])) {
return false;
}
$noti = new self();
$noti->reference_id = $ref_no;
$noti->reference_code = $ref_code;
$noti->subject = $body['subject'];
$noti->body = json_encode($body);
$noti->recipient = $recipient;
$noti->type = 'DB';
$noti->status = 'UNREAD';
$noti->save();
return true;
}
/**
* Mail to according environment, we don't want sent email to true recipient when we are developing.
*
* @param string $recipient. Who's the recipient
* @param boolean $ignore. ignore the environtment, sent it directly to specified recipient
*
* @return void
*/
public static function mail_to($recipient, $ignore=false) {
if($ignore) {
return $recipient;
}
$param = Yii::$app->params;
return $param['emailEnvironment'] == 'DEV' ? $param['devEmail'] : $recipient;
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'notification_id' => Yii::t('app', 'Notification ID'),
'reference_id' => Yii::t('app', 'Reference ID'),
'reference_code' => Yii::t('app', 'Reference Code'),
'subject' => Yii::t('app', 'Subject'),
'body' => Yii::t('app', 'Body'),
'type' => Yii::t('app', 'Type'),
'recipient' => Yii::t('app', 'Recipient'),
'status' => Yii::t('app', 'Status'),
];
}
}