Exemplo n.º 1
0
 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]);
     }
 }
Exemplo n.º 2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     //$query = NewsFeed::find()->orderBy('PostID')->joinWith(['users'])->all();
     $sql = "SELECT * FROM posts ORDER BY `TimeStamp` ASC";
     $query = NewsFeed::findBySql($sql)->all();
     $dataProvider = new ArrayDataProvider(['allModels' => $query]);
     if (!($this->load($params) && $this->validate())) {
         //$query->joinWith(['users']);
         return $dataProvider;
     }
     return $dataProvider;
 }