Further optimisation of FieldDefinition class.

This commit is contained in:
Markus Birth 2016-07-10 21:49:08 +02:00
parent 2e8a54cf9d
commit ebc0c89fcc
2 changed files with 58 additions and 20 deletions

View File

@ -6,6 +6,7 @@ class FieldDefinition
{ {
private $definitionsPrefix; private $definitionsPrefix;
private $fieldData = array(); private $fieldData = array();
private $placeholders = array();
/** /**
* @param string $type Type of FieldDefinition (hotel, car, etc.) * @param string $type Type of FieldDefinition (hotel, car, etc.)
@ -25,15 +26,18 @@ class FieldDefinition
$fieldData = array($fieldData); $fieldData = array($fieldData);
} }
$this->fieldData = $fieldData; $this->fieldData = $fieldData;
$this->prepareFields();
} }
public function getFieldData() public function getFieldData()
{ {
$this->processPlaceholders();
return $this->fieldData; return $this->fieldData;
} }
public function getGroups() public function getGroups()
{ {
$this->processPlaceholders();
$byGroup = array(); $byGroup = array();
foreach ($this->fieldData['groups'] as $id=>$group) { foreach ($this->fieldData['groups'] as $id=>$group) {
$byGroup[$group] = array( $byGroup[$group] = array(
@ -82,35 +86,69 @@ class FieldDefinition
} }
} }
/** private function prepareFields()
* Adds the given $values or default values and replaces $placeholders. Also
* adds some helping attributes to fields.
*
* @param mixed[] Key-value-array of values (value) to assign to fields (key)
* @param string[] Key-value-array of values (value) to replace placeholders (key) with
*/
public function addFieldValues($values = array(), $placeholders = array())
{ {
foreach ($this->fieldData['fields'] as $key=>$meta) { foreach ($this->fieldData['fields'] as $key=>$meta) {
$meta['field_id'] = $key; $meta['field_id'] = $key;
$groupName = $this->fieldData['groups'][$meta['group']]; $groupName = $this->fieldData['groups'][$meta['group']];
$meta['group_name'] = $groupName; $meta['group_name'] = $groupName;
// Assign session value if set, or use default if set if (isset($meta['default']) && (!isset($meta['value']) || empty($meta['value']))) {
if (isset($values[$key])) { $meta['value'] = $meta['default'];
$meta['value'] = $values[$key];
$this->addValueTranslations($meta);
} elseif (isset($meta['default'])) {
if (isset($placeholders[$meta['default']])) {
$meta['value'] = $placeholders[$meta['default']];
} else {
$meta['value'] = $meta['default'];
}
} }
// Field type marker for Mustache // Field type marker for Mustache
$meta['fieldtype_' . $meta['type']] = true; $meta['fieldtype_' . $meta['type']] = true;
$this->addValueTranslations($meta);
$this->addSupportValues($meta);
// Add to fieldlist
$this->fieldData['fields'][$key] = $meta;
}
}
/**
* Adds a placeholder token and the desired replacement value.
*
* @param string $placeholder Placeholder, e.g. USER_NAME
* @param string $replacement Replacement value, e.g. John Doe
*/
public function addPlaceholder($placeholder, $replacement)
{
$this->placeholders[$placeholder] = $replacement;
}
public function addPlaceholders($placeholders)
{
$this->placeholders = array_merge($this->placeholders, $placeholders);
}
public function processPlaceholders()
{
foreach ($this->fieldData['fields'] as $key=>$meta) {
if (isset($this->placeholders[$meta['value']])) {
$meta['value'] = $this->placeholders[$meta['value']];
}
$this->fieldData['fields'][$key] = $meta;
}
}
/**
* Adds the given $values or default values and replaces $placeholders. Also
* adds some helping attributes to fields.
*
* @param mixed[] Key-value-array of values (value) to assign to fields (key)
*/
public function addFieldValues($values = array())
{
foreach ($this->fieldData['fields'] as $key=>$meta) {
// Assign session value if set, or use default if set
if (isset($values[$key])) {
$meta['value'] = $values[$key];
$this->addValueTranslations($meta);
}
$this->addSupportValues($meta); $this->addSupportValues($meta);
// Add to fieldlist // Add to fieldlist

View File

@ -15,8 +15,8 @@ $placeholders = array(
'USER_NAME' => $data['user']['name_first'] . ' ' . $data['user']['name_last'], 'USER_NAME' => $data['user']['name_first'] . ' ' . $data['user']['name_last'],
'USER_EMAIL' => $data['user']['email'], 'USER_EMAIL' => $data['user']['email'],
); );
$fd->addPlaceholders($placeholders);
$fd->addFieldValues($_SESSION[$skey], $placeholders); $fd->addFieldValues($_SESSION[$skey]);
$by_group = $fd->getGroups(); $by_group = $fd->getGroups();