From 5589192c10d45b7ba2ce890072669975f5bbad60 Mon Sep 17 00:00:00 2001 From: Andreas Ferber Date: Mon, 27 Nov 2017 02:26:36 +0100 Subject: [PATCH] Configurable default values for fields Add a new configuration option to columns that allows adding default values for fields. --- _test/AccessTableDataDB.test.php | 32 ++++++++++ _test/Type_AbstractBase.test.php | 12 ++-- _test/edit.test.php | 15 +++++ _test/json/schema1.struct.json | 4 ++ _test/json/schema3.struct.json | 106 +++++++++++++++++++++++++++++++ _test/json/testtable.struct.json | 2 + helper/field.php | 10 ++- meta/AccessTable.php | 18 ++++-- types/AbstractBaseType.php | 12 +++- types/Dropdown.php | 3 + 10 files changed, 201 insertions(+), 13 deletions(-) create mode 100644 _test/json/schema3.struct.json diff --git a/_test/AccessTableDataDB.test.php b/_test/AccessTableDataDB.test.php index b7097e59..72a5d452 100644 --- a/_test/AccessTableDataDB.test.php +++ b/_test/AccessTableDataDB.test.php @@ -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, ''); + } } diff --git a/_test/Type_AbstractBase.test.php b/_test/Type_AbstractBase.test.php index ea71af54..743c7242 100644 --- a/_test/Type_AbstractBase.test.php +++ b/_test/Type_AbstractBase.test.php @@ -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() ); @@ -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() ); @@ -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() ); @@ -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() ); diff --git a/_test/edit.test.php b/_test/edit.test.php index 7733c6c5..b729fd3f 100644 --- a/_test/edit.test.php +++ b/_test/edit.test.php @@ -18,6 +18,7 @@ public function setUp() { $this->loadSchemaJSON('schema1'); $this->loadSchemaJSON('schema2', 'schema2int'); + $this->loadSchemaJSON('schema3'); $this->saveData( 'page01', 'schema1', @@ -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'; diff --git a/_test/json/schema1.struct.json b/_test/json/schema1.struct.json index 2dbcbb34..3970c81c 100644 --- a/_test/json/schema1.struct.json +++ b/_test/json/schema1.struct.json @@ -22,6 +22,7 @@ "inpage": true, "ineditor": true }, + "default": "", "prefix": "", "postfix": "", "label": { @@ -44,6 +45,7 @@ "inpage": true, "ineditor": true }, + "default": "", "prefix": "", "postfix": "", "label": { @@ -66,6 +68,7 @@ "inpage": true, "ineditor": true }, + "default": "", "prefix": "", "postfix": "", "label": { @@ -88,6 +91,7 @@ "inpage": true, "ineditor": true }, + "default": "", "prefix": "", "postfix": "", "label": { diff --git a/_test/json/schema3.struct.json b/_test/json/schema3.struct.json new file mode 100644 index 00000000..5b874193 --- /dev/null +++ b/_test/json/schema3.struct.json @@ -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": "" + } + } + } + ] +} diff --git a/_test/json/testtable.struct.json b/_test/json/testtable.struct.json index 861a8e25..03db21ea 100644 --- a/_test/json/testtable.struct.json +++ b/_test/json/testtable.struct.json @@ -9,6 +9,7 @@ "label": "testcolumn", "class": "Text", "config": { + "default": "defvalue1", "prefix": "", "postfix": "" } @@ -20,6 +21,7 @@ "label": "testMulitColumn", "class": "Text", "config": { + "default": "defvalue2.1, defvalue2.2", "prefix": "", "postfix": "" } diff --git a/helper/field.php b/helper/field.php index 210e2bc4..964823c6 100644 --- a/helper/field.php +++ b/helper/field.php @@ -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)) { + $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); } diff --git a/meta/AccessTable.php b/meta/AccessTable.php index 07a6dfdf..3f1afdda 100644 --- a/meta/AccessTable.php +++ b/meta/AccessTable.php @@ -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); + } } $value = new Value($col, $val); diff --git a/types/AbstractBaseType.php b/types/AbstractBaseType.php index 635b4510..32e57889 100644 --- a/types/AbstractBaseType.php +++ b/types/AbstractBaseType.php @@ -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 @@ -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 @@ -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 * diff --git a/types/Dropdown.php b/types/Dropdown.php index cde23890..892dd519 100644 --- a/types/Dropdown.php +++ b/types/Dropdown.php @@ -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()), @@ -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()),