Commit d3fe1f7e authored by Marc Egger's avatar Marc Egger
Browse files

Refs #10120 build toml, current toml parser needs php 7.1!

parent 4799d6bc
Pipeline #3548 failed with stages
in 2 minutes and 11 seconds
......@@ -3,9 +3,11 @@
namespace IMATHUZH\Qfq\Core\Form;
use Yosymfony\Toml\Toml;
use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Store\Store;
use Yosymfony\Toml\TomlBuilder;
// TODO: move to Constants.php if this file is actually used
const SCHEMA_COLUMN_NAME = 'COLUMN_NAME';
......@@ -13,7 +15,7 @@ const SCHEMA_COLUMN_DEFAULT = 'COLUMN_DEFAULT';
class FormAsFile
{
public function databaseToJson(string $formName, Database $database,Store $store)
public function databaseToJson(string $formName, Database $database, string $dbNameQFQ)
{
// TODO: create FORM_schema.JSON and FORM_ELEMENT_schema.JSON with default values
// TODO: read defaults from FORM_schema.JSON instead of database schema
......@@ -35,39 +37,60 @@ class FormAsFile
[$formName], 'Form "' . $formName . '" not found or multiple forms with the same name.');
// define $formNoDefault: array(column name => value)
$formNoDefault = $this->filterNonDefaultColumns($form, 'Form', $database, $store);
$formNoDefault = $this->filterNonDefaultColumns($form, 'Form', $database, $dbNameQFQ);
// define $formElementsNoDefault: array(array(column name => value))
$formElements = $database->sql("SELECT * FROM `FormElement` AS `fe` WHERE `fe`.`formId` = ? ORDER BY `fe`.`ord`, `fe`.`id`", ROW_REGULAR, [$form["id"]]);
$formElementsNoDefault = array_map(function ($formElement) use ($store, $database) {
return $this->filterNonDefaultColumns($formElement, 'FormElement', $database, $store);
$formElementsNoDefault = array_map(function ($formElement) use ($dbNameQFQ, $database) {
return $this->filterNonDefaultColumns($formElement, 'FormElement', $database, $dbNameQFQ);
}, $formElements);
$formNoDefault['formElementDefaults'] = $this->getColumnDefaults('FormElement', $database, $store);
$formNoDefault['formElements'] = $formElements;
$formNoDefault['formElementsNoDefault'] = $formElementsNoDefault;
/////// DEBUG
// collect output
$columnDefaults = $this->getColumnDefaults('FormElement', $database, $dbNameQFQ);
// parse TOML
$array = Toml::Parse('key = [1,2,3]');
// build TOML
$tb = new TomlBuilder();
$tb->addTable('Form');
foreach($formNoDefault as $parameter => $value) {
$tb->addValue($parameter, $value);
}
foreach ($formElementsNoDefault as $formElement) {
// add form elements to toml
// php auf version 7.1 hoch
// composer update
$tb->addArrayTables('FormElement');
foreach ($formElement as $parameter => $value) {
$tb->addValue($parameter, $value);
}
}
$result = $tb->getTomlString();
throw new \UserFormException(json_encode([
ERROR_MESSAGE_TO_USER => 'Marc debug error',
ERROR_MESSAGE_TO_DEVELOPER => json_encode($formNoDefault, JSON_PRETTY_PRINT)
ERROR_MESSAGE_TO_DEVELOPER => $result // json_encode($array, JSON_PRETTY_PRINT)
]));
}
/**
* @param string $tableName
* @param Database $database
* @param Store $store
* @param string $dbNameQFQ
* @return mixed|null
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
public function getColumnDefaults(string $tableName, Database $database, Store $store): array
public function getColumnDefaults(string $tableName, Database $database, string $dbNameQFQ): array
{
// define: $formSchema: array(array('COLUMN_NAME' => column name, 'COLUMN_DEFAULT' => column default))
$dbNameQFQ = $store->getVar(SYSTEM_DB_NAME_QFQ, STORE_SYSTEM);
$query = "SELECT " . SCHEMA_COLUMN_NAME . ", " . SCHEMA_COLUMN_DEFAULT . " FROM Information_Schema.Columns WHERE Table_Schema = '$dbNameQFQ' AND Table_Name = '$tableName'";
$formSchema = $database->sql($query, ROW_REGULAR, []);
......@@ -79,18 +102,18 @@ class FormAsFile
}
/**
* @param $row
* @param array $row
* @param string $tableName
* @param Database $database
* @param Store $store
* @param string $dbNameQFQ
* @return array
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
public function filterNonDefaultColumns(array $row, string $tableName, Database $database, Store $store): array
public function filterNonDefaultColumns(array $row, string $tableName, Database $database, string $dbNameQFQ): array
{
$columnDefaults = $this->getColumnDefaults($tableName, $database, $store);
$columnDefaults = $this->getColumnDefaults($tableName, $database, $dbNameQFQ);
// return $formNoDefault: array(column name => value)
return array_filter($row, function ($value, $columnName) use ($columnDefaults) {
......
......@@ -62,7 +62,7 @@ class QuickFormQuery {
*/
protected $evaluate = null;
protected $formSpec = array();
protected $formSpec = array(); // Stores the form content after parsing SQL queries and QFQ syntax stored in form attributes
protected $feSpecAction = array(); // Form Definition: copy of the loaded form
protected $feSpecNative = array(); // FormEelement Definition: all formElement.class='action' of the loaded form
protected $feSpecNativeRaw = array(); // FormEelement Definition: all formElement.class='action' of the loaded form
......@@ -1000,7 +1000,7 @@ class QuickFormQuery {
///////////////// JUST A TEST, DELETE ME! //////////////
$formASFile = new FormAsFile();
$formASFile->databaseToJson('person', $this->dbArray[$this->dbIndexQfq], $this->store);
$formASFile->databaseToJson('form', $this->dbArray[$this->dbIndexQfq], $this->store->getVar(SYSTEM_DB_NAME_QFQ, STORE_SYSTEM));
///////////////// TEST FINISHED ////////////////////////
// Load form
......@@ -1069,6 +1069,8 @@ class QuickFormQuery {
}
$formSpec = $this->syncSystemFormConfig($formSpec);
// Set form parameter which are expected to exist.
$formSpec = $this->initForm($formSpec, $recordId);
$formSpec = array_merge($formSpec, $parseLater);
......
......@@ -2,7 +2,8 @@
"require": {
"phpoffice/phpspreadsheet": "^1.3",
"ext-json": "*",
"twig/twig": "^2.0"
"twig/twig": "^2.0",
"yosymfony/toml": "^0.3.3"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment