コード例 #1
0
 public function actionIndex($id)
 {
     /* @var $model Category */
     $model = Category::find()->where(['id' => $id])->one();
     //@TODO ->andWhere(['>', 'parent_id', 0]) this is required if we have childs
     if (!$model) {
         throw new NotFoundHttpException('Category not found');
     }
     $query = Post::find()->where(['category_id' => $model->id, 'parent_id' => 0]);
     if ($model->parent) {
         $query->orWhere(['category_id' => $model->parent->id]);
     }
     return $this->render('index', ['model' => $model, 'dataProvider' => new ActiveDataProvider(['query' => $query->orderBy('id DESC')])]);
 }
コード例 #2
0
 public function actionIndex($id)
 {
     /* @var $model Post */
     $model = Post::find()->where(['id' => $id, 'parent_id' => 0])->one();
     if (!$model) {
         throw new NotFoundHttpException('The requested post does not exist.');
     }
     $model->views++;
     $model->save(false);
     $comment = new Post();
     $comment->parent_id = $model->id;
     $comment->category_id = $model->category_id;
     if ($comment->load(Yii::$app->request->post()) && $comment->save()) {
         return $this->redirect(['/forum/post', 'id' => $model->id]);
     }
     $query = Post::find()->where(['id' => $id])->orWhere(['parent_id' => $id])->orderBy('parent_id, id ASC');
     return $this->render('index', ['dataProvider' => new ActiveDataProvider(['query' => $query]), 'model' => $model, 'comment' => $comment]);
 }
コード例 #3
0
ファイル: index.php プロジェクト: benaspaulikas/yii2-forum
/**
 * Created with love.
 * User: BenasPaulikas
 * Date: 2016-03-30
 * Time: 21:51
 */
use benaspaulikas\forum\models\Post;
use kartik\grid\GridView;
use yii\helpers\Html;
/* @var $model benaspaulikas\forum\models\Category */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Forum'), 'url' => ['/forum']];
$this->params['breadcrumbs'][] = $this->title;
echo Html::a(Yii::t('app', 'Create Topic'), ['/forum/post/create', 'category_id' => $model->id], ['class' => 'btn btn-success pull-right']);
?>
    <br/><br/>
<?php 
echo GridView::widget(['export' => false, 'beforeHeader' => [['columns' => [['content' => $model->title . ($model->description ? Html::tag('p', $model->description, ['class' => 'forum-description']) : null), 'options' => ['colspan' => 5, 'class' => 'text-center warning']]]]], 'dataProvider' => $dataProvider, 'summary' => false, 'columns' => [['label' => Yii::t('app', 'Topic'), 'format' => 'raw', 'value' => function ($model) {
    $return = '';
    $return .= Html::a(Html::encode($model->subject), $model->url);
    return $return;
}], ['label' => Yii::t('app', 'Author'), 'value' => function ($model) {
    return $model->user->name;
}], ['attribute' => 'views'], ['attribute' => Yii::t('app', 'Comments'), 'value' => function ($model) {
    return Post::find()->where(['parent_id' => $model->id])->count();
}], ['label' => Yii::t('app', 'Last message'), 'format' => 'raw', 'value' => function ($model) {
    $post = Post::find()->where(['parent_id' => $model->id])->orWhere(['id' => $model->id])->orderBy('id DESC')->one();
    return $post->date . '<br/>' . $post->user->name;
}]]]);