Your IP : 216.73.216.79


Current Path : /var/www/html/akept/backend/modules/contentPhoto/controllers/
Upload File :
Current File : /var/www/html/akept/backend/modules/contentPhoto/controllers/ContentPhotoListController.php

<?php

namespace backend\modules\contentPhoto\controllers;

use Yii;
use backend\modules\contentPhoto\models\ContentPhotoList;
use backend\modules\contentPhoto\models\ContentPhotoListSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * ContentPhotoListController implements the CRUD actions for ContentPhotoList model.
 */
class ContentPhotoListController extends Controller {

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

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

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

    /**
     * Displays a single ContentPhotoList 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 ContentPhotoList model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate2($gallery_id) {
        $model = new ContentPhotoList();
        $model->photo_gallery_id = $gallery_id;
        if ($model->load(Yii::$app->request->post())) {
            $imagefiles = UploadedFile::getInstances($model, 'photo_url');
            $model->photo_url = (string) count($imagefiles);
            $count = 0;
            if (!is_null($imagefiles)) {
                $dirpath = dirname(getcwd());
                foreach ($imagefiles as $file) {
                    $count++;
                    $modelUpload = new ContentPhotoList();
                    $modelUpload->created_at = date("Y-m-d H:i:s");
                    $modelUpload->created_by = Yii::$app->user->identity->id;
                    $modelUpload->updated_at = date("Y-m-d H:i:s");
                    $modelUpload->updated_by = Yii::$app->user->identity->id;
                    $modelUpload->photo_main = 1;
                    $modelUpload->photo_sort = 0;
                    $modelUpload->photo_language = 'Malay';
                    $modelUpload->photo_gallery_id = $gallery_id;
                    $modelUpload->photo_descr = $model->photo_descr;

                    $photo_url = $count . '-' . date("YmdHis") . '.' . $file->extension;
                    $file->saveAs('../uploads/photo-gallery/' . $photo_url);

                    $modelUpload->photo_url = $photo_url;

                    $modelUpload->save(false);
                }
            }

            $this->getView()->registerJs("swal.fire({
                    title:'Berjaya',
                    icon: 'success', 
                    text: 'Data telah disimpan', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index', 'gallery_id' => $gallery_id]) . "';
                });");
        }
        return $this->render('create2', ['model' => $model,]);
    }

    public function actionCreate($gallery_id) {
        $model = new ContentPhotoList();
        $model->scenario = 'create';
        $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->photo_main = 1;
        $model->photo_sort = 0;
        $model->photo_language = 'Malay';
        $model->photo_gallery_id = $gallery_id;
        if ($model->load(Yii::$app->request->post())) {
            $model->photo_url = UploadedFile::getInstance($model, 'photo_url');
            if (!empty($model->photo_url)) {
                $photo_url = date("YmdHis") . '.' . $model->photo_url->extension;
                if ($model->photo_url->saveAs('../uploads/photo-gallery/' . $photo_url))
                    $model->photo_url = $photo_url;
            }

            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Berjaya',
                    icon: 'success', 
                    text: 'Data telah disimpan', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index', 'gallery_id' => $model->photo_gallery_id]) . "';
                });");
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing ContentPhotoList 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;
        $original_photo_url = $model->photo_url;
        if ($model->load(Yii::$app->request->post())) {
            $model->photo_url = UploadedFile::getInstance($model, 'photo_url');
            if (!empty($model->photo_url)) {
                $photo_url = date("YmdHis") . '.' . $model->photo_url->extension;
                if ($model->photo_url->saveAs('../uploads/photo-gallery/' . $photo_url))
                    $model->photo_url = $photo_url;
            } else {
                $model->photo_url = $original_photo_url;
            }
            if ($model->save()) {
                //update translation
                if (isset($_POST['oldtranslation']) && !empty($_POST['oldtranslation'])) {
                    foreach ($_POST['oldtranslation'] as $oldtranslation) {
                        if (($modeltranslationupdate = ContentPhotoList::findOne($oldtranslation)) !== null) {
                            $modeltranslationupdate->photo_descr = $_POST['old_descr_' . $oldtranslation];
                            $modeltranslationupdate->photo_language = $_POST['old_language_' . $oldtranslation];
                            $modeltranslationupdate->updated_at = date("Y-m-d H:i:s");
                            $modeltranslationupdate->updated_by = Yii::$app->user->identity->id;
                            $modeltranslationupdate->save(false);
                        }
                    }
                }

                $this->getView()->registerJs("swal.fire({
                    title:'Berjaya',
                    icon: 'success', 
                    text: 'Data telah disimpan', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index', 'gallery_id' => $model->photo_gallery_id]) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing ContentPhotoList 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 = $_POST['id'];
        $status = 'failed';
        ContentPhotoList::deleteAll(['photo_parent_id' => $id]);
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

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

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

    public function actionAddtranslation($id) {
        $modeltranslation = new ContentPhotoList();
        $modeltranslation->photo_parent_id = $id;
        $modeltranslation->photo_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 = ContentPhotoList::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 = ContentPhotoList::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->photo_sort = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }
}