From ebc0c89fcc3888adad56c356f41b1d8966fc42f8 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 10 Jul 2016 21:49:08 +0200 Subject: [PATCH] Further optimisation of FieldDefinition class. --- lib/Frs/FieldDefinition.php | 74 ++++++++++++++++++++++++++++--------- prep_form.php | 4 +- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/lib/Frs/FieldDefinition.php b/lib/Frs/FieldDefinition.php index ab26845..9a6a113 100644 --- a/lib/Frs/FieldDefinition.php +++ b/lib/Frs/FieldDefinition.php @@ -6,6 +6,7 @@ class FieldDefinition { private $definitionsPrefix; private $fieldData = array(); + private $placeholders = array(); /** * @param string $type Type of FieldDefinition (hotel, car, etc.) @@ -25,15 +26,18 @@ class FieldDefinition $fieldData = array($fieldData); } $this->fieldData = $fieldData; + $this->prepareFields(); } public function getFieldData() { + $this->processPlaceholders(); return $this->fieldData; } public function getGroups() { + $this->processPlaceholders(); $byGroup = array(); foreach ($this->fieldData['groups'] as $id=>$group) { $byGroup[$group] = array( @@ -82,35 +86,69 @@ class FieldDefinition } } - /** - * 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()) + private function prepareFields() { foreach ($this->fieldData['fields'] as $key=>$meta) { $meta['field_id'] = $key; $groupName = $this->fieldData['groups'][$meta['group']]; $meta['group_name'] = $groupName; - // Assign session value if set, or use default if set - if (isset($values[$key])) { - $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']; - } + if (isset($meta['default']) && (!isset($meta['value']) || empty($meta['value']))) { + $meta['value'] = $meta['default']; } // Field type marker for Mustache $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); // Add to fieldlist diff --git a/prep_form.php b/prep_form.php index 4f47b42..1ea5a6a 100644 --- a/prep_form.php +++ b/prep_form.php @@ -15,8 +15,8 @@ $placeholders = array( 'USER_NAME' => $data['user']['name_first'] . ' ' . $data['user']['name_last'], 'USER_EMAIL' => $data['user']['email'], ); - -$fd->addFieldValues($_SESSION[$skey], $placeholders); +$fd->addPlaceholders($placeholders); +$fd->addFieldValues($_SESSION[$skey]); $by_group = $fd->getGroups();