i18d-galleryManager / GalleryController.php

<?php
/**
 * Backend controller for GalleryManager widget.
 * Provides following features:
 *  - Image removal
 *  - Image upload/Multiple upload
 *  - Arrange images in gallery
 *  - Changing name/description associated with image
 *
 * @author Bogdan Savluk <savluk.bogdan@gmail.com>
 */

class GalleryController extends CController
{
    /**
     * Removes image with id specified in post request.
     * On success returns 'OK'
     */
    public function actionDelete()
    {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $id = $_POST['id'];
            /** @var $photo GalleryPhoto */
            $photo = GalleryPhoto::model()->findByPk($id);
            if ($photo !== null && $photo->delete()) echo 'OK';
            else echo 'FAIL';
        } else echo 'FAIL';
    }

    /**
     * Method to handle file upload thought XHR2
     * On success returns JSON object with image info.
     * @param $gallery_id string Gallery Id to upload images
     * @throws CHttpException
     */
    public function actionAjaxUpload($gallery_id = null)
    {
        if (Yii::app()->getRequest()->getIsPostRequest()) {

            $model = new GalleryPhoto();
            $model->gallery_id = $gallery_id;
            $i18nModels = array();
            if (isset($_POST['GalleryPhoto']))
                $model->attributes = $_POST['GalleryPhoto'];
            foreach (Yii::app()->params['languages'] as $lang) {
                $i18nModel = new GalleryPhotoI18n();
                $i18nModel->lang = $lang;
                if (isset($_POST['GalleryPhotoI18n']) && isset($_POST['GalleryPhotoI18n'][$lang]))
                    $model->attributes = $_POST['GalleryPhotoI18n'][$lang];
                $i18nModels[$lang] = $i18nModel;
            }
            $imageFile = CUploadedFile::getInstance($model, 'image');
            $model->file_name = $imageFile->getName();
            $model->save();

            foreach (Yii::app()->params['languages'] as $lang) {
                $i18nModels[$lang]->id = $model->id;
                $i18nModels[$lang]->save();
            }

            $model->setImage($imageFile->getTempName());
            header("Content-Type: application/json");

            $i18ns = array();
            foreach (Yii::app()->params['languages'] as $lang) {
                $i18ns[$lang] = array(
                    'name' => (string)$i18nModels[$lang]->name,
                    'description' => (string)$i18nModels[$lang]->description,
                    'link' => (string)$i18nModels[$lang]->link,
                );
            }
            echo CJSON::encode(
                array(
                    'id' => $model->id,
                    'rank' => $model->rank,
                    'i18ns' => $i18ns,
                    'preview' => $model->getPreview(),
                ));
        } else throw new CHttpException(403);
    }

    /**
     * Saves images order according to request.
     * Variable $_POST['order'] - new arrange of image ids, to be saved
     * @throws CHttpException
     */
    public function actionOrder()
    {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $gp = $_POST['order'];
            $orders = array();
            $i = 0;
            foreach ($gp as $k => $v) {
                if (!$v) $gp[$k] = $k;
                $orders[] = $gp[$k];
                $i++;
            }
            sort($orders);
            $i = 0;
            foreach ($gp as $k => $v) {
                /** @var $p GalleryPhoto */
                $p = GalleryPhoto::model()->findByPk($k);
                $p->rank = $orders[$i];
                $p->save(false);
                $i++;
            }
            if ($_POST['ajax'] == true) {
                echo CJSON::encode(array('result' => 'ok'));
            } else {
                $this->redirect($_POST['returnUrl']);
            }
        } else
            throw new CHttpException(403);
    }

    /**
     * Method to update images name/description via AJAX.
     * On success returns JSON array od objects with new image info.
     * @throws CHttpException
     */
    public function actionChangeData()
    {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $data = $_POST['photo'];
            $criteria = new CDbCriteria();
            $criteria->index = 'id';
            $criteria->addInCondition('id', array_keys($data));
            /** @var $models GalleryPhoto[] */
            $models = GalleryPhoto::model()->findAll($criteria);
            foreach ($data as $id => $attributes) {
                $i18nModels = $models[$id]->i18ns;
                foreach (Yii::app()->params['languages'] as $lang) {
                    if (isset($attributes[$lang])) {
                        if (isset($attributes[$lang]['name']))
                            $i18nModels[$lang]->name = $attributes[$lang]['name'];
                        if (isset($attributes[$lang]['description']))
                            $i18nModels[$lang]->description = $attributes[$lang]['description'];
                        if (isset($attributes[$lang]['link']))
                            $i18nModels[$lang]->link = $attributes[$lang]['link'];
                        $i18nModels[$lang]->save();
                    }
                }
            }
            $resp = array();
            foreach ($models as $model) {
                $i18ns = array();
                foreach (Yii::app()->params['languages'] as $lang) {
                    $i18ns[$lang] = array(
                        'name' => (string)$model->i18ns[$lang]->name,
                        'description' => (string)$model->i18ns[$lang]->description,
                        'link' => (string)$model->i18ns[$lang]->link,
                    );
                }
                $resp[] = array(
                    'id' => $model->id,
                    'rank' => $model->rank,
                    'i18ns' => $i18ns,
                    'preview' => $model->getPreview(),
                );
            }
            echo CJSON::encode($resp);
        } else
            throw new CHttpException(403);
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.