Skip to content

Commit

Permalink
Merge pull request #2113 from chihiro-adachi/fix-form-session
Browse files Browse the repository at this point in the history
会員マスタの検索条件のセッション保持方法を変更
  • Loading branch information
ryo-endo committed Mar 21, 2017
2 parents 1868773 + a74de65 commit 9054454
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 26 deletions.
29 changes: 9 additions & 20 deletions src/Eccube/Controller/Admin/Customer/CustomerController.php
Expand Up @@ -85,8 +85,9 @@ public function index(Application $app, Request $request, $page_no = null)
$page_count
);

// sessionのデータ保持
$session->set('eccube.admin.customer.search', $searchData);
// sessionに検索条件を保持.
$viewData = \Eccube\Util\FormUtil::getViewData($searchForm);
$session->set('eccube.admin.customer.search', $viewData);
$session->set('eccube.admin.customer.search.page_no', $page_no);
}
} else {
Expand All @@ -96,16 +97,18 @@ public function index(Application $app, Request $request, $page_no = null)
$session->remove('eccube.admin.customer.search.page_no');
} else {
// pagingなどの処理
$searchData = $session->get('eccube.admin.customer.search');
if (is_null($page_no)) {
$page_no = intval($session->get('eccube.admin.customer.search.page_no'));
} else {
$session->set('eccube.admin.customer.search.page_no', $page_no);
}
if (!is_null($searchData)) {
$viewData = $session->get('eccube.admin.customer.search');
if (!is_null($viewData)) {
// sessionに保持されている検索条件を復元.
$searchData = \Eccube\Util\FormUtil::submitAndGetData($searchForm, $viewData);

// 表示件数
$pcount = $request->get('page_count');
$page_count = empty($pcount) ? $page_count : $pcount;
$page_count = $request->get('page_count', $page_count);

$qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);

Expand All @@ -123,20 +126,6 @@ public function index(Application $app, Request $request, $page_no = null)
$page_no,
$page_count
);

// セッションから検索条件を復元
if (count($searchData['sex']) > 0) {
$sex_ids = array();
foreach ($searchData['sex'] as $Sex) {
$sex_ids[] = $Sex->getId();
}
$searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
}

if (!is_null($searchData['pref'])) {
$searchData['pref'] = $app['eccube.repository.master.pref']->find($searchData['pref']->getId());
}
$searchForm->setData($searchData);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/Eccube/Service/CsvExportService.php
Expand Up @@ -381,12 +381,13 @@ public function getOrderQueryBuilder(Request $request)
public function getCustomerQueryBuilder(Request $request)
{
$session = $request->getSession();
if ($session->has('eccube.admin.customer.search')) {
$searchData = $session->get('eccube.admin.customer.search');
$this->findDeserializeObjects($searchData);
} else {
$searchData = array();
}
$viewData = $session->get('eccube.admin.customer.search', array());

$app = \Eccube\Application::getInstance();
$searchForm = $app['form.factory']
->create('admin_search_customer', null, array('csrf_protection' => true));

$searchData = \Eccube\Util\FormUtil::submitAndGetData($searchForm, $viewData);

// 会員データのクエリビルダを構築.
$qb = $this->customerRepository
Expand Down
71 changes: 71 additions & 0 deletions src/Eccube/Util/FormUtil.php
@@ -0,0 +1,71 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
*
* http://www.lockon.co.jp/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

namespace Eccube\Util;


use Symfony\Component\Form\FormInterface;

class FormUtil
{
/**
* formオブジェクトからviewDataを取得する.
*
* @param FormInterface $form
* @return array
*/
public static function getViewData(FormInterface $form)
{
$viewData = array();
$forms = $form->all();

if (empty($forms)) {
return $form->getViewData();
}

foreach ($forms as $key => $value) {
// choice typeは各選択肢もFormとして扱われるため再帰しない.
if ($value->getConfig()->hasOption('choices')) {
$viewData[$key] = $value->getViewData();
} else {
$viewData[$key] = self::getViewData($value);
}
}

return $viewData;
}

/**
* formオブジェクトにviewdataをsubmitし, マッピングした結果を返す.
*
* @param FormInterface $form
* @param $viewData
* @return mixed
*/
public static function submitAndGetData(FormInterface $form, $viewData)
{
$form->submit($viewData);

return $form->getData();
}
}
151 changes: 151 additions & 0 deletions tests/Eccube/Tests/Util/FormUtilTest.php
@@ -0,0 +1,151 @@
<?php

namespace Eccube\Tests\Util;

use Eccube\Tests\EccubeTestCase;
use Eccube\Util\FormUtil;

class FormUtilTest extends EccubeTestCase
{
protected $form;

protected $formData = array(
'pref' => '28',
'name' => 'パーコレータ',
'date' => '2017-02-01'
);

public function setUp()
{
parent::setUp();

$this->form = $this->app['form.factory']
->createBuilder(
'form',
null,
array(
'csrf_protection' => false,
)
)
->add('pref', 'pref')
->add('name', 'text')
->add('date', 'date', array(
'label' => '受注日(FROM)',
'required' => false,
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'empty_value' => array('year' => '----', 'month' => '--', 'day' => '--'),
))
->getForm();
}

public function testGetViewData()
{
$this->form->submit($this->formData);

$viewData = FormUtil::getViewData($this->form);

// POSTしたデータと同じになるはず.
$this->assertEquals($this->formData, $viewData);
}

public function testSubmitAndGetData()
{
$data = FormUtil::submitAndGetData($this->form, $this->formData);

// formはsubmitされている.
$this->assertTrue($this->form->isSubmitted());

// prefはPrefエンティティに変換されている.
$this->assertInstanceOf('\Eccube\Entity\Master\Pref', $data['pref']);
$this->assertEquals(28, $data['pref']->getId());
$this->assertEquals('兵庫県', $data['pref']->getName());

// dateはDateTimeに変換されている.
$this->assertInstanceOf('\DateTime', $data['date']);
}

/**
* AddressTypeなど, 子要素をもつFormTypeのテスト.
*/
public function testNestedFormType()
{
$formData = array(
'address' => array(
'pref' => '27',
'addr01' => '北区',
'addr02' => '梅田'
)
);

$form = $this->app['form.factory']
->createBuilder(
'form',
null,
array(
'csrf_protection' => false,
)
)
->add('address', 'address')
->getForm();

$form->submit($formData);
$viewData = FormUtil::getViewData($form);
$this->assertEquals($formData, $viewData);
}

/**
* choice typeのテスト
*/
public function testChoiceType()
{
$formData = array(
'sex' => '1',
);

$form = $this->app['form.factory']
->createBuilder(
'form',
null,
array(
'csrf_protection' => false,
)
)
->add('sex', 'sex')
->getForm();

$form->submit($formData);
$viewData = FormUtil::getViewData($form);
$this->assertEquals($formData, $viewData);
}


/**
* choice type(multiple)のテスト
*/
public function testChoiceTypeMultiple()
{
$formData = array(
'sex' => array('1', '2')
);

$form = $this->app['form.factory']
->createBuilder(
'form',
null,
array(
'csrf_protection' => false,
)
)
->add('sex', 'sex', array(
'multiple' => true,
))
->getForm();

$form->submit($formData);
$viewData = FormUtil::getViewData($form);
$this->assertEquals($formData, $viewData);
}

}

0 comments on commit 9054454

Please sign in to comment.