public function searchRecentAnnouncement($params, $size)
 {
     $query = Announcement::find()->orderBy(['created_at' => SORT_DESC]);
     $dataProvider->sort->attributes['id'] = ['asc' => ['id' => SORT_ASC], 'desc' => ['id' => SORT_DESC]];
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => $size]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     return $dataProvider;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Announcement::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'datetime' => $this->datetime, 'Administrator_id' => $this->Administrator_id]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content]);
     return $dataProvider;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Announcement::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $query->joinWith('userFk AS userFk');
     $dataProvider->sort->attributes['userFk.username'] = ['asc' => ['userFk.username' => SORT_ASC], 'desc' => ['userFk.username' => SORT_DESC]];
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['announcement_id' => $this->announcement_id, 'user_fk' => $this->user_fk, 'announcement_date' => $this->announcement_date]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'announcement', $this->announcement]);
     $query->andFilterWhere(['like', 'userFk.username', $this->getAttribute('userFk.username')]);
     return $dataProvider;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Announcement::find();
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $query->asArray()->all();
     }
     $query->orFilterWhere(['like', 'title', $this->globalSearch])->orFilterWhere(['like', 'content', $this->globalSearch]);
     $raw = $query->asArray()->all();
     $returnArr = [];
     foreach ($raw as $row) {
         $detail = substr($row['content'], 0, 200) . '...';
         $arrayRow = ['type' => 'announcement', 'id' => $row['id'], 'title' => $row['title'], 'detail' => $detail];
         array_push($returnArr, $arrayRow);
     }
     return $returnArr;
 }
 public function lastAnnouncement()
 {
     $announcement = Announcement::find()->orderBy(['id' => SORT_DESC])->one();
     return $announcement;
 }
 public function countAnnouncement()
 {
     $count = count(Announcement::find()->all());
     return $count;
 }
 public static function getNewestAnnounsment()
 {
     $lastAnnounsment = Announcement::find()->orderBy('announcement_date DESC')->one();
     return $lastAnnounsment;
 }
 public function destroy(Request $request, $id)
 {
     $v = Announcement::find($id);
     if ($v) {
         // retrieve image detail prior to deletion
         $images = DB::table('files')->join('announcement_files', 'files.id', '=', 'announcement_files.file_id')->where('announcement_files.announcement_id', $id)->get();
         $res = Announcement::where("id", $id)->where('user_id', Auth::user()->id)->delete();
         if ($res) {
             // delete also the images from db and local storage
             if (count($images) > 0) {
                 foreach ($images as $image) {
                     try {
                         $imgFile = File::find($image->file_id);
                         if ($imgFile) {
                             $imgFile->delete();
                             \Illuminate\Support\Facades\File::delete(MyHelper::getImageFromStorage($id, $imgFile->new_filename));
                         }
                     } catch (\Exception $e) {
                         Log::info($e->getMessage());
                     }
                 }
             }
             $request->session()->flash("notif", "Announcement successfully deleted");
             return ['error' => false];
         } else {
             return ['error' => true, 'message' => 'Failed to delete announcement!'];
         }
     } else {
         $request->session()->flash("notif", "announcement not available!");
         return ['error' => true];
     }
 }