Skip to content
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
77 changes: 67 additions & 10 deletions src/BoltFormsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,81 @@ private function getDefaults(): Collection

private function getAdditionalFormConfigs(): array
{
$configPath = explode('.yaml', $this->getExtension()->getConfigFilenames()['main'])[0] . DIRECTORY_SEPARATOR;

$mainConfigPath = $this->getExtension()->getConfigFilenames()['main'];
$boltFormsRoot = explode('.yaml', $mainConfigPath)[0];
$extrasConfigPath = $boltFormsRoot . DIRECTORY_SEPARATOR;

// Share global_aliases with other configs
$defaultConfig;
$global_aliases;

try {
$defaultConfig = file_get_contents($boltFormsRoot.'.yaml');
}
catch (ParseException $e) {
throw new ParseException(sprintf('Default BoltForm Config Not Found'));
}

$global_aliases = $this->getFormGlobals($defaultConfig);

$finder = new Finder();

$finder->files()->in($configPath)->name('*.yaml');

$finder->files()->in($extrasConfigPath)->name('*.yaml');

// Exit if no sub_files str
if (! $finder->hasResults()) {
return [];
}

$result = [];

foreach ($finder as $file) {
$formName = basename($file->getBasename(), '.yaml');

$result[$formName] = Yaml::parseFile($file->getRealPath());
$ymlContents = '';

if (is_file($file->getRealPath()) && is_readable($file->getRealPath())){
$ymlContents = file_get_contents($file->getRealPath());
}
try {
$result[$formName] = Yaml::parse($global_aliases . $ymlContents);
} catch (ParseException $e) {
$e;
throw new ParseException(sprintf('Error detected on form %s: %s', $formName, $e->getMessage()));
}
unset($result[$formName]['global_aliases']);
}

return $result;
}

private function getFormGlobals(string $defaultFormContents): string
{
$start = strpos($defaultFormContents, 'global_aliases');

if ($start === false) return '';

$end;
$commented_out;

//Check if commented out
if ($start === 0):
$commented_out = false;
else:
// Check to see if a disabled through commenting (previous 5 char)
$rewind_check_start_pos = ($start > 5)? $start - 5 : 0;
$scan_str = substr($defaultFormContents,$rewind_check_start_pos, $start);

$comment_found = strpos($scan_str, '#'.-1); //Note: actually a position, not a bool, reverse searched
$commented_out = ($comment_found !== false && $comment_found < strpos($scan_str, "\n".-1)); //ie: # found and no inbetween \n
endif;

if ($commented_out) return '';

// This is used to find the first instance of a line which starts with neither a whitespace or comment (ie the end of the global aliases, if starting just after the g (start + 1))
preg_match("/^[^\s\#]/m", $defaultFormContents."", $matches, PREG_OFFSET_CAPTURE, $start+1);
if (sizeof($matches) == 0) return '';

$end = $matches[0][1];
$rt_str = substr($defaultFormContents,$start,$end);
return $rt_str;
}
}
2 changes: 1 addition & 1 deletion src/EventSubscriber/ContentTypePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function setContentFields(Content $content, Form $form, PostSubmitEvent

if (is_array($value)) {
$value = implode(', ', array_map(function ($entry) {
return $entry[0];
return $entry;
}, $value));
}
$value = (string) $value;
Expand Down