Skip to content

Feature: Configurable default values for fields #353

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions _test/AccessTableDataDB.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,36 @@ public function test_pseudodiff() {

$this->assertEquals($expected_pseudodiff, $actual_pseudodiff);
}

public function test_getData_defaults() {
$schemaData = meta\AccessTable::byTableName('testtable', 'newpage', time());

// act
$actual_data = $schemaData->getData();

$expected_data = array(
'testcolumn' => 'defvalue1',
'testMulitColumn' => array('defvalue2.1', 'defvalue2.2')
);

// assert
foreach($expected_data as $key => $value) {
$this->assertEquals($value, $actual_data[$key]->getValue());
}
}

public function test_getDataArray_defaults() {
$schemaData = meta\AccessTable::byTableName('testtable', 'newpage', time());

// act
$actual_data = $schemaData->getDataArray();

$expected_data = array(
'testcolumn' => 'defvalue1',
'testMulitColumn' => array('defvalue2.1', 'defvalue2.2')
);

// assert
$this->assertEquals($expected_data, $actual_data, '');
}
}
12 changes: 8 additions & 4 deletions _test/Type_AbstractBase.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function test_trans_empty_noplugin() {
'hint' => array(
'en' => ''
),
'visibility' => array('inpage' => true, 'ineditor' => true)
'visibility' => array('inpage' => true, 'ineditor' => true),
'default' => ''
),
$type->getConfig()
);
Expand All @@ -60,7 +61,8 @@ public function test_trans_preset_noplugin() {
'hint' => array(
'en' => 'english hint'
),
'visibility' => array('inpage' => true, 'ineditor' => true)
'visibility' => array('inpage' => true, 'ineditor' => true),
'default' => ''
),
$type->getConfig()
);
Expand Down Expand Up @@ -93,7 +95,8 @@ public function test_trans_empty_plugin() {
'it' => '',
'de' => '',
),
'visibility' => array('inpage' => true, 'ineditor' => true)
'visibility' => array('inpage' => true, 'ineditor' => true),
'default' => ''
),
$type->getConfig()
);
Expand Down Expand Up @@ -133,7 +136,8 @@ public function test_trans_preset_plugin() {
'it' => '',
'de' => 'german hint',
),
'visibility' => array('inpage' => true, 'ineditor' => true)
'visibility' => array('inpage' => true, 'ineditor' => true),
'default' => ''
),
$type->getConfig()
);
Expand Down
15 changes: 15 additions & 0 deletions _test/edit.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function setUp() {

$this->loadSchemaJSON('schema1');
$this->loadSchemaJSON('schema2', 'schema2int');
$this->loadSchemaJSON('schema3');
$this->saveData(
'page01',
'schema1',
Expand Down Expand Up @@ -63,6 +64,20 @@ public function test_createForm_emptyData() {
$this->checkField($pq, 'schema1', 'fourth', '');
}

public function test_createForm_defaultData() {
$edit = new mock\action_plugin_struct_edit();
global $ID;
$ID = 'page02';
$test_html = $edit->createForm('schema3');

$pq = \phpQuery::newDocument($test_html);
$this->assertEquals('schema3', $pq->find('legend')->text());
$this->checkField($pq, 'schema3', 'first', 'first');
$this->checkField($pq, 'schema3', 'second', 'second1, second2');
$this->checkField($pq, 'schema3', 'third', 'third');
$this->checkField($pq, 'schema3', 'fourth', 'fourth');
}

public function test_createForm_postData() {
global $INPUT, $ID;
$ID = 'page01';
Expand Down
4 changes: 4 additions & 0 deletions _test/json/schema1.struct.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"inpage": true,
"ineditor": true
},
"default": "",
"prefix": "",
"postfix": "",
"label": {
Expand All @@ -44,6 +45,7 @@
"inpage": true,
"ineditor": true
},
"default": "",
"prefix": "",
"postfix": "",
"label": {
Expand All @@ -66,6 +68,7 @@
"inpage": true,
"ineditor": true
},
"default": "",
"prefix": "",
"postfix": "",
"label": {
Expand All @@ -88,6 +91,7 @@
"inpage": true,
"ineditor": true
},
"default": "",
"prefix": "",
"postfix": "",
"label": {
Expand Down
106 changes: 106 additions & 0 deletions _test/json/schema3.struct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"structversion": "2016-03-21",
"schema": "schema1",
"id": "1",
"user": "",
"config": {
"allowed editors": "",
"label": {
"en": ""
}
},
"columns": [
{
"colref": 1,
"ismulti": false,
"isenabled": true,
"sort": 10,
"label": "first",
"class": "Text",
"config": {
"visibility": {
"inpage": true,
"ineditor": true
},
"default": "first",
"prefix": "",
"postfix": "",
"label": {
"en": ""
},
"hint": {
"en": ""
}
}
},
{
"colref": 2,
"ismulti": true,
"isenabled": true,
"sort": 20,
"label": "second",
"class": "Text",
"config": {
"visibility": {
"inpage": true,
"ineditor": true
},
"default": "second1, second2",
"prefix": "",
"postfix": "",
"label": {
"en": ""
},
"hint": {
"en": ""
}
}
},
{
"colref": 3,
"ismulti": false,
"isenabled": true,
"sort": 30,
"label": "third",
"class": "Text",
"config": {
"visibility": {
"inpage": true,
"ineditor": true
},
"default": "third",
"prefix": "",
"postfix": "",
"label": {
"en": ""
},
"hint": {
"en": ""
}
}
},
{
"colref": 4,
"ismulti": false,
"isenabled": true,
"sort": 40,
"label": "fourth",
"class": "Text",
"config": {
"visibility": {
"inpage": true,
"ineditor": true
},
"default": "fourth",
"prefix": "",
"postfix": "",
"label": {
"en": ""
},
"hint": {
"en": ""
}
}
}
]
}
2 changes: 2 additions & 0 deletions _test/json/testtable.struct.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"label": "testcolumn",
"class": "Text",
"config": {
"default": "defvalue1",
"prefix": "",
"postfix": ""
}
Expand All @@ -20,6 +21,7 @@
"label": "testMulitColumn",
"class": "Text",
"config": {
"default": "defvalue2.1, defvalue2.2",
"prefix": "",
"postfix": ""
}
Expand Down
10 changes: 9 additions & 1 deletion helper/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ public function renderfield($params, Doku_Form $form, $formid) {
}

// output the field
$value = new Value($this->column, $this->opt['value']);
$rawvalue = $this->opt['value'];
if (empty($rawvalue)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would overwrite a value of 0 with the default value.

$rawvalue = $this->column->getType()->getDefaultValue();
}
if ($this->column->isMulti()) {
$rawvalue = explode(',', $rawvalue);
$rawvalue = array_map('trim', $rawvalue);
}
$value = new Value($this->column, $rawvalue);
$field = $this->makeField($value, $params['name']);
$form->addElement($field);
}
Expand Down
18 changes: 12 additions & 6 deletions meta/AccessTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ protected function consolidateData($DBdata, $asarray = false) {
// if no data saved yet, return empty strings
if($DBdata) {
$val = $DBdata[0]['out' . $col->getColref()];

// multi val data is concatenated
if($col->isMulti()) {
$val = explode($sep, $val);
$val = array_filter($val);
}
} else {
$val = '';
}
$val = $col->getType()->getDefaultValue();

// multi val data is concatenated
if($col->isMulti()) {
$val = explode($sep, $val);
$val = array_filter($val);
// multi val data is concatenated
if($col->isMulti()) {
$val = explode(',', $val);
$val = array_map('trim', $val);
}
Comment on lines +217 to +223
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the else branch logic changed here. The isMulti() check used to be outside the else, now it is inside?

}

$value = new Value($col, $val);
Expand Down
12 changes: 10 additions & 2 deletions types/AbstractBaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractBaseType {
/**
* @var array config keys that should not be cleaned despite not being in $config
*/
protected $keepconfig = array('label', 'hint', 'visibility');
protected $keepconfig = array('label', 'hint', 'visibility', 'default');

/**
* @var string label for the field
Expand Down Expand Up @@ -72,7 +72,8 @@ public function __construct($config = null, $label = '', $ismulti = false, $tid
'visibility' => array(
'inpage' => true,
'ineditor' => true,
)
),
'default' => '',
);

// use previously saved configuration, ignoring all keys that are not supposed to be here
Expand Down Expand Up @@ -215,6 +216,13 @@ public function isVisibleInPage() {
return $this->config['visibility']['inpage'];
}

/**
* @return string
*/
public function getDefaultValue() {
return $this->config['default'];
}

/**
* Split a single value into multiple values
*
Expand Down
3 changes: 3 additions & 0 deletions types/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function getOptions() {
* @return string
*/
public function valueEditor($name, $rawvalue, $htmlID) {
$rawvalue = trim($rawvalue);
$params = array(
'name' => $name,
'class' => 'struct_'.strtolower($this->getClass()),
Expand Down Expand Up @@ -60,6 +61,8 @@ public function valueEditor($name, $rawvalue, $htmlID) {
* @return string
*/
public function multiValueEditor($name, $rawvalues, $htmlID) {
$rawvalues = array_map('trim', $rawvalues);
$rawvalues = array_filter($rawvalues);
$params = array(
'name' => $name . '[]',
'class' => 'struct_'.strtolower($this->getClass()),
Expand Down