public function actionIndex()
 {
     $categories = Category::find()->where(['parent_id' => 0])->all();
     if (count($categories) == 1) {
         return $this->redirect(['/forum/category', 'id' => $categories[0]->id]);
     }
     return $this->render('index', ['categories' => $categories]);
 }
 public function actionCreate($category_id)
 {
     $category = Category::find()->where(['id' => $category_id])->one();
     if (!$category) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     $model = new Post();
     $model->category_id = $category_id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['/forum/post', 'id' => $model->id]);
     }
     return $this->render('create', ['model' => $model, 'category' => $category]);
 }
 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')])]);
 }
Exemple #4
0
 public function getCategory()
 {
     return $this->hasOne(Category::className(), ['id' => 'category_id']);
 }
Exemple #5
0
 * Created with love.
 * User: BenasPaulikas
 * Date: 2016-03-30
 * Time: 00:56
 */
/* @var benaspaulikas\forum\models\Category[] $categories */
use benaspaulikas\forum\models\Category;
use benaspaulikas\forum\models\Post;
use kartik\grid\GridView;
use yii\data\ActiveDataProvider;
use yii\helpers\Html;
use yii\helpers\StringHelper;
$this->title = Yii::t('app', 'Forum');
$this->params['breadcrumbs'][] = $this->title;
foreach ($categories as $category) {
    echo GridView::widget(['export' => false, 'beforeHeader' => [['columns' => [['content' => $category->title, 'options' => ['colspan' => 6, 'class' => 'text-center warning']]]]], 'dataProvider' => new ActiveDataProvider(['query' => Category::find()->where(['parent_id' => $category->id])]), 'summary' => false, 'beforeRow' => function ($category) {
        Yii::$app->params['category'] = Post::getPostByCategory($category)->orderBy('id DESC')->one();
    }, 'columns' => [['attribute' => 'name', 'label' => Yii::t('app', 'Forum'), 'format' => 'raw', 'value' => function ($model) {
        return Html::a($model->title, $model->url);
    }], ['label' => Yii::t('app', 'Topics'), 'value' => function ($category) {
        return Post::getPostByCategory($category)->andWhere(['parent_id' => 0])->count();
    }], ['label' => Yii::t('app', 'Posts'), 'value' => function ($category) {
        return Post::getPostByCategory($category)->andWhere(['not', ['parent_id' => 0]])->count();
    }], ['label' => Yii::t('app', 'Last Post'), 'format' => 'raw', 'value' => function () {
        $post = Yii::$app->params['category'];
        if ($post) {
            return Yii::$app->formatter->asRelativeTime(strtotime($post->date));
        } else {
            return '-';
        }
    }], ['label' => '', 'format' => 'raw', 'value' => function () {
 public function getParent()
 {
     return $this->hasOne(Category::className(), ['id' => 'parent_id']);
 }