Your IP : 216.73.216.79


Current Path : /var/www/html/ebookingdbkl/backend/modules/serviceApplication/controllers/
Upload File :
Current File : /var/www/html/ebookingdbkl/backend/modules/serviceApplication/controllers/ServiceTaskController.php

<?php

namespace backend\modules\serviceApplication\controllers;

use Yii;
use backend\modules\serviceApplication\models\ServiceTask;
use backend\modules\serviceApplication\models\ServiceTaskSearch;
use backend\modules\notification\models\NotificationTemplate;
use backend\modules\serviceApplication\queue\NotificationEmailJob;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use backend\modules\serviceApplication\Modules as M;

/**
 * ServiceTaskController implements the CRUD actions for ServiceTask model.
 */
class ServiceTaskController extends Controller {

    const TEMPLATE_TASK_ASSIGMENT = 'TASK_ASSIGNMENT_NOTIFICATION_VIDEO_PHOTO';

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ServiceTask models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new ServiceTaskSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ServiceTask model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ServiceTask model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ServiceTask();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        $model->task_status = 'assign';

        if ($model->load(Yii::$app->request->post())) {

            if($model->save()) {
                $this->sendNotification($model->task_id);
            }

            return $this->redirect(['index']);
        }
        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing ServiceTask model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(['index']);
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing ServiceTask model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {

        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the ServiceTask model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ServiceTask the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = ServiceTask::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(M::t('app', 'PageNotExist'));
    }

    public function actionIn($id) {
        $model = $this->findModel($id);
        $model->task_status = 'taskin';
        $model->task_in = date("Y-m-d H:i:s");
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->save())
            return $this->redirect(['index']);
    }

    public function actionOut($id) {
        $model = $this->findModel($id);
        $model->task_status = 'taskout';
        $model->task_out = date("Y-m-d H:i:s");
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        
        $application = \backend\modules\serviceApplication\models\ServiceApplication::findOne($model->service_id);
        $application->service_status = 'complete';
        $application->save(); 
        
        if ($model->save())
            return $this->redirect(['index']);
    }

    public function sendNotification($task_id) {

        $task = ServiceTask::findOne($task_id);

        if(empty($task))
            return false;

        // $app = ServiceApplication::find()->where(['service_id' => $task])->one();

        $template = NotificationTemplate::find()
                        ->where(['code' => self::TEMPLATE_TASK_ASSIGMENT])
                        ->one();

        if(empty($template))
            return false;

        $service = [];

        if($task->application->service_photo) {
            $service[] = 'Jurugambar';
        }

        if($task->application->service_video) {
            $service[] = 'Juruvideo';
        }

        $content = strtr($template->content, [
            '{{DATE}}' => date('d-m-Y'),
            '{{APPLICATION_NAME}}' => $task->application->service_programme,
            '{{APPLICATION_DATE}}' => $task->application->service_date_start . ' ' . 
                                      $task->application->service_time_start . ' - ' . 
                                      $task->application->service_date_end . ' ' . 
                                      $task->application->service_time_end,
            '{{APPLICATION_TYPE}}' => implode(' / ', $service),
        ]);

        $body = [];
        $body['subject'] = $template->name;
        $body['content'] = $content;
        
        $this->emailJob($task_id, $body, $task->pic->user->email);

        return true;

    }

    public function actionStub() {
        $this->sendNotification(2);
    }

    private function emailJob($id, $body, $rec){
        /** email **/
        try {
            Yii::$app->queue->push(
                new NotificationEmailJob([
                    'ref_id' => $id,
                    'ref_code' => 'TASK_S_APP',
                    'body' => $body,
                    'recipient' => $rec
                    ])
                );  
            
        } catch(\Throwable $t) {
            throw new \Exception("Error Sending Email To Queue - " . $t->getMessage());
        }
    }

}