Просмотр файла vkclone-0.0.1/protected/modules/gallery/controllers/DefaultController.php

Размер файла: 2.77Kb
<?php
class DefaultController extends FrontEndController {
	public function accessRules() {
		return CMap::mergeArray(array(
			array('allow',
				'actions' => array('index', 'recentPhotos', 'myPhotos', 'uploadPhoto', 'myAlbums', 'userPhotos'),
				'users' => array('@'),
			),
		), parent::accessRules());
	}

	public function actionIndex() {
		$this->render('index', array(
			'photos' => new CActiveDataProvider(GalleryPhoto::model(), array(
				'criteria' => array(
					'order' => 'uploaded_at DESC',
					'with' => 'user',
				),
			)),
		));
	}

	public function actionUserPhotos($user) {
		if (($user = User::model()->findByPk($user)) === null)
			throw new CHttpException(404, 'Invalid User!');
		if ($user->id == $this->webUser->id)
			$this->redirect(array('myPhotos'));

		$this->render('userPhotos', array(
			'user' => $user,
			'photos' => new CActiveDataProvider(GalleryPhoto::model(), array(
				'criteria' => array(
					'order' => 'uploaded_at DESC',
					'condition' => 'user_id = :user',
					'with' => 'user',
					'params' => array(':user' => $user->id),
				),
			)),
		));
	}

	/**
	 * Shows my photos
	 */
	public function actionMyPhotos() {
		$this->render('myPhotos', array(
			'photos' => new CActiveDataProvider(GalleryPhoto::model(), array('criteria' => array(
				'condition' => 'user_id = :user',
				'params' => array(':user' => $this->webUser->id),
				'order' => 'uploaded_at DESC',
			))),
		));
	}

	/**
	 * Shows upload form
	 */
	public function actionUploadPhoto() {
		$model = new GalleryPhoto('create');
		$model->user_id = Yii::app()->user->record->id;

		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			$model->image = CUploadedFile::getInstance($model, 'image');
			if ($model->validate() && is_object($model->image)) {
				$model->name = $model->image->name;
				if ($model->save()) {
					Yii::app()->user->setAlert(Yii::t('Gallery', 'Photo {name} added ({size})', array('{name}' => CHtml::encode($model->name), '{size}' => Yii::app()->format->formatSize($model->image->size))), 'success');
					$this->refresh();
				}
			}
		}

		$this->render('uploadPhoto', array(
			'model' => $model,
		));
	}

	/**
	 * Shows my albums
	 */
	public function actionMyAlbums() {
		$model = new GalleryAlbum('add');
		$model->user_id = Yii::app()->user->record->id;
		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			if ($model->validate() && $model->save()) {
				$this->refresh();
			}
		}

		$this->render('myAlbums', array(
			'albums' => new CActiveDataProvider(GalleryAlbum::model(), array('criteria' => array(
				'condition' => 'user_id = :user',
				'params' => array(':user' => $this->webUser->id),
				'order' => 'updated_at DESC',
			))),
			'model' => $model,
		));
	}
}