Your IP : 216.73.216.79


Current Path : /var/www/html/ebookingdbkl/common/models/
Upload File :
Current File : /var/www/html/ebookingdbkl/common/models/Notification.php

<?php

namespace common\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' => 45],
            [['subject'], 'string', 'max' => 250],
            [['body'], 'string', 'max' => 10000],
            [['type', 'status'], 'string', 'max' => 12],
        ];
    }
    
    public static function email(int $ref_no=0000, $ref_code, array $body, $recipient, $ignore=false) { // 0000 system

        try {

        if(empty($body['subject']) || empty($body['content'])) {
            return false;
        }

        $recipient = self::mail_to($recipient, $ignore);

        $mail = Yii::$app->mailer->compose()
            ->setFrom(Yii::$app->params['adminEmail'])
            ->setTo($recipient) 
            ->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->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;
    }
    
    public static function sms($ref_no, $ref_code, array $body, $recipient) {

        try {
            $recipient =  preg_replace('/[^A-Za-z0-9\-]/', '', $recipient);

            $sms = new MySMS();

            $body = $sms->send($recipient, $body['content']); /// number without country code

            $body = $body->getContents();

            // $body = '{
            //     "status_code": 200,
            //     "message": "SMS queued successfully.",
            //     "error": false,
            //     "code": 12134,
            //     "data": {
            //         "id": "4c763c5857f54cfbb092f2ce6d71ca71",
            //         "user_id": 0,
            //         "campaign_id": 0,
            //         "schedule_id": false,
            //         "cost": 0.1,
            //         "status": -1,
            //         "completed_at": 1649950104,
            //         "country": "MY",
            //         "phone_number": "+601110878935",
            //         "created_at": 1649950104,
            //         "preview": "EBOOKING-DBKL: cobaan penghatarana sms",
            //         "message_id": "4c763c5857f54cfbb092f2ce6d71ca71"
            //     }
            // }';

            $body_de = json_decode($body);

            $noti = new self();
            $noti->reference_id = $ref_no;
            $noti->reference_code = $ref_code;
            $noti->subject = $body_de->data->preview;
            $noti->body = is_object($body) ? json_encode($body) : $body;
            $noti->recipient = $recipient;
            $noti->type = 'SMS';
            $noti->status = 'SUCCESS';
            
            if(!$noti->save()) {
                throw new \Exception("Error Saving SMS notification status" .  json_encode($noti->errors) . json_encode($body));
            }
        } catch (\Throwable $t) {

            $noti = new self();
            $noti->reference_id = $ref_no;
            $noti->reference_code = $ref_code;
            $noti->subject = 'NO_SUBJECT';
            $noti->body = $t->getMessage() . ' - LINE: ' . $t->getLine();
            $noti->recipient = $recipient;
            $noti->type = 'SMS';
            $noti->status = 'FAILED';
            $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'),
        ];
    }
}