/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Warning::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'warning_type' => $this->warning_type, 'station_id' => $this->station_id, 'warning_time' => $this->warning_time, 'read' => $this->read]); $query->andFilterWhere(['like', 'message', $this->message]); return $dataProvider; }
public function hasUnreadWarning($id) { if ($id > 0) { $newest = Warning::find()->where(['station_id' => $id, 'read' => Warning::STATUS_UNREAD])->one(); if ($newest) { return $newest; } } return false; }
private function buildQuery($export = false) { $parseData = []; if ($export) { $query = new Query(); $query->select(['warning.message AS message', 'station.name AS station_name', 'DATE_FORMAT(FROM_UNIXTIME(warning.warning_time), "%d/%m/%Y %H:%i:%s") AS warning_date'])->from('warning')->leftJoin('station', 'station.id = warning.station_id')->where([]); } else { $query = Warning::find()->leftJoin('station', 'station.id = warning.station_id')->where([]); } // permission $stationIds = []; $role = new Role(); if (!$role->isAdministrator) { $position = $role->getPosition(); $stationIds = Station::getByRole($position, Yii::$app->user->id); } //filter by stations $stations = Yii::$app->request->get('station'); if (!empty($stations)) { $stationIds = array_merge($stationIds, $stations); } if (!empty($stationIds)) { foreach ($stationIds as $k => $id) { if (intval($id) <= 0) { unset($stationIds[$k]); } } } if (!empty($stationIds)) { $condition = ['in', 'station_id', $stationIds]; $query->where($condition); } // filter by area $areaId = Yii::$app->request->get('area'); if ($areaId > 0) { $query->andWhere(['station.area_id' => $areaId]); } // filter by center $center = Yii::$app->request->get('center'); if ($center > 0) { $query->andWhere(['station.center_id' => $center]); } // filter by time points $getBy = Yii::$app->request->get('get_by'); if ($getBy) { if ($getBy == 'today') { $timePoints = Convert::currentTimePoints(); } else { if ($getBy == 'week') { $timePoints = Convert::currentWeekTimePoints(); } else { if ($getBy == 'month') { $timePoints = Convert::currentMonthTimePoints(); } } } $query->andWhere(['>=', 'warning.warning_time', $timePoints['start']]); $query->andWhere(['<=', 'warning.warning_time', $timePoints['end']]); } // filter by time duration $fromDate = Yii::$app->request->get('from_date'); if ($fromDate) { $fromTime = Convert::date2Time($fromDate, 'd/m/Y'); $query->andWhere(['>=', 'warning_time', $fromTime]); } $toDate = Yii::$app->request->get('to_date'); if ($toDate) { $toTime = Convert::date2Time($toDate, 'd/m/Y', 'end'); $query->andWhere(['<=', 'warning_time', $toTime]); } $query->orderBy('warning_time DESC'); return ['query' => $query, 'parseData' => $parseData]; }