public function beforeDelete() { if (parent::beforeDelete()) { if ($this->parent_id == 0) { Post::deleteAll(['parent_id' => $this->id]); } return true; } else { return false; } }
public function actionDelete($id) { /* @var $model Post */ $model = Post::findOne($id); if (Yii::$app->user->id == $model->user_id) { $model->delete(); if ($model->parent_id) { return $this->redirect(['/forum/post', 'id' => $model->parent_id]); } } return $this->redirect(['/forum/']); }
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')])]); }
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 () { $post = Yii::$app->params['category']; if ($post) { $parent = $post->parent_id ? $post->parent : $post; $label = Html::tag('span', Html::a(Html::encode(StringHelper::truncate($parent->subject, 40)), $parent->url, []), ['class' => 'text-highlights rounded text-highlights-green']); return $label; } return false;
/** * 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; }]]]);