Your IP : 216.73.216.79


Current Path : /var/www/html/school/backend/controllers/
Upload File :
Current File : /var/www/html/school/backend/controllers/MessageController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\Message;
use backend\models\MessageSearch;
use backend\models\Staff;
use backend\models\MessageGroupMember;
use backend\models\Log;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * MessageController implements the CRUD actions for Message model.
 */
class MessageController extends Controller {

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

    /**
     * Lists all Message models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new MessageSearch();
        $searchModel->recipient = Yii::$app->user->identity->staff->id;
        $searchModel->box = 'IN';
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    public function actionIndexout() {
        $searchModel = new MessageSearch();
        $searchModel->sender = Yii::$app->user->identity->staff->id;
        $searchModel->box = 'OUT';
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single Message model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Message model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionSearchRecipient($sid, $sname, $sdialog) {
        $model = new Staff();

        return $this->renderPartial('search_recipient', ['model' => $model, 'sid' => $sid, 'sname' => $sname, 'sdialog' => $sdialog]);
    }

    public function actionListRecipient($staff_name, $projectnm, $sid, $sname, $sdialog) {
        $sql = "SELECT a.staff_name from dmg_user_info a INNER JOIN dmg_user b ON a.userid=b.id;";
        if ($staff_name !== '')
            $sql .= " AND a.staff_name LIKE '%" . $staff_name . "%'";

        $mStaff = Staff::findBySql($sql)->all();

        $data['mStaff'] = $mStaff;
        $data['sid'] = $sid;
        $data['sname'] = $sname;
        $data['sdialog'] = $sdialog;
        return $this->renderPartial('list_recipient', $data);
    }

    public function actionCreate() {
        $model = new Message();
        $id = Yii::$app->user->identity->staff->id;
        $staff = Staff::findOne($id);
        //$model->status = 99;//draft
        //$model->status = 10;//IN
//        $model->status = 10;
//        $model->box = 'IN';
//        $model->sender = Yii::$app->user->identity->staff->id;
        if ($model->load(Yii::$app->request->post())) {


            $model->sender = Yii::$app->user->identity->staff->id;
            $recipients = Yii::$app->request->post('staff');

//            $recipient = [];

            foreach ($recipients as $recipient) {
                $message = new Message();
//                $message->sender = $sender;
                $message->recipient = $recipient;
//                $message->isNewRecord = true;
//                $message->recipient = $m;
                $message->message = $model->message;
                $message->title = $model->title;

                $message->sender = $model->sender;

                $message->status = $model->status;


                $message->box = 'IN';
                $message->status = 10;
                $message->date_sent = Yii::$app->formatter->asDatetime('now', 'php:Y-m-d');
                $message->time_sent = Yii::$app->formatter->asDatetime('now', 'php:H:i:s');
                $message->updated_at = Yii::$app->formatter->asDatetime('now', 'php:Y-m-d H:i:s');
                
                $model->pic = UploadedFile::getInstance($model, 'pic');

                if (!empty($model->pic)) {
                    $path = 'uploads/message/'; //'../../www/admin/uploads/message/';
                    $model->pic->saveAs($path . date('Y_m_d_H_i_s') . '_' . $model->pic->baseName . '.' . $model->pic->extension);

                    $message->pic_sys = $path . date('Y_m_d_H_i_s') . '_' . $model->pic->baseName . '.' . $model->pic->extension;
                    $message->pic = $model->pic->baseName . "." . $model->pic->extension;
                } else {
                    $model->pic = NULL;
                }


                if ($message->save()) {
                    $log = new Log();
                    $log->module = Yii::$app->controller->id;
                    $log->details = Yii::$app->controller->action->id . ":" . $message->id;
                    $log->save();
                } else {
                    var_dump($message->errors);
                    exit;
                }

                Yii::$app->session->setFlash('success', Yii::t('app', 'Mesej berjaya dihantar'));
                return $this->redirect(['sent', 'id' => $message->id]);
            }
        } else {
//            var_dump($model);
//            exit;

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

    public function actionReply($id) {
        $model = $this->findModel($id);
        
        $id = Yii::$app->user->identity->staff->id;
        $staff = Staff::findOne($id);
        $model->recipient = $model->sender;
        $model->sender = $model->recipient;
        $model->title = 'Re:' . $model->title;
        
        
        //$model->status = 99;//draft
        //$model->status = 10;//IN
//        $model->status = 10;
//        $model->box = 'IN';
//        $model->sender = Yii::$app->user->identity->staff->id;
        if ($model->load(Yii::$app->request->post())) {


            $model->sender = Yii::$app->user->identity->staff->id;
            $recipients = Yii::$app->request->post('staff');

//            $recipient = [];

            foreach ($recipients as $recipient) {
                $message = new Message();
//                $message->sender = $sender;
                $message->recipient = $recipient;
//                $message->isNewRecord = true;
//                $message->recipient = $m;
                $message->message = $model->message;
                $message->title = $model->title;

                $message->sender = $model->sender;

                $message->status = $model->status;


                $message->box = 'IN';
                $message->status = 10;
                $message->date_sent = Yii::$app->formatter->asDatetime('now', 'php:Y-m-d');
//                $message->time_sent = Yii::$app->formatter->asDatetime('now', 'php:H:i:s');
                $message->time_sent = new Expression('NOW()');
                $message->updated_at = Yii::$app->formatter->asDatetime('now', 'php:Y-m-d H:i:s');
                
                $model->pic = UploadedFile::getInstance($model, 'pic');

                if (!empty($model->pic)) {
                    $path = 'uploads/message/'; //'../../www/admin/uploads/message/';
                    $model->pic->saveAs($path . date('Y_m_d_H_i_s') . '_' . $model->pic->baseName . '.' . $model->pic->extension);

                    $message->pic_sys = $path . date('Y_m_d_H_i_s') . '_' . $model->pic->baseName . '.' . $model->pic->extension;
                    $message->pic = $model->pic->baseName . "." . $model->pic->extension;
                } else {
                    $model->pic = NULL;
                }


                if ($message->save()) {
                    $log = new Log();
                    $log->module = Yii::$app->controller->id;
                    $log->details = Yii::$app->controller->action->id . ":" . $message->id;
                    $log->save();
                } else {
                    var_dump($message->errors);
                    exit;
                }

                Yii::$app->session->setFlash('success', Yii::t('app', 'Mesej berjaya dihantar'));
                return $this->redirect(['sent', 'id' => $message->id]);
            }
        } else {
            return $this->render('update', ['model' => $model,]);
        }
    }
    
    public function actionSent() {
        $searchModel = new MessageSearch();
        $searchModel->sender = Yii::$app->user->identity->staff->id;
        $searchModel->box = 'IN';
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Deletes an existing Message model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

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

    public function actionDeleteincheck() {
        switch ($_POST['submitbutton']) {
            case "deletebtn": {
                    if ($selection = (array) Yii::$app->request->post('selection')) {
                        foreach ($selection as $id) {
                            $this->findModel($id)->delete();
                        }
//                    \Yii::$app->session->setFlash('msg', 'Peti masuk telah dihapus');
                    }
                }
                return $this->redirect(['index']);
        }
    }

    public function actionDeleteoutcheck() {
        switch ($_POST['submitbutton']) {
            case "deletebtn": {
                    if ($selection = (array) Yii::$app->request->post('selection')) {
                        foreach ($selection as $id) {
                            $this->findModel($id)->delete();
                        }
//                    \Yii::$app->session->setFlash('msg', 'Peti masuk telah dihapus');
                    }
                }
                return $this->redirect(['indexout']);
        }
    }

    public function actionRead() {
        $model = $this->findModel($id);

        $model->read = '1';
        $model->save();
    }

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

}