Your IP : 216.73.216.79


Current Path : /var/www/html/ekursus/backend/controllers/
Upload File :
Current File : /var/www/html/ekursus/backend/controllers/MessageGroupController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\MessageGroup;
use backend\models\MessageGroupSearch;
use backend\models\MessageGroupMember;
use backend\models\MessageGroupMemberSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MessageGroupController implements the CRUD actions for MessageGroup model.
 */
class MessageGroupController extends Controller {

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

    public function actionIndex() {
        $searchModel = new MessageGroupSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    public function actionMyGroup() {
        $searchModel = new MessageGroupSearch();
//        $searchModel->grptype = '01'; //display individual only
        $searchModel->created_by = Yii::$app->user->identity->staff->id;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

//        $searchModel = new DataDeliverySearch();
//        $searchModel->agencycode = Yii::$app->session['agencycode'];
//        $dataProvider = $searchModel->searchAgency(Yii::$app->request->queryParams);
//        //$dataProvider = $searchModel->searchAgency(Yii::$app->request->queryParams, Yii::$app->session['agencycode']);
//        $dataProvider->pagination->pageSize = Yii::$app->params['pagingsize'];
//
//        return $this->render('indexAgency', ['searchModel' => $searchModel,'dataProvider' => $dataProvider]);
//    }

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

    /**
     * Creates a new MessageGroup model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new MessageGroup();
        $model->group_category = '01';
//        $model->activeind = '1';
        //$test = new MessageGroupMember();

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

//            $test->user_group_id = $model->id;
//
//            $test->save();

            $log = new \backend\models\Log();
            $log->module = Yii::$app->controller->id;
            $log->details = Yii::$app->controller->action->id . ":" . $model->id;

            $log->save();

            Yii::$app->session->setFlash('success', Yii::t('app', 'The message group has been successfully added.'));
            return $this->redirect(['update', 'id' => $model->id]);
        } else {
            return $this->render('create', ['model' => $model]);
        }
    }

    public function actionCreateMyGroup() {
        $model = new MessageGroup();
        $model->group_category = '01';
//        $model->grptype = '01';
//        $model->activeind = '1';
        $model->scenario = 'staff';

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

            $model->group_category = '01';
//             $model->grptype = '01';

            $log = new \backend\models\Log();
            $log->module = Yii::$app->controller->id;
            $log->details = Yii::$app->controller->action->id . ":" . $model->id;

            $log->save();

            Yii::$app->session->setFlash('success', Yii::t('app', 'The message group has been successfully added.'));
            return $this->redirect(['update-my-group', 'id' => $model->id]);
        } else {
            return $this->render('createMyGroup', ['model' => $model]);
        }
    }

    /**
     * Updates an existing MessageGroup model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        $searchModel = new MessageGroupMemberSearch();
        $searchModel->message_group_id = $id;
        //$dataProvider = $searchModel->search();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data['searchModel'] = $searchModel;
        $data['dataProvider'] = $dataProvider;
        $data['model'] = $model;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $log = new \backend\models\Log();
            $log->module = Yii::$app->controller->id;
            $log->details = Yii::$app->controller->action->id . ":" . $model->id;

            $log->save();
            Yii::$app->session->setFlash('success', Yii::t('app', 'The new message group has been successfully update.'));
            return $this->redirect(['index', 'id' => $model->id]);
        } else {
            return $this->render('update', $data);
        }
    }

    public function actionUpdateMyGroup($id) {
        $model = $this->findModel($id);
        $model->scenario = 'staff';

        $searchModel = new MessageGroupMemberSearch();
        $searchModel->message_group_id = $id;
        //$searchModel->scenario = 'staff';
        //$dataProvider = $searchModel->search();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data['searchModel'] = $searchModel;
        $data['dataProvider'] = $dataProvider;
        $data['model'] = $model;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $log = new \backend\models\Log();
            $log->module = Yii::$app->controller->id;
            $log->details = Yii::$app->controller->action->id . ":" . $model->id;

            $log->save();
            Yii::$app->session->setFlash('success', Yii::t('app', 'The new message group has been successfully update.'));
            return $this->redirect(['my-group', 'id' => $model->id]);
        } else {
            return $this->render('updateMyGroup', $data);
        }
    }

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


        $log = new \backend\models\Log();
        $log->module = Yii::$app->controller->id;
        $log->details = Yii::$app->controller->action->id . ":" . $id;

        $log->save();

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

    public function actionDeleteMyGroup($id) {
        MessageGroupMember::deleteAll('message_group_id = ' . $id);
        $this->findModel($id)->delete();


        $log = new \backend\models\Log();
        $log->module = Yii::$app->controller->id;
        $log->details = Yii::$app->controller->action->id . ":" . $id;

        $log->save();

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

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

}