/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, $condition) { $query = Station::find()->where($condition); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->params['page_size']]]); $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, 'center_id' => $this->center_id, 'area_id' => $this->area_id, 'type' => $this->type, 'user_id' => $this->user_id, 'status' => $this->status, 'picture_warning_numb' => $this->picture_warning_numb]); $query->andFilterWhere(['like', 'code', $this->code])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'firmware', $this->firmware])->andFilterWhere(['like', 'staff', $this->staff])->andFilterWhere(['like', 'addition', $this->addition])->andFilterWhere(['like', 'picture_url', $this->picture_url])->andFilterWhere(['like', 'video_url', $this->video_url])->andFilterWhere(['like', 'latitude', $this->latitude])->andFilterWhere(['like', 'longtitude', $this->longtitude])->andFilterWhere(['like', 'phone', $this->phone])->andFilterWhere(['like', 'email', $this->email]); return ['provider' => $dataProvider, 'query' => $query]; }
public function actionCreate() { $model = new PowerEquipment(); if ($model->load(Yii::$app->request->post()) && $model->save()) { //add new equipment to all exist stations $newID = Yii::$app->db->lastInsertID; $stations = Station::find()->all(); if (!empty($stations)) { foreach ($stations as $station) { var_dump($station['id']); Yii::$app->db->createCommand()->insert('power_status', ['station_id' => $station['id'], 'item_id' => $newID])->execute(); } } return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model]); } }
public function actionIndex() { $stations = Station::find()->all(); if (!empty($stations)) { foreach ($stations as $station) { $lastTime = 0; $lastStatus = StationStatus::find()->where(['station_id' => $station['id'], 'received' => StationStatus::STATUS_RECEIVED, 'status' => StationStatus::STATUS_OK])->orderBy('time_update DESC')->one(); if ($lastStatus) { $lastTime = $lastStatus['time_update']; } $currentTime = time(); if ($lastTime > 0 && $lastTime >= $currentTime - self::LIMIT_DURATION) { $status = Station::STATUS_CONNECTED; } else { $status = Station::STATUS_LOST; } if ($status != $station['status']) { Yii::$app->db->createCommand()->update('station', ['status' => $status, 'updated_at' => time()], ['id' => $station['id']])->execute(); } } } }
public function actionAjaxFilter() { if (Yii::$app->request->isAjax) { $area = Yii::$app->request->post('area'); $center = Yii::$app->request->post('center'); $query = Station::find()->select('id, name')->where([]); if ($area > 0) { $query->andWhere(['area_id' => $area]); } if ($center > 0) { $query->andWhere(['center_id' => $center]); } $stations = $query->all(); $html = ['<option value="0">Chọn trạm</option>']; if (!empty($stations)) { foreach ($stations as $s) { $html[] = '<option value="' . $s['id'] . '">' . $s['name'] . '</option>'; } } $data['html'] = implode('', $html); print json_encode($data); } }
<?php /** * Created by PhpStorm. * User: JFog * Date: 7/16/2015 * Time: 12:33 AM */ use common\components\helpers\Show; use common\components\helpers\Convert; use common\models\Area; use common\models\Center; use common\models\Station; $areaList = Area::find()->all(); $centerList = Center::find()->all(); $query = Station::find()->select('id, name')->where([]); if (isset($_GET['area']) && $_GET['area'] > 0) { $query->andWhere(['area_id' => $_GET['area']]); } if (isset($_GET['center']) && $_GET['center'] > 0) { $query->andWhere(['center_id' => $_GET['center']]); } $collections = $query->all(); $stationList[] = ['id' => '0', 'name' => 'Chọn trạm']; if (!empty($collections)) { foreach ($collections as $col) { $stationList[] = ['id' => $col['id'], 'name' => $col['name']]; } } //print '<pre>'; print_r($stationList); die; ?>
public function actionUpdateStatus() { $post = Yii::$app->request->post(); if (!empty($post)) { $data = []; $ids = $post['ids']; $idArr = explode(',', $ids); if (!empty($idArr)) { $stations = Station::find()->where(['in', 'id', $idArr])->all(); $now = time(); if (!empty($stations)) { foreach ($stations as $station) { $data[$station['id']]['status'] = $station['status']; $data[$station['id']]['since'] = $now - $station['updated_at']; } } } print json_encode($data); } }