Skip to content

Commit

Permalink
[7-3] AdminInquiryListControllerを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
hidenorigoto committed Dec 28, 2015
1 parent fe6d337 commit 61e74b2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
60 changes: 60 additions & 0 deletions docs/lists/ch07/07-03.txt
@@ -0,0 +1,60 @@
...
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use AppBundle\Entity\Inquiry;
use League\Csv\Writer;
...

/**
* @Route("/search.{_format}",
* defaults={"_format": "html"},
* requirements={
* "_format": "html|csv",
* }
* )
*/
public function indexAction(Request $request, $_format)
{
...
$inquiryList = $inquiryRepository->findAllByKeyword($keyword);

if ($_format == 'csv') {
$response = new Response($this->createCsv($inquiryList));
$d = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'inquiry_list.csv'
);
$response->headers->set('Content-Disposition', $d);

return $response;
}
...
}

private function createSearchForm()
{
return $this->createFormBuilder()
->add('search', 'search')
->add('submit', 'button', [
'label' => '検索',
])
->getForm();
}

private function createCsv($inquiryList)
{
/** @var Writer $writer */
$writer = Writer::createFromString('','');
$writer->setNewline("\r\n");

foreach ($inquiryList as $inquiry) {
/** @var Inquiry $inquiry */
$writer->insertOne([
$inquiry->getId(),
$inquiry->getName(),
$inquiry->getEmail()
]);
}

return (string)$writer;
}
44 changes: 41 additions & 3 deletions src/AppBundle/Controller/AdminInquiryListController.php
Expand Up @@ -5,16 +5,25 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use AppBundle\Entity\Inquiry;
use League\Csv\Writer;

/**
* @Route("/admin/inquiry")
*/
class AdminInquiryListController extends Controller
{
/**
* @Route("/search")
* @Route("/search.{_format}",
* defaults={"_format": "html"},
* requirements={
* "_format": "html|csv",
* }
* )
*/
public function indexAction(Request $request)
public function indexAction(Request $request, $_format)
{
$form = $this->createSearchForm();
$form->handleRequest($request);
Expand All @@ -28,6 +37,17 @@ public function indexAction(Request $request)

$inquiryList = $inquiryRepository->findAllByKeyword($keyword);

if ($_format == 'csv') {
$response = new Response($this->createCsv($inquiryList));
$d = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'inquiry_list.csv'
);
$response->headers->set('Content-Disposition', $d);

return $response;
}

return $this->render('Admin/Inquiry/index.html.twig',
[
'form' => $form->createView(),
Expand All @@ -40,9 +60,27 @@ private function createSearchForm()
{
return $this->createFormBuilder()
->add('search', 'search')
->add('submit', 'submit', [
->add('submit', 'button', [
'label' => '検索',
])
->getForm();
}

private function createCsv($inquiryList)
{
/** @var Writer $writer */
$writer = Writer::createFromString('','');
$writer->setNewline("\r\n");

foreach ($inquiryList as $inquiry) {
/** @var Inquiry $inquiry */
$writer->insertOne([
$inquiry->getId(),
$inquiry->getName(),
$inquiry->getEmail()
]);
}

return (string)$writer;
}
}

0 comments on commit 61e74b2

Please sign in to comment.