Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KJ: Issue 1508 リレーションを設定した複数の子テーブルに対しての優先順 #1560

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/lang/en/exment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@
'suggest_column_label' => 'Table Column',
'suggest_other_label' => 'Other',
'form_block_name' => 'Form Block Name',
'form_block_order' => 'Form Block Display Order',
'field_default' => 'Standard',
'read_only' => 'Read Only',
'view_only' => 'View Only',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ja/exment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,7 @@
'suggest_column_label' => 'テーブル列',
'suggest_other_label' => 'その他',
'form_block_name' => 'フォームブロック名',
'form_block_order' => '表示順',
'field_default' => '標準',
'read_only' => '読み取り専用',
'view_only' => '表示専用',
Expand Down
7 changes: 7 additions & 0 deletions resources/views/custom-form/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
}} {{ Form::text("{$custom_form_block['header_name']}[form_block_view_name]", $custom_form_block['form_block_view_name'],
['class' => 'form-control', 'style' => 'width:400px;']) }}
</div>
{{-- select hasmany or hasmanytable --}}
@if($custom_form_block['form_block_type'] != '0')
<div class="form-group">
{{ Form::label("", exmtrans('custom_form.form_block_order'), ['class' => 'control-label', 'style' => 'padding-left:15px;padding-right:15px;'])
}} {{ Form::number("{$custom_form_block['header_name']}[options][form_block_order]", $custom_form_block['form_block_order'], ['class' => 'form-control', 'style' => 'width:70px', 'min' => '0', 'step' => '1']) }}
</div>
@endif
</div>

@if($custom_form_block['form_block_type'] != '2')
Expand Down
4 changes: 3 additions & 1 deletion src/Controllers/CustomFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,9 @@ protected function getFormBlockItems($form)
$req_custom_form_blocks = old('custom_form_blocks');
if (!isset($req_custom_form_blocks)
) {
return $form->custom_form_blocks;
return $form->custom_form_blocks->sortBy(function ($item, $key) {
return $item->getOption('form_block_order')?? 0;
});
}

return collect($req_custom_form_blocks)->map(function ($req_custom_form_block, $key) {
Expand Down
5 changes: 4 additions & 1 deletion src/DataItems/Form/DefaultForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ public function form()
$force_caculate_column = [];
$this->setCustomFormEvents($calc_formula_array, $changedata_array, $relatedlinkage_array, $force_caculate_column);

$custom_form_blocks = $this->custom_form->custom_form_blocks->sortBy(function ($item, $key) {
return $item->getOption('form_block_order')?? -1;
});
// loop for custom form blocks
foreach ($this->custom_form->custom_form_blocks as $custom_form_block) {
foreach ($custom_form_blocks as $custom_form_block) {
// if available is false, continue
if (!$custom_form_block->available) {
continue;
Expand Down
5 changes: 4 additions & 1 deletion src/DataItems/Show/DefaultShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,11 @@ protected function setChildBlockBox($row)

$trashed = boolval(request()->get('trashed'));

$custom_form_blocks = $this->custom_form->custom_form_blocks->sortBy(function ($item, $key) {
return $item->getOption('form_block_order')?? -1;
});
// loop for custom form blocks
foreach ($this->custom_form->custom_form_blocks as $custom_form_block) {
foreach ($custom_form_blocks as $custom_form_block) {
// if available is false, continue
if (!$custom_form_block->available) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/Database/Seeder/TestDataSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ protected function createRelationTables($users)
return $view->view_kind_type == ViewKindType::FILTER;
});

$child_table_2 = $this->createTable('child_table_2' . $relationItem['suffix'], $childOptions);
$this->createPermission([Permission::CUSTOM_VALUE_EDIT => $child_table_2]);

// cerate pivot table
$pivot_table = $this->createTable('pivot_table' . $relationItem['suffix'], [
'menuParentId' => $menu->id,
Expand Down
3 changes: 2 additions & 1 deletion src/Services/FormSetting/FormBlock/BlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public function getItemsForDisplay(): array
'header_name' => $this->getHtmlHeaderName(),
'suggests' => $this->getSuggestItems(),
'custom_form_rows' => $this->getCustomFormRows(),
'hasmany_type' => $this->custom_form_block->getOption('hasmany_type')
'hasmany_type' => $this->custom_form_block->getOption('hasmany_type'),
'form_block_order' => $this->custom_form_block->getOption('form_block_order')
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Services/FormSetting/FormBlock/RelationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static function getDefaultBlock(CustomTable $custom_table, CustomRelation
$block->form_block_view_name = $block->label;
$block->available = 0;
$block->options = [
'hasmany_type' => null
'hasmany_type' => null,
'form_block_order' => 0
];

/** @phpstan-ignore-next-line */
Expand Down
148 changes: 119 additions & 29 deletions tests/Browser/CCustomFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace Exceedone\Exment\Tests\Browser;

use Exceedone\Exment\Model\CustomForm;
use Exceedone\Exment\Model\CustomFormBlock;
use Exceedone\Exment\Model\CustomFormColumn;
use Exceedone\Exment\Model\CustomColumn;
use Exceedone\Exment\Model\CustomTable;
use Exceedone\Exment\Model\CustomRelation;

class CCustomFormTest extends ExmentKitTestCase
{
Expand All @@ -15,36 +20,25 @@ protected function setUp(): void
{
parent::setUp();
$this->login();
}
$this->prepareTestTables();
}

/**
* prepare test table.
* prepare test table and columns.
*/
public function testPrepareTestTable()
protected function prepareTestTables()
{
$this->createCustomTable('exmenttest_form');
$this->createCustomTable('exmenttest_form_relation');
$this->createCustomColumns('exmenttest_form', ['integer', 'text', 'datetime', 'select', 'boolean', 'yesno', 'image']);
}

/**
* prepare test columns.
*/
public function testPrepareTestColumn()
protected function tearDown(): void
{
$targets = ['integer', 'text', 'datetime', 'select', 'boolean', 'yesno', 'image'];
$this->createCustomColumns('exmenttest_form', $targets);
if (($custom_table = CustomTable::getEloquent('exmenttest_form')) != null) {
$custom_table->delete();
}
}

/**
* prepare test columns and relation.
*/
public function testPrepareTestColumnAndRelation()
{
$targets = ['integer', 'text', 'datetime', 'select', 'boolean', 'yesno', 'image'];
$this->createCustomColumns('exmenttest_form_relation', $targets);
$this->createCustomRelation('exmenttest_form', 'exmenttest_form_relation');
}


/**
* Check custom form display.
*/
Expand All @@ -63,24 +57,17 @@ public function testDisplayFormSetting()
->seeInElement('label', 'フォーム表示名')
->seeInElement('h3[class=box-title]', exmtrans('custom_form.header_basic_setting'))
->seeInElement('h3[class=box-title]', 'テーブル - Exmenttest Form')
->seeInElement('h3[class=box-title]', '子テーブル - Exmenttest Form Relation')
->seeInElement('label', 'フォームブロック名')
->seeInElement('h4', 'フォーム項目')
->seeInElement('h5', 'フォーム項目 候補一覧')
->seeInElement('h5', 'フォーム項目 候補一覧')
// ->seeInElement('span[class=item-label]', 'ID')
// ->seeInElement('span[class=item-label]', '内部ID(20桁)')
->seeInElement('span', 'Integer')
->seeInElement('span', 'One Line Text')
->seeInElement('span', 'Date and Time')
->seeInElement('span', 'Select From Static Value')
->seeInElement('span', 'Select 2 value')
->seeInElement('span', 'Yes No')
->seeInElement('span', 'Image')
// ->seeInElement('span[class=item-label]', '作成日時')
// ->seeInElement('span[class=item-label]', '更新日時')
// ->seeInElement('span[class=item-label]', '作成ユーザー')
// ->seeInElement('span[class=item-label]', '更新ユーザー')
->seeInElement('span', '見出し')
->seeInElement('span', '説明文')
->seeInElement('span', 'HTML')
Expand All @@ -92,6 +79,9 @@ public function testDisplayFormSetting()
*/
public function testAddFormSuccess()
{
$custom_table = CustomTable::where('table_name', 'exmenttest_form')->first();
$custom_table_id = array_get($custom_table, 'id');

$pre_cnt = CustomForm::count();

// Create custom form
Expand All @@ -103,7 +93,7 @@ public function testAddFormSuccess()
->assertEquals($pre_cnt + 1, CustomForm::count())
;

$raw = CustomForm::orderBy('id', 'desc')->first();
$raw = CustomForm::where('custom_table_id', $custom_table_id)->orderBy('id', 'desc')->first();
$id = array_get($raw, 'id');

// Update custom form
Expand All @@ -113,5 +103,105 @@ public function testAddFormSuccess()
->press('admin-submit')
->seePageIs(admin_url('form/exmenttest_form'))
->seeInElement('td', '更新したフォーム');

$block = CustomFormBlock::where('custom_form_id', $id)->where('form_block_type', '0')->first();
$block_id = array_get($block, 'id');

$columns = CustomColumn::where('custom_table_id', $custom_table_id)->get();

// add field column
foreach($columns as $idx => $column) {
$form_column = new CustomFormColumn();
$form_column->custom_form_block_id = $block_id;
$form_column->form_column_type = 0;
$form_column->form_column_target_id = $column->id;
$form_column->row_no = 1;
$form_column->column_no = 1;
$form_column->width = 2;
$form_column->order = $idx + 1;
$form_column->save();
}

$this->visit(admin_url('data/exmenttest_form/create'))
->seeInElement('label', 'Integer')
->seeInElement('label', 'One Line Text')
->seeInElement('label', 'Date and Time')
->seeInElement('label', 'Select From Static Value')
->seeInElement('label', 'Select 2 value')
->seeInElement('label', 'Yes No')
->seeInElement('label', 'Image')
;
}

/**
* avairable relation blocks.
*/
public function testRelationFormSuccess()
{
$custom_table = CustomTable::where('table_name', 'parent_table')->first();
$custom_table_id = array_get($custom_table, 'id');

$raw = CustomForm::where('custom_table_id', $custom_table_id)->where('default_flg', 1)->first();
$id = array_get($raw, 'id');

$blocks = CustomFormBlock::where('custom_form_id', $id)->get();

$relations = CustomRelation::getRelationsByParent($custom_table);
foreach ($relations as $idx => $relation) {
$block = $blocks->first(function ($val) use($relation) {
return $val->form_block_target_table_id == $relation->child_custom_table_id;
});
if (!$block) {
$block = new CustomFormBlock();
$block->custom_form_id = $id;
$block->form_block_type = 1;
$block->form_block_target_table_id = $relation->child_custom_table_id;
}
$block->setOption('form_block_order', $idx);
$block->available = 1;
$block->save();
}

$block_ids = CustomFormBlock::where('custom_form_id', $id)->pluck('id');

$this->visit(admin_url('form/parent_table/'. $id . '/edit'))
->seeInElement('h3[class=box-title]', 'テーブル - parent_table')
->seeInElement('h3[class=box-title]', '子テーブル - child_table')
->seeInElement('h3[class=box-title]', '子テーブル - child_table_2')
->type('親テーブルのブロック', 'custom_form_blocks[' . $block_ids[0] .'][form_block_view_name]')
->type('子テーブル1のブロック', 'custom_form_blocks[' . $block_ids[1] .'][form_block_view_name]')
->type('子テーブル2のブロック', 'custom_form_blocks[' . $block_ids[2] .'][form_block_view_name]')
->press('admin-submit')
->seePageIs(admin_url('form/parent_table'))
;

$this->visit(admin_url('form/parent_table/'. $id . '/edit'))
->seeInField('custom_form_blocks[' . $block_ids[0] .'][form_block_view_name]', '親テーブルのブロック')
->seeInField('custom_form_blocks[' . $block_ids[1] .'][form_block_view_name]', '子テーブル1のブロック')
->seeInField('custom_form_blocks[' . $block_ids[2] .'][form_block_view_name]', '子テーブル2のブロック')
;

// check before update
$this->visit(admin_url('data/parent_table/create'));
$crawler = $this->crawler()->filter('h4[class=field-header]');
$element = $crawler->eq(0);
$this->assertEquals('子テーブル1のブロック', $element->text());
$element = $crawler->eq(1);
$this->assertEquals('子テーブル2のブロック', $element->text());

$this->visit(admin_url('form/parent_table/'. $id . '/edit'))
->type('2', 'custom_form_blocks[' . $block_ids[1] .'][options][form_block_order]')
->type('1', 'custom_form_blocks[' . $block_ids[2] .'][options][form_block_order]')
->press('admin-submit')
->seePageIs(admin_url('form/parent_table'))
;

// check after update
$this->visit(admin_url('data/parent_table/create'));
$crawler = $this->crawler()->filter('h4[class=field-header]');
$element = $crawler->eq(0);
$this->assertEquals('子テーブル2のブロック', $element->text());
$element = $crawler->eq(1);
$this->assertEquals('子テーブル1のブロック', $element->text());
}
}
33 changes: 16 additions & 17 deletions tests/Feature/CustomValueDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,22 +335,21 @@ protected function getCustomValueInfo(CustomTable $custom_table, $custom_value_i
protected function assertCustomRelationCount(CustomValue $custom_value, int $dataCount, int $deleteCount)
{
// delete custom relation is 1:n value
$relations = CustomRelation::getRelationsByParent($custom_value->custom_table, RelationType::ONE_TO_MANY);
// loop relations
foreach ($relations as $relation) {
$children = $custom_value->getChildrenValues($relation, true)
->withTrashed()
->get();

$dataCountResult = $children->filter(function ($child) {
return !$child->trashed();
})->count();
$deleteCountResult = $children->filter(function ($child) {
return $child->trashed();
})->count();

$this->assertMatch($dataCountResult, $dataCount);
$this->assertMatch($deleteCountResult, $deleteCount);
}
$relation = CustomRelation::getRelationByParentChild($custom_value->custom_table,
TestDefine::TESTDATA_TABLE_NAME_CHILD_TABLE, RelationType::ONE_TO_MANY);

$children = $custom_value->getChildrenValues($relation, true)
->withTrashed()
->get();

$dataCountResult = $children->filter(function ($child) {
return !$child->trashed();
})->count();
$deleteCountResult = $children->filter(function ($child) {
return $child->trashed();
})->count();

$this->assertMatch($dataCountResult, $dataCount);
$this->assertMatch($deleteCountResult, $deleteCount);
}
}
3 changes: 3 additions & 0 deletions tests/Unit/PermissionUpDownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use Exceedone\Exment\Model\CustomTable;
use Exceedone\Exment\Model\System;
use Exceedone\Exment\Enums\JoinedOrgFilterType;
use Exceedone\Exment\Tests\DatabaseTransactions;

class PermissionUpDownTest extends UnitTestBase
{
use DatabaseTransactions;

protected function init()
{
System::clearCache();
Expand Down
Loading