Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Created December 2, 2019 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimonetti/21349666524170365d1c09ebbb952739 to your computer and use it in GitHub Desktop.
Save esimonetti/21349666524170365d1c09ebbb952739 to your computer and use it in GitHub Desktop.
Script to convert a csv file into Sugar php dropdowns
<?php
// example dropdowns.csv content
// "ddname","key1","value1"
// "ddname","key2","value2"
$fileName = 'dropdowns.csv';
if (file_exists($fileName)) {
$file = fopen($fileName, 'r');
$dd = [];
while (($line = fgetcsv($file)) !== FALSE) {
if (count($line) === 3) {
// clean ddname
$ddname = preg_replace('/[^a-z0-9]/', '_', strtolower($line[0]));
// clean keys
$key = preg_replace('/[^a-z0-9]/', '_', strtolower($line[1]));
$val = $line[2];
echo "\$app_list_strings['" . $ddname . "']['" . $key . "'] = '" . $val . "';" . PHP_EOL;
} else {
echo 'The file format is invalid, it should have 3 CSV columns for each row: "ddname","key","value", provided instead ' . print_r($line, true) . PHP_EOL;
}
}
fclose($file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment