Your IP : 216.73.216.79


Current Path : /var/www/html/chatbot/backend/controllers/content/
Upload File :
Current File : /var/www/html/chatbot/backend/controllers/content/ContentImageSliderController.php

<?php

namespace backend\controllers\content;

use Yii;
use backend\models\ContentImageSlider;
use backend\models\ContentImageSliderSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use common\components\AccessRule;

/**
 * ContentImageSliderController implements the CRUD actions for ContentImageSlider model.
 */
class ContentImageSliderController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

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

        if ($model->load(Yii::$app->request->post())) {
            $model->content_image_slider_image_url = UploadedFile::getInstance($model, 'content_image_slider_image_url');
            $model->content_image_slider_image_url_en = UploadedFile::getInstance($model, 'content_image_slider_image_url_en');
            if ($model->upload()) {
                // file is uploaded successfully
                 if ($model->save(false)) {
                     return $this->redirect(['index']);
                }
            }
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing ContentImageSlider 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->scenario = 'update';

        $model->uppath = $model->content_image_slider_image_url;
        $model->uppathen = $model->content_image_slider_image_url_en;
        
        if ($model->load(Yii::$app->request->post())) {
            
            $model->content_image_slider_image_url = UploadedFile::getInstance($model, 'content_image_slider_image_url');
            $model->content_image_slider_image_url_en = UploadedFile::getInstance($model, 'content_image_slider_image_url_en');

            if ($model->upload()) {
                // file is uploaded successfully
                if ($model->save(false)) {
                    return $this->redirect(['index']);
                }
            }
        }

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

    /**
     * Deletes an existing ContentImageSlider 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)
    {   
        $model = $this->findModel($id);

        $url = $model->content_image_slider_image_url;
        $url = Yii::getAlias('@webroot') . "/uploads/" . $url;
        
        if(file_exists($url)){
            unlink($url);
        }
        $model->delete();
        return $this->redirect(['index']);
    }

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

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