/** * download * * @return file * @throws InternalErrorException */ public function download() { App::uses('TemporaryFolder', 'Files.Utility'); App::uses('CsvFileWriter', 'Files.Utility'); App::uses('ZipDownloader', 'Files.Utility'); $zipPassword = $this->__getCompressPassword(); if ($zipPassword === false) { return false; } try { $userId = Current::read('User.id'); $contentKey = $this->request->params['key']; $this->CircularNotice->initCircularNotice($this); // 回覧を取得 $content = $this->CircularNoticeContent->getCircularNoticeContent($contentKey, $userId); if (!$content) { return $this->throwBadRequest(); } $contentId = $content['CircularNoticeContent']['id']; // Paginator経由で回覧先一覧を取得 $this->Paginator->settings = $this->CircularNoticeTargetUser->getCircularNoticeTargetUsersForPaginator($contentId, $this->params['named'], $userId, 0); $targetUsers = $this->Paginator->paginate('CircularNoticeTargetUser'); $tmpFolder = new TemporaryFolder(); $csvFile = new CsvFileWriter(array('folder' => $tmpFolder->path)); // ヘッダ取得 $header = $this->CircularNotice->getTargetUserHeader(); $csvFile->add($header); // 回答データ整形 $content = $content['CircularNoticeContent']; foreach ($targetUsers as $targetUser) { $answer = $this->__parseAnswer($content['reply_type'], $targetUser); $readDatetime = __d('circular_notices', 'Unread'); if ($targetUser['CircularNoticeTargetUser']['read_datetime']) { $readDatetime = $this->CircularNotice->getDisplayDateFormat($targetUser['CircularNoticeTargetUser']['read_datetime']); } $replyDatetime = __d('circular_notices', 'Unreply'); if ($targetUser['CircularNoticeTargetUser']['reply_datetime']) { $replyDatetime = $this->CircularNotice->getDisplayDateFormat($targetUser['CircularNoticeTargetUser']['reply_datetime']); } $data = array(h($targetUser['User']['handlename']), h($readDatetime), h($replyDatetime), h($answer)); $csvFile->add($data); } } catch (Exception $e) { $this->NetCommons->setFlashNotification(__d('circular_notices', 'download error'), array('interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL)); $this->redirect(NetCommonsUrl::actionUrl(array('controller' => 'circular_notices', 'action' => 'view', 'block_id' => Current::read('Block.id'), 'frame_id' => Current::read('Frame.id'), 'key' => $contentKey))); return false; } $this->autoRender = false; $zipFileName = $content['subject'] . CircularNoticeComponent::EXPORT_COMPRESS_FILE_EXTENSION; $downloadFileName = $content['subject'] . CircularNoticeComponent::EXPORT_FILE_EXTENSION; return $csvFile->zipDownload(rawurlencode($zipFileName), $downloadFileName, $zipPassword); }
/** * download * * @return void * @throws InternalErrorException */ public function download() { // NetCommonsお約束:コンテンツ操作のためのURLには対象のコンテンツキーが必ず含まれている // まずは、そのキーを取り出す // 登録フォームキー $registrationKey = $this->_getRegistrationKeyFromPass(); // キー情報をもとにデータを取り出す $registration = $this->RegistrationAnswerSummaryCsv->getRegistrationForAnswerCsv($registrationKey); if (!$registration) { $this->_setFlashMessageAndRedirect(__d('registrations', 'Designation of the registration does not exist.')); return; } // 圧縮用パスワードキーを求める if (!empty($this->request->data['AuthorizationKey']['authorization_key'])) { $zipPassword = $this->request->data['AuthorizationKey']['authorization_key']; } else { $this->_setFlashMessageAndRedirect(__d('registrations', 'Setting of password is required always to download answers.')); return; } try { $tmpFolder = new TemporaryFolder(); $csvFile = new CsvFileWriter(array('folder' => $tmpFolder->path)); // 登録データを一気に全部取得するのは、データ爆発の可能性があるので // REGISTRATION_CSV_UNIT_NUMBER分に制限して取得する $offset = 0; do { $datas = $this->RegistrationAnswerSummaryCsv->getAnswerSummaryCsv($registration, self::REGISTRATION_CSV_UNIT_NUMBER, $offset); // CSV形式で書きこみ foreach ($datas as $data) { $csvFile->add($data); } $dataCount = count($datas); // データ数カウント $offset += $dataCount; // 次の取得開始位置をずらす } while ($dataCount == self::REGISTRATION_CSV_UNIT_NUMBER); // データ取得数が制限値分だけとれている間は繰り返す } catch (Exception $e) { // NetCommonsお約束:エラーメッセージのFlash表示 $this->_setFlashMessageAndRedirect(__d('registrations', 'download error')); return; } // Downloadの時はviewを使用しない $this->autoRender = false; // ダウンロードファイル名決定 登録フォーム名称をつける $zipFileName = $registration['Registration']['title'] . '.zip'; $downloadFileName = $registration['Registration']['title'] . '.csv'; // 出力 return $csvFile->zipDownload(rawurlencode($zipFileName), $downloadFileName, $zipPassword); }