I don't know how to mention michaelsy (Michael) here since he hasn't said anything in this thread besides correcting the title quietly. What is fie??? I have no idea :-) Thank you, Michael!
Below is my code to save posted data via bureaucracy forms in a csv file. I use CSV plugin to check contents of each csv file in a wiki page viewable to admin only. My next goal is to make each csv file editable via bureaucracy forms since people sometimes make mistakes and they want to correct posted data later. I want to do it without using other plugins. I will need time to think how to do it.
public function handleData($fields, $thanks)
{
$thanks = "Thank you for participating";
$data = [[],[]];
$fname_arr = ['/home/xxxxxx/data/media/admin/','','.csv'];
foreach($fields as $obj){
$obj2arr = (array)$obj;
if (array_key_exists('values', $obj2arr) && array_key_exists('__formpage_id__', $obj2arr["values"]) && empty($fname_arr[1])) {
$fname_arr[1] = str_replace(':','_',$obj2arr["values"]["__formpage_id__"]);
}
if (array_key_exists('opt', $obj2arr) && array_key_exists('value', $obj2arr["opt"])) {
array_push($data[0],$obj2arr["opt"]["label"]);
array_push($data[1],$obj2arr["opt"]["value"]);
}
}
$filename = implode($fname_arr);
if (file_exists($filename)) {
array_shift($data);
}
$fp = fopen($filename, 'a');
foreach($data as $d){
fputcsv($fp, $d);
}
fclose($fp);
return $thanks;
}