1. Bogdan Savluk
  2. galleryManager

Commits

Bogdan Savluk  committed 46dbff0

Cosmetic fixes in GalleryController.

  • Participants
  • Parent commits 1784521
  • Branches default

Comments (0)

Files changed (1)

File GalleryController.php

View file
 
 class GalleryController extends CController
 {
+    public function filters()
+    {
+        return array(
+            'postOnly + delete, ajaxUpload, order, changeData',
+        );
+    }
+
     /**
      * 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';
+        $id = $_POST['id'];
+        /** @var $photo GalleryPhoto */
+        $photo = GalleryPhoto::model()->findByPk($id);
+        if ($photo !== null && $photo->delete()) echo 'OK';
+        else throw new CHttpException(400, 'Photo, not found');
     }
 
     /**
      */
     public function actionAjaxUpload($gallery_id = null)
     {
-        if (Yii::app()->getRequest()->getIsPostRequest()) {
+        $model = new GalleryPhoto();
+        $model->gallery_id = $gallery_id;
+        if (isset($_POST['GalleryPhoto']))
+            $model->attributes = $_POST['GalleryPhoto'];
+        $imageFile = CUploadedFile::getInstance($model, 'image');
+        $model->file_name = $imageFile->getName();
+        $model->save();
 
-            $model = new GalleryPhoto();
-            $model->gallery_id = $gallery_id;
-            if (isset($_POST['GalleryPhoto']))
-                $model->attributes = $_POST['GalleryPhoto'];
-
-            $imageFile = CUploadedFile::getInstance($model, 'image');
-            $model->file_name = $imageFile->getName();
-            $model->save();
-
-            $model->setImage($imageFile->getTempName());
-            header("Content-Type: application/json");
-            echo CJSON::encode(
-                array(
-                    'id' => $model->id,
-                    'rank' => $model->rank,
-                    'name' => (string)$model->name, //todo: something wrong with model - it returns null, but it must return an empty string
-                    'description' => (string)$model->description,
-                    'preview' => $model->getPreview(),
-                ));
-        } else throw new CHttpException(403);
+        $model->setImage($imageFile->getTempName());
+        header("Content-Type: application/json");
+        echo CJSON::encode(
+            array(
+                'id' => $model->id,
+                'rank' => $model->rank,
+                'name' => (string)$model->name,
+                'description' => (string)$model->description,
+                'preview' => $model->getPreview(),
+            ));
     }
 
     /**
      */
     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);
+        if (!isset($_POST['order'])) throw new CHttpException(400, 'No data, to save');
+        $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']);
+        }
     }
 
     /**
      */
     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) {
-                if (isset($attributes['name']))
-                    $models[$id]->name = $attributes['name'];
-                if (isset($attributes['description']))
-                    $models[$id]->description = $attributes['description'];
-                $models[$id]->save();
-            }
-            $resp = array();
-            foreach ($models as $model) {
-                $resp[] = array(
-                    'id' => $model->id,
-                    'rank' => $model->rank,
-                    'name' => (string)$model->name,
-                    'description' => (string)$model->description,
-                    'preview' => $model->getPreview(),
-                );
-            }
-            echo CJSON::encode($resp);
-        } else
-            throw new CHttpException(403);
+        if (!isset($_POST['photo'])) throw new CHttpException(400, 'Nothing, to save');
+        $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) {
+            if (isset($attributes['name']))
+                $models[$id]->name = $attributes['name'];
+            if (isset($attributes['description']))
+                $models[$id]->description = $attributes['description'];
+            $models[$id]->save();
+        }
+        $resp = array();
+        foreach ($models as $model) {
+            $resp[] = array(
+                'id' => $model->id,
+                'rank' => $model->rank,
+                'name' => (string)$model->name,
+                'description' => (string)$model->description,
+                'preview' => $model->getPreview(),
+            );
+        }
+        echo CJSON::encode($resp);
     }
 }