<?php
class DefaultController extends FrontEndController {
public function accessRules() {
return CMap::mergeArray(array(
array('allow',
'actions' => array('index', 'recentGroups', 'myGroups', 'userGroups', 'participationGroups'),
'users' => array('@'),
),
), parent::accessRules());
}
/**
* Shows the most popular groups
*/
public function actionIndex() {
$model = new Group('search');
$model->unsetAttributes();
if (isset($_GET[CHtml::modelName(Group::model())]))
$model->attributes = $_GET[CHtml::modelName(Group::model())];
$this->render('index', array(
'model' => $model,
'groups' => new CActiveDataProvider(Group::model(), array(
'criteria' => array(
'order' => 'total_members DESC'
),
)),
));
}
/**
* Shows recently created groups
*/
public function actionRecentGroups() {
$this->render('recentGroups', array(
'groups' => new CActiveDataProvider(Group::model(), array('criteria' => array(
'order' => 'created_at DESC',
))),
));
}
/**
* Shows groups I created
*/
public function actionMyGroups() {
$model = new Group('create');
$model->creator_id = Yii::app()->user->record->id;
if (isset($_POST[CHtml::modelName($model)])) {
$model->attributes = $_POST[CHtml::modelName($model)];
if ($model->validate() && $model->create()) {
Yii::app()->user->setAlert(Yii::t('Groups', 'Group created'), 'success');
$this->refresh();
}
}
$this->render('myGroups', array(
'model' => $model,
'groups' => new CActiveDataProvider(Group::model(), array('criteria' => array(
'condition' => 'creator_id = :creator_id',
'params' => array(':creator_id' => $this->webUser->id),
'order' => 'total_members',
))),
));
}
public function actionUserGroups($user) {
if (($user = User::model()->findByPk($user)) === null)
throw new CHttpException(404, 'Invalid User!');
if ($user->id == $this->webUser->id)
$this->redirect(array('myGroups'));
$this->render('userGroups', array(
'user' => $user,
'groups' => new CActiveDataProvider(Group::model(), array('criteria' => array(
'condition' => 'creator_id = :creator_id',
'params' => array(':creator_id' => $user->id),
'order' => 'total_members',
))),
));
}
/**
* Shows groups I participated
*/
public function actionParticipationGroups() {
$this->render('participationGroups', array(
'groups' => new CActiveDataProvider(Group::model(), array('criteria' => array(
'condition' => '(SELECT COUNT(*) FROM '.GroupMember::model()->tableName().' WHERE `group_id` = `t`.`id` AND `user_id` = :user_id) > 0',
'params' => array(':user_id' => Yii::app()->user->record->id),
'order' => 't.total_members',
))),
));
}
}