Your IP : 216.73.216.79


Current Path : /var/www/html/persada/backend/modules/contentPoll/controllers/
Upload File :
Current File : /var/www/html/persada/backend/modules/contentPoll/controllers/ContentPollController.php

<?php

namespace backend\modules\contentPoll\controllers;

use Yii;
use backend\modules\contentPoll\models\ContentPoll;
use backend\modules\contentPoll\models\ContentPollSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ContentPollController implements the CRUD actions for ContentPoll model.
 */
class ContentPollController extends Controller {

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

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

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

    /**
     * Displays a single ContentPoll 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 ContentPoll model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ContentPoll();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->poll_main = 1;
        if ($model->load(Yii::$app->request->post())) {

            $poll_answer_arr = [];
            if (!empty($_POST['answer_main_no'])) {
                foreach ($_POST['answer_main_no'] as $answer_main_no) {
                    $poll_answer_arr [] = $_POST['answer_main_' . $answer_main_no];
                }
                $model->poll_answer = json_encode($poll_answer_arr);
            }

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

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

    /**
     * Updates an existing ContentPoll 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())) {
            $poll_answer_arr = [];
            if (!empty($_POST['answer_main_no'])) {
                foreach ($_POST['answer_main_no'] as $answer_main_no) {
                    $poll_answer_arr [] = $_POST['answer_main_' . $answer_main_no];
                }
                $model->poll_answer = json_encode($poll_answer_arr);
            }
            if ($model->save()) {
                //update translation
                if (isset($_POST['oldtranslation']) && !empty($_POST['oldtranslation'])) {
                    foreach ($_POST['oldtranslation'] as $oldtranslation) {
                        if (($modeltranslationupdate = ContentPoll::findOne($oldtranslation)) !== null) {

                            $poll_answer_arr2 = [];
                            if (!empty($_POST['translation_' . $oldtranslation . '_answer_no'])) {
                                foreach ($_POST['translation_' . $oldtranslation . '_answer_no'] as $answer_translation_no) {
                                    $poll_answer_arr2 [] = $_POST['translation_' . $oldtranslation . '_answer_' . $answer_translation_no];
                                }
                                $modeltranslationupdate->poll_answer = json_encode($poll_answer_arr2);
                            }

                            $modeltranslationupdate->poll_language = $_POST['old_language_' . $oldtranslation];
                            $modeltranslationupdate->poll_question = $_POST['old_question_' . $oldtranslation];
                            $modeltranslationupdate->updated_at = date("Y-m-d H:i:s");
                            $modeltranslationupdate->updated_by = Yii::$app->user->identity->id;
                            $modeltranslationupdate->save(false);
                        }
                    }
                }
                return $this->redirect(['index']);
            }
        }

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

    /**
     * Deletes an existing ContentPoll 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) {
        ContentPoll::deleteAll(['poll_parent_id' => $id]);
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

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

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

    public function actionAddtranslation($id) {
        $model = $this->findModel($id);
        if (!empty($model)) {
            $modeltranslation = new ContentPoll();
            $modeltranslation->poll_answer = $model->poll_answer;
            $modeltranslation->poll_parent_id = $id;
            $modeltranslation->poll_main = 0;
            $modeltranslation->created_at = date("Y-m-d H:i:s");
            $modeltranslation->created_by = Yii::$app->user->identity->id;
            $modeltranslation->updated_at = date("Y-m-d H:i:s");
            $modeltranslation->updated_by = Yii::$app->user->identity->id;
            if ($modeltranslation->save(false))
                return $this->redirect(['update', 'id' => $id]);
        }
    }

    public function actionDeletetranslation($id) {
        if (($model = ContentPoll::findOne($id)) !== null) {
            $model->delete();
        }
    }

    public function actionSorting($data = '') {
        $count = 0;
        $data_arr = explode(',', $data);
        if (!empty($data_arr)) {
            foreach ($data_arr as $slider_id) {
                $model = ContentPoll::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->poll_sorting = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }

}