Created
December 2, 2019 23:29
-
-
Save esimonetti/21349666524170365d1c09ebbb952739 to your computer and use it in GitHub Desktop.
Script to convert a csv file into Sugar php dropdowns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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