Your IP : 216.73.217.143


Current Path : /var/www/html/project/controllers/
Upload File :
Current File : /var/www/html/project/controllers/PicController.php

<?php

namespace app\controllers;

use Yii;
use yii\helpers\Html;
use app\models\Pic;
use app\models\PicSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\User;
use app\models\ParamList;
use yii\helpers\ArrayHelper;

/**
 * PicController implements the CRUD actions for Pic model.
 */
class PicController extends Controller {

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

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

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

    /**
     * Displays a single Pic 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 Pic model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Pic();

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

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

    /**
     * Updates an existing Pic 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);

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

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

    /**
     * Deletes an existing Pic 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 Pic model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Pic the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Pic::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionAddpic() {
        $ParamList = new ParamList();
        $countpic = $_POST['countpic'];
        return '<div class="row">
                <div class="col-5">
                    <input type="hidden" value="' . $countpic . '" name="new_pic_id[]" class="picno">
                    ' . Html::dropDownList('new_pic_role_' . $countpic, '', $ParamList->getparamlist2(2), [
                    'class' => 'mb-2 form-control kt-selectpicker',
                    'id' => 'new_pic_role_' . $countpic,
                    'prompt' => '- Choose role -',
                    'onchange' => 'changerole(this.value,' . $countpic . ')',
                    'data-live-search' => 'true'
                ]) . '
                </div>
                <div class="col-7">' . Html::dropDownList('new_pic_user_' . $countpic, '', ArrayHelper::map(User::find()->where(['status' => '1'])->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), [
                    'class' => 'form-control kt-selectpicker',
                    'id' => 'new_pic_user_' . $countpic,
                    'prompt' => '- Choose staff -',
                    'onchange' => 'changeassignto($("#new_pic_role_"+' . $countpic . ').val(),this.value)',
                    'data-live-search' => 'true'
                ]) . '
            </div>
        </div>';
    }

    public function actionChangerole() {
        $output = '';
        $model = User::find()->where(['status' => '1'])->andwhere(['role' => $_POST['role']])->orderBy(['name' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $output .= '<option value="' . $row->id . '">' . $row->name . '</option>';
            }
        }
        return $output;
    }

}