/** * Displays a single Posts model. * @param integer $id * @return mixed */ public function actionView($id) { $searchModel = new CommentsSearch(); $comments = Comments::findAll(['PostID' => $id]); $newComment = new Comments(); $sql = "SELECT MAX(CommentID) AS CommentID FROM comments"; $max_id = Comments::findBySql($sql)->one(); $upload_id_comments = $max_id->CommentID; if ($newComment->load(Yii::$app->request->post())) { $newComment->TimeStamp = date("Y-m-d H:i:s"); $newComment->Attachment = UploadedFile::getInstance($newComment, 'Attachment'); if ($newComment->Attachment && $newComment->validate()) { $newComment->Attachment->saveAs('../uploads/comments/' . $newComment->Attachment->baseName . '_11111' . $upload_id_comments . '.' . $newComment->Attachment->extension); $newComment->Attachment = $newComment->Attachment->baseName . '_11111' . $upload_id_comments . '.' . $newComment->Attachment->extension; } if ($newComment->save()) { return $this->redirect(['view', 'id' => $newComment->PostID]); } } else { return $this->render('view', ['model' => $this->findModel($id), 'newComment' => $newComment, 'comments' => $comments]); } }
public function actionFeed() { $query = "SELECT * FROM exams ORDER BY `ExamId` DESC"; $exams = Exams::findBySql($query)->all(); $model = new NewsFeed(); $newComment = new Comments(); $sql = "SELECT * FROM posts ORDER BY `TimeStamp` DESC"; $models = NewsFeed::findBySql($sql)->all(); $sql2 = "SELECT *, MAX(Likes) FROM posts"; $popular = NewsFeed::findBySql($sql2)->one(); $comments_popular = $popular->comments; $sql3 = "SELECT * FROM posts WHERE Pinned = 1"; $pinned = NewsFeed::findBySql($sql3)->one(); $comments_pinned = $pinned->comments; $sort = new Sort(['attributes' => ['TimeStamp' => ['asc' => ['TimeStamp' => SORT_ASC], 'desc' => ['TimeStamp' => SORT_DESC], 'default' => SORT_DESC, 'label' => 'Latest']]]); $sql4 = "SELECT MAX(PostID) AS PostID FROM posts"; $max_id = NewsFeed::findBySql($sql4)->one(); $upload_id_posts = $max_id->PostID; $sql5 = "SELECT MAX(CommentID) AS CommentID FROM comments"; $max_id2 = Comments::findBySql($sql5)->one(); $upload_id_comments = $max_id2->CommentID; $success = false; if ($model->load(Yii::$app->request->post())) { //$model->PostContent = "Sample na naman"; $model->TimeStamp = date("Y-m-d H:i:s"); $post_title = substr($model->PostContent, 0, 15); $model->PostTitle = $post_title . "..."; $user = Users::find()->where(['UserID' => $model->UserID])->one(); $model->Attachment = UploadedFile::getInstance($model, 'Attachment'); if ($model->Attachment && $model->validate()) { $model->Attachment->saveAs('../uploads/posts/' . $model->Attachment->baseName . '_00000' . $upload_id_posts . '.' . $model->Attachment->extension); $model->Attachment = $model->Attachment->baseName . '_00000' . $upload_id_posts . '.' . $model->Attachment->extension; } if ($model->save()) { $success = true; } } if ($newComment->load(Yii::$app->request->post())) { $newComment->TimeStamp = date("Y-m-d H:i:s"); $newComment->Attachment = UploadedFile::getInstance($newComment, 'Attachment'); if ($newComment->Attachment && $newComment->validate()) { $newComment->Attachment->saveAs('../uploads/comments/' . $newComment->Attachment->baseName . '_11111' . $upload_id_comments . '.' . $newComment->Attachment->extension); $newComment->Attachment = $newComment->Attachment->baseName . '_11111' . $upload_id_comments . '.' . $newComment->Attachment->extension; } if ($newComment->save()) { $success = true; } } //Yii::info($comments, __METHOD__); if ($success) { return $this->refresh(); } else { return $this->render('feed', ['model' => $model, 'models' => $models, 'newComment' => $newComment, 'popular' => $popular, 'comments_popular' => $comments_popular, 'pinned' => $pinned, 'comments_pinned' => $comments_pinned, 'sort' => $sort, 'exams' => $exams]); } }