Your IP : 216.73.216.79


Current Path : /var/www/html/learn/backend/controllers/content/
Upload File :
Current File : /var/www/html/learn/backend/controllers/content/ContentPhotoController.php

<?php

namespace backend\controllers\content;

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


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

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

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

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

        if ($model->load(Yii::$app->request->post())) {
            $model->uploadImage = UploadedFile::getInstances($model, 'uploadImage');
            $model->thumbup = UploadedFile::getInstance($model, 'thumbup');

            // echo '<pre>'; var_dump($_POST); echo '</pre>';die();
            if ($model->save(false)) {
                if ($model->upload($model->content_photo_id)) {
                    // file is uploaded successfully
                    $updatePath = $model->findOne($model->content_photo_id);
                    $updatePath->content_photo_file_url = implode('#', $model->uploadArr);
                    $updatePath->content_photo_thumb_image = $model->thumbpath;
                    $updatePath->save(); 

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

    /**
     * Updates an existing ContentPhoto 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())) {
            $image = $model->content_photo_file_url;
            
            $model->uploadImage = UploadedFile::getInstances($model, 'uploadImage');
            $model->thumbup = UploadedFile::getInstance($model, 'thumbup');
                 
            if ($model->upload($id)) {

                if(!empty($model->uploadArr)){
                    $model->content_photo_file_url = implode('#', $model->uploadArr);
                }
                
                if(!empty($model->thumbpath)){
                    $model->content_photo_thumb_image = $model->thumbpath;
                }
                
                if ($model->save(false)) {
                    return $this->redirect(['index']);
                }
            }
        }


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

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

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

    public function actionArchive($id){
        $model  = $this->findModel($id);
        if($model->content_photo_file_status == 1){
            $model->content_photo_file_status = 0;
        }else{
            $model->content_photo_file_status = 1;
        }
        $model->save();
        return $this->redirect(['index']);
    }
}