<?php

class SliderImagesController extends RController
{

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'rights',
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionQuickUpdate()
    {
        $es = new EditableSaver('SliderImages');
        $es->update();
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate($slider_id)
    {
        $slider = Slider::model()->findByPk($slider_id);
        if ($slider === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        $model = new SliderImages;
        $model->slider_id = $slider_id;
        if (isset($_FILES['images']) && isset($_POST['SliderImages'])) {
            $model->attributes = $_POST['SliderImages'];
            $model->type = 'image';
            $images = CUploadedFile::getInstancesByName('images');
            // proceed if the images have been set
            if (isset($images) && count($images) > 0) {
                $ok = true;
                // go through each uploaded image
                foreach ($images as $image => $pic) {
                    $extension = $pic->getExtensionName();
                    if (!in_array($extension, ['png', 'jpg', 'jpeg', 'gif', 'swf', 'flv'])) {
                        continue;
                    }
                    $picname = uniqid() . '.' . $extension;
                    $path = Yii::app()->params['uploadDir'] . get_class($model) . '/' . $picname;
                    if ($pic->saveAs($path)) {
                        // add it to the main model now
                        $image = new SliderImages();
                        $image->image = $picname; //it might be $image->name for you, filename is just what I chose to call it in my model
                        $image->slider_id = $slider_id; // this links your picture model to the main model (like your user, or profile model)
                        if ($image->save()) {
                            $ok = $ok && true;
                        } else {
                            echo 'ERROR!';
                            echo '<pre dir="ltr">', var_dump($image->getErrors()), '</pre>';
                        }
                    } else {
                        echo 'Cannot upload!';
                    }
                }
                if ($ok) {
                    $this->redirect(array('slider/view', 'id' => $slider_id));
                }
            }
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }

    ////////////////////////////////////////////////addVideo////////////////////////////////
    public function actionAddVideo($slider_id)
    {
        $slider = Slider::model()->findByPk($slider_id);
        if ($slider === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        $model = new SliderImages;
        $model->slider_id = $slider_id;

        if (isset($_POST['SliderImages'])) {
            $model->attributes = $_POST['SliderImages'];
            $model->type = 'video';
            $model->slider_id = $slider_id;
            if ($model->save()) {

                $this->redirect(array('slider/view', 'id' => $slider_id));
            }
        }
        $this->render('addVideo', array(
            'model' => $model,
        ));
    }
    ///////////////////////////////uploadFlash////////////////////////

    public function actionUploadFlash($slider_id)
    {
        $slider = Slider::model()->findByPk($slider_id);
        if ($slider === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        $model = new SliderImages;

        // $model->type='flash';
        $model->slider_id = $slider_id;
        if (isset($_POST['SliderImages'])) {
            $model->attributes = $_POST['SliderImages'];
            // $model->type='flash';

            $images = CUploadedFile::getInstancesByName('images');
            // proceed if the images have been set
            if (isset($images) && count($images) > 0) {
                $ok = true;
                // go through each uploaded image
                foreach ($images as $image => $pic) {
                    $picname = uniqid() . '.' . $pic->getExtensionName();
                    $path = Yii::app()->params['uploadDir'] . get_class($model) . '/' . $picname;
                    if ($pic->saveAs($path)) {
                        // add it to the main model now
                        $image = new SliderImages();
                        $image->image = $picname; //it might be $image->name for you, filename is just what I chose to call it in my model
                        $image->type = 'flash';
                        $image->slider_id = $slider_id; // this links your picture model to the main model (like your user, or profile model)

                        if ($image->save()) {
                            $ok = $ok && true;
                        } else {
                            echo 'ERROR!';
                            echo '<pre dir="ltr">', var_dump($image->getErrors()), '</pre>';
                        }
                    } else {
                        echo 'Cannot upload!';
                    }
                }
                if ($ok) {
                    $this->redirect(array('slider/view', 'id' => $slider_id));
                }
            }
        }
        $this->render('uploadFlash', array(
            'model' => $model,
        ));
    }
    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id, $slider_id)
    {
        $model = $this->loadModel($id);
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if (isset($_POST['SliderImages'])) {
            $model->attributes = $_POST['SliderImages'];
            if ($model->save())
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('slider/view', 'id' => $slider_id));
        }
        $this->render('update', array(
            'model' => $model,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {

        $id = $id * 1;
        if ($id) {
            $model = $this->loadModel($id);
            $slider_id = $model->slider_id;
            $path = Yii::app()->params['uploadDir'] . get_class($model) . '/' . $model->image;
            if (is_file($path)) {
                unlink($path);
            }
            $model->delete();
            if (!isset($_GET['ajax'])) {
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('slider/view', 'id' => $slider_id));
            }
        }
    }

    /**
     * Manages all models.
     */
    public function actionAdmin($slider_id)
    {
        $model = new SliderImages('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['SliderImages'])) {
            $model->attributes = $_GET['SliderImages'];
        }
        $model->slider_id = $slider_id;
        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return SliderImages the loaded model
     * @throws CHttpException
     */
    public function loadModel($id)
    {
        $model = SliderImages::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param SliderImages $model the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'slider-images-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
