Esempio n. 1
0
/**
 * Append rows to one of the csv record files.
 * 
 * @uses csv_singlerecord_test()
 * @uses csv_parameters()
 * @uses csv_files_header()
 * @param array $csv_data    the data array, either file data or claim data
 * @param string $file_type  which of our file types to use
 * @param string $csv_type   either 'claim' or 'file'
 * @return int               number of characters written per fputcsv()
 */
function csv_write_record($csv_data, $file_type, $csv_type)
{
    //
    if (!is_array($csv_data)) {
        return FALSE;
    }
    // use CSV_RECORD class to write ibr or ebr claims data to the csv file
    //  csv, batch, ibr, ebr, f997, or era
    if (!strpos("|era|f997|ibr|ebr|dpr|f277|batch|ta1|ack", $file_type)) {
        csv_edihist_log("csv_write_record error: incorrect file type {$file_type}");
        return FALSE;
    }
    $ft = $file_type;
    $ft = strpos("|835", $file_type) ? 'era' : $ft;
    $ft = strpos("|837", $file_type) ? 'batch' : $ft;
    $ft = strpos("|999|ack|ta1", $file_type) ? 'f997' : $ft;
    $params = csv_parameters($ft);
    //
    if ($csv_type == "claim") {
        $fp = $params['claims_csv'];
    } elseif ($csv_type == "file") {
        $fp = $params['files_csv'];
    } else {
        csv_edihist_log("csv_writedata_csv error: incorrect csv type {$csv_type}");
        return FALSE;
    }
    //
    $fh = fopen($fp, 'a');
    // count characters written -- returned by fputcsv
    $indc = 0;
    // if we fail to open the file, return the result, expect FALSE
    if (!$fh) {
        return FALSE;
    }
    // test for a new file
    if (filesize($fp) === 0) {
        $ar_h = csv_files_header($file_type, $csv_type);
        $td = fgetcsv($fh);
        if ($td === FALSE || $td === NULL) {
            // assume we have an empty file
            // write header row if this is a new csv file
            if (count($ar_h)) {
                $indc += fputcsv($fh, $ar_h);
            }
        }
    }
    // test array for dimension counts
    $is_sngl = csv_singlerecord_test($csv_data);
    if ($is_sngl) {
        $indc += fputcsv($fh, $csv_data);
    } else {
        // multi-dimensional array -- we rely on array_flatten to
        // assure us that the array depth is 1
        foreach ($csv_data as $row) {
            $wr = csv_array_flatten($row);
            // $wr is false if $row is not an array
            if ($wr) {
                $indc += fputcsv($fh, $wr);
            } else {
                continue;
            }
        }
    }
    fclose($fh);
    //
    return $indc;
}
Esempio n. 2
0
/**
 * A multidimensional array will be flattened to a single row.
 *
 * @param array $array array to be flattened
 * @return array
 */
function csv_array_flatten($array)
{
    //
    if (!is_array($array)) {
        return FALSE;
    }
    $result = array();
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $result = array_merge($result, csv_array_flatten($value));
        } else {
            $result[$key] = $value;
        }
    }
    return $result;
}