Example #1
0
 public function actionView($id, $slug, $parentSectionId, $parentSectionSlug, $childSectionId, $childSectionSlug)
 {
     /** @var Post $post */
     $post = Post::find()->where(['id' => $id, 'slug' => $slug])->with(['section', 'section.parent'])->one();
     if ($post === null || $post->section->parent->id != $parentSectionId || $post->section->parent->slug != $parentSectionSlug || $post->section->id != $childSectionId || $post->section->slug != $childSectionSlug) {
         throw new HttpException(404, 'Cannot find the requested post.');
     }
     $comment = Comment::create(['post_id' => $post->id]);
     if (Yii::$app->getRequest()->getIsAjax() && $comment->load(Yii::$app->getRequest()->post())) {
         Yii::$app->getResponse()->format = Response::FORMAT_JSON;
         return ActiveForm::validate($comment);
     }
     if ($comment->load(Yii::$app->getRequest()->post()) && $comment->save()) {
         return $this->refresh();
     }
     $comments = Comment::findByPost($post->id);
     return $this->render('view', ['post' => $post, 'comment' => $comment, 'comments' => $comments]);
 }
Example #2
0
 public function beforeSave($insert)
 {
     if (!parent::beforeSave($insert)) {
         return false;
     }
     $query = new Query();
     $query->select('COUNT(*)')->where(['post_id' => $this->post_id])->from(static::tableName());
     $count = $query->scalar(static::getDb());
     if ($count == 0) {
         $this->left = 1;
         $this->right = 2;
         $this->level = 1;
     } else {
         if (empty($this->parent_id)) {
             $query = new Query();
             $query->select('MAX({{right}})')->where(['post_id' => $this->post_id])->from(static::tableName());
             $maxRight = $query->scalar(static::getDb());
             $this->left = $maxRight + 1;
             $this->right = $maxRight + 2;
             $this->level = 1;
         } else {
             /*$query = new Query();
                             $query
                                 ->select(['{{left}} left', '{{level}} level'])
                                 ->where(['post_id' => $this->post_id, 'id' => $this->parent_id])
                                 ->from(static::tableName());
                             $data1 = $query->all(static::getDb());
             
                             static::getDb()
                                 ->createCommand('UPDATE ' . static::tableName() . ' SET {{right}} = {{right}} + 2 WHERE {{right}} > :left')
                                 ->bindValue(':left', $data1[0]['left'])
                                 ->execute();
             
                             static::getDb()
                                 ->createCommand('UPDATE ' . static::tableName() . ' SET {{left}} = {{left}} + 2 WHERE {{left}} > :left')
                                 ->bindValue(':left', $data1[0]['left'])
                                 ->execute();
             
                             $this->left = $data1[0]['left'] + 1;
                             $this->right = $data1[0]['left'] + 2;
                             $this->level = $data1[0]['level'] + 1;*/
             $query = new Query();
             $query->select(['id', '{{left}} left', '{{right}} right', '{{level}} level'])->where(['post_id' => $this->post_id, 'id' => $this->parent_id])->from(static::tableName());
             $data1 = $query->all(static::getDb());
             if ($data1[0]['left'] != $data1[0]['right'] - 1) {
                 $query = new Query();
                 $query->select(['id', '{{left}} left', '{{right}} right', '{{level}} level', '{{text}}'])->where(['post_id' => $this->post_id, 'level' => $data1[0]['level']])->andWhere('{{left}} > :r AND {{id}} != :id', [':r' => $data1[0]['right'], ':id' => $data1[0]['id']])->from(static::tableName());
                 $data2 = $query->one(static::getDb());
                 static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{right}} = {{right}} + 2 WHERE {{right}} > :left')->bindValue(':left', $data2['left'] - 2)->execute();
                 static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{left}} = {{left}} + 2 WHERE {{left}} > :left')->bindValue(':left', $data2['left'] - 2)->execute();
                 $this->left = $data2['right'] - 2;
                 $this->right = $data2['right'] - 1;
                 $this->level = $data1[0]['level'] + 1;
             } else {
                 static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{right}} = {{right}} + 2 WHERE {{right}} > :left')->bindValue(':left', $data1[0]['left'])->execute();
                 static::getDb()->createCommand('UPDATE ' . static::tableName() . ' SET {{left}} = {{left}} + 2 WHERE {{left}} > :left')->bindValue(':left', $data1[0]['left'])->execute();
                 $this->left = $data1[0]['left'] + 1;
                 $this->right = $data1[0]['left'] + 2;
                 $this->level = $data1[0]['level'] + 1;
             }
         }
     }
     return true;
 }