public function actionExportcsv() { $post = Yii::$app->request->post(); $get = Yii::$app->request->get(); if (Yii::$app->request->isAjax) { if (isset($post['alertId'])) { $alertData = $this->getAlertIdInformation($post['alertId']); } $filename = date('Ymd_His') . '_' . $alertData->id . '.csv'; $output = fopen(Yii::$app->basePath . '/web/' . $this->tmpPath . '/' . $filename, 'w'); $headerArray = array('AlertID', 'AlertDate', 'AlertMagnitude', 'AlertSeverity', 'Event', 'PropertyID', 'PropertyStreetAddress', 'PropertyCity', 'PropertyState', 'PropertyZipcode', 'PropertyClient', 'PropertyLatitude', 'PropertyLongitude', 'PropertyStatus'); fputcsv($output, $headerArray); foreach ($this->_affectedProperties as $property) { $csvLine = []; $csvLine[] = $alertData->id; $csvLine[] = date('M d, Y H:i', $alertData->date); $csvLine[] = $alertData->magnitude; $csvLine[] = $alertData->severity; $csvLine[] = \app\models\WeatherAlert::getAlertTypeByEventId($alertData->event); $csvLine[] = $property->id; $csvLine[] = $property->streetAddress; $csvLine[] = $property->city; $csvLine[] = $property->state; $csvLine[] = $property->zipcode; $csvLine[] = $property->client; $csvLine[] = $property->latitude; $csvLine[] = $property->longitude; $csvLine[] = $property->status; fputcsv($output, $csvLine); $csvInlineOutput[] = $csvLine; } echo json_encode(['file' => $filename]); } else { if (isset($get['file']) && file_exists(Yii::$app->basePath . '/web/' . $this->tmpPath . '/' . $get['file'])) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($get['file'])); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize(Yii::$app->basePath . '/web/' . $this->tmpPath . '/' . $get['file'])); readfile(Yii::$app->basePath . '/web/' . $this->tmpPath . '/' . $get['file']); Yii::$app->end(); } } }
<h1>Alert Information</h1> <table width="100%" border="1"> <tr> <th>ID</th> <th>Date</th> <th>EventID</th> <th>Magnitude</th> <th>Severity</th> </tr> <tr> <td><?= $alert->id ?></td> <td><?= date('M d, Y H:i',$alert->date) ?></td> <td><?= \app\models\WeatherAlert::getAlertTypeByEventId($alert->event) ?></td> <td><?= $alert->magnitude ?></td> <td><?= $alert->severity ?></td> </tr> </table> <h1>Affected Properties Information</h1> <table width="100%" border="1"> <tr> <th>ID</th> <th>Street Address</th> <th>City</th> <th>State</th> <th>Zip Code</th> <th>Client</th> <th>Latitude</th> <th>Longitude</th>