| Current Path : /var/www/html/eform/frontend/models/ |
| Current File : /var/www/html/eform/frontend/models/Notification.php |
<?php
namespace frontend\models;
use Yii;
/**
* 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],
];
}
/**
* {@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'),
];
}
/**
* Email function
* @param int $ref_no reference number in integer usualy id
* @param string $ref_code reference code in string
* @param array $body email body ['subject' => '', 'content' => '']
* @param mixed $recipient recipient
* @param mixed|null $recipient_cc cc recipient
* @return void
*/
public static function email(
int $ref_no,
string $ref_code,
array $body,
mixed $recipient,
mixed $recipient_cc=null,
$view=null, $data=[]) {
try {
if(empty($body['subject'])) {
return false;
}
if(empty($body['content']) && empty($view)) {
return false;
}
$mail = Yii::$app->mailer->compose($view, $data)
->setFrom(Yii::$app->params['adminEmail'])
->setTo($recipient);
if(!empty($recipient_cc)) {
$mail->setCc($recipient_cc);
}
$mail->setSubject($body['subject']);
/** send the html body if not template pass */
if(empty($view)) {
$mail->setHtmlBody($body['content']);
}
$mail->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;
}
}