function amr_lines_to_csv($lines, $ulist, $strip_endings, $strip_html = false, $suffix, $wrapper, $delimiter, $nextrow, $tofile = false)
{
    if (isset($lines) and is_array($lines)) {
        $t = count($lines);
    } else {
        $t = 0;
    }
    $csv = '';
    if ($t > 0) {
        array_walk($lines, 'amr_undo_db_slashes');
        array_walk($lines, 'amr_remove_ID_from_front');
        // REMOVE technical ID
        if ($strip_endings) {
            foreach ($lines as $k => $line) {
                $csv .= apply_filters('amr_users_csv_line', $line['csvcontent']) . $nextrow;
            }
        } else {
            foreach ($lines as $k => $line) {
                $csv .= $line['csvcontent'] . $nextrow;
            }
        }
        $csv = str_replace('","', $wrapper . $delimiter . $wrapper, $csv);
        /* we already have in std csv - allow for other formats */
        $csv = str_replace($nextrow . '"', $nextrow . $wrapper, $csv);
        $csv = str_replace('"' . $nextrow, $wrapper . $nextrow, $csv);
        if ($csv[0] == '"') {
            $csv[0] = $wrapper;
        }
        // 20140722 }
        if (amr_debug()) {
            echo '<br />In Debug only: Csv setup: Report: ' . $ulist . ' ' . sprintf(__('%s lines found, 1 heading line, the rest data.', 'amr-users'), $t);
            $bytes = mb_strlen($csv);
            echo ' Size = ' . amru_convert_mem($bytes) . '<br />';
        }
        if ($tofile) {
            $csvfile = amr_users_to_csv($ulist, $csv, $suffix);
            $csvurl = amr_users_get_csv_link($ulist, $suffix);
            if ($suffix == 'txt') {
                $html = '<br />' . __('Public user list txt file: ', 'amr-users') . '<br />' . $csvurl;
            } else {
                $html = '<br />' . __('Public user list csv file: ', 'amr-users') . '<br />' . $csvurl;
            }
        } else {
            echo '<p>' . sprintf(__('List %s, %s lines, plus heading line', 'amr-users'), $ulist, $t) . '</p>';
            $html = amr_csv_form($csv, $suffix);
        }
    } else {
        // 20140722 added empty file check
        $csv = '';
        //20140722 empty so previous file will be deleted
        $csvurl = amr_users_get_csv_link($ulist, $suffix);
        $html = '<br />' . __('No lines found.', 'amr-users');
    }
    return $html;
}
 function amr_empty_start_list($linessaved, $icols, $cols, $page, $rowsperpage, $totalitems, $caption, $search, $ulist, $c, $filtercol, $sortedbynow, $options = array())
 {
     global $aopt, $amain, $amrusers_fieldfiltering, $amr_current_list, $amr_search_result_count, $ahtm;
     // the display html structure to use
     global $amr_refreshed_heading;
     $amr_current_list = $ulist;
     $html = $hhtml = $fhtml = '';
     $filterhtml = '';
     $filterhtml_separate = '';
     $apply_filter_html = '';
     $filter_submit_html = '';
     $summary = '';
     $explain_filter = '';
     $adminoptions = array('show_search' => true, 'show_perpage' => true, 'show_pagination' => true, 'show_headings' => true, 'show_csv' => true, 'show_refresh' => true);
     if (!is_admin()) {
         // set public options to overrwite admin
         foreach ($adminoptions as $i => $opt) {
             if (isset($options[$i])) {
                 $adminoptions[$i] = $options[$i];
             } else {
                 $adminoptions[$i] = '';
             }
         }
     }
     //if (WP_DEBUG) var_dump($adminoptions);
     if (!empty($_REQUEST['headings']) or !empty($_REQUEST['filtering'])) {
         $adminoptions['show_search'] = false;
         $adminoptions['show_csv'] = false;
         $adminoptions['show_refresh'] = false;
         $adminoptions['show_perpage'] = false;
         $adminoptions['show_headings'] = false;
         $adminoptions['show_pagination'] = true;
         $amain['filter_html_type'][$amr_current_list] = 'none';
         // if editingheadings, then no showingfilter
     }
     if (is_admin() or !isset($amain['html_type'][$amr_current_list])) {
         // must be after the check above, so will force table in admin
         $ahtm = amr_get_html_to_use('table');
     } else {
         $ahtm = amr_get_html_to_use($amain['html_type'][$amr_current_list]);
     }
     if (empty($linessaved)) {
         $saveditems = 0;
     } else {
         $saveditems = count($linessaved);
     }
     if (is_array($caption)) {
         $caption = '<h3 class="caption">' . implode(', ', $caption) . '</h3>';
     }
     // now fix the icols and cols for any special functioning--------------------------
     if (isset($icols[0]) and $icols[0] == 'ID') {
         /* we only saved the ID so that we can access extra info on display - we don't want to always display it */
         unset($icols[0]);
         unset($cols[0]);
     }
     $icols = array_unique($icols);
     // since may end up with two indices, eg if filtering and grouping by same value
     foreach ($icols as $i => $col) {
         if ($col == 'index') {
             // we only saved the index so that we can access extra info on display - we don't want to display it
             if (!isset($aopt['list'][$amr_current_list]['selected']['index'])) {
                 unset($icols[$i]);
                 unset($cols[$i]);
             }
         } else {
             if (!isset($cols[$i])) {
                 unset($icols[$i]);
             }
         }
     }
     // end fix icols and cols
     if (!empty($search)) {
         $searchselectnow = sprintf(__('%s Users found.', 'amr-users'), $amr_search_result_count);
         $searchselectnow .= sprintf(__('Searching for "%s" in list', 'amr-users'), $search);
     }
     // reset count if searching
     if (isset($amain['sortable'])) {
         $sortable = $amain['sortable'];
     } else {
         $sortable = false;
     }
     if (!empty($adminoptions['show_headings'])) {
         //admin always has
         if (is_admin()) {
             if (!empty($amr_refreshed_heading)) {
                 $summary = $amr_refreshed_heading;
             } else {
                 $summary = $c->get_cache_summary(amr_rptid($ulist));
             }
         }
         if (!empty($sortedbynow)) {
             $summary = str_replace('<li class="sort">', $sortedbynow, $summary);
         }
         if (!empty($searchselectnow)) {
             $summary = str_replace('<li class="selected">', '<li class="searched">' . $searchselectnow . '</li><li class="selected">', $summary);
         }
         if (!empty($filtercol)) {
             $text = implode(', ', $filtercol);
             $summary = str_replace('<li class="selected">', '<li class="selected">' . __('Selected users with: ', 'amr-users') . $text . '</li><li class="selected">', $summary);
         }
     }
     //
     //$sortedbynow is set if maually resorted
     if (!isset($amain['html_type'][$amr_current_list]) or !isset($amain['filter_html_type']) or isset($amain['filter_html_type'][$amr_current_list]) and $amain['filter_html_type'][$amr_current_list] == "intableheader") {
         if (function_exists('amr_show_filters')) {
             // for pseudo compatability if unmatched versions
             $filterhtml = amr_show_filters($cols, $icols, $ulist, $filtercol);
             // will have tr and th
         }
     } elseif (!empty($amain['filter_html_type'][$amr_current_list]) and $amain['filter_html_type'][$amr_current_list] == "above") {
         if (function_exists('amr_show_filters_alt')) {
             $filterhtml_separate = amr_show_filters_alt($cols, $icols, $ulist, $filtercol);
         }
     }
     if (!empty($filterhtml) or !empty($filterhtml_separate)) {
         $apply_filter_html = amr_show_apply_filter_button($ulist);
     }
     // footer
     $fhtml = $ahtm['tfoot'] . $ahtm['tr'] . '>';
     if (stristr($ahtm['th'], '<th')) {
         // if table
         $fhtml .= $ahtm['th'] . ' colspan="' . count($icols) . '">' . amr_users_give_credit();
     } else {
         $fhtml .= $ahtm['th'] . '>';
     }
     $fhtml .= $ahtm['thc'] . $ahtm['trc'] . $ahtm['tfootc'];
     /* setup the html for the table headings */
     if (!empty($linessaved)) {
         $html .= amr_display_a_page($linessaved, $icols, $cols, $ahtm);
     }
     //
     if (!empty($adminoptions['show_search'])) {
         $sformtext = alist_searchform($ulist);
     } else {
         $sformtext = '';
     }
     //
     if (!empty($adminoptions['show_csv'])) {
         $csvtext = amr_users_get_csv_link($ulist, 'csv');
     } else {
         $csvtext = '';
     }
     //
     if (!empty($adminoptions['show_refresh'])) {
         $refreshtext = amr_users_get_refresh_link($ulist);
     } else {
         $refreshtext = '';
     }
     //
     if (!empty($adminoptions['show_perpage'])) {
         $pformtext = alist_per_pageform($ulist);
     } else {
         $pformtext = '';
     }
     if (!empty($amr_search_result_count)) {
         if ($rowsperpage > $amr_search_result_count) {
             $rowsperpage = $amr_search_result_count;
         }
         //$totalitems = 	$amr_search_result_count;
     }
     if (function_exists('amr_custom_navigation')) {
         $custom_nav = amr_custom_navigation($ulist);
     } else {
         $custom_nav = '';
     }
     $moretext = '';
     if (!empty($adminoptions['show_pagination'])) {
         // allows on to just show latest x
         $pagetext = amr_pagetext($page, $totalitems, $rowsperpage);
     } else {
         $pagetext = '';
     }
     if (!empty($filterhtml) or !empty($hhtml)) {
         $hhtml = $ahtm['thead'] . $filterhtml . $hhtml . $ahtm['theadc'];
     }
     $html = amr_manage_headings_submit() . $filter_submit_html . $sformtext . $explain_filter . $filterhtml_separate . $apply_filter_html . $custom_nav . $pagetext . PHP_EOL . '<div id="userslist' . $ulist . '"><!-- user list-->' . PHP_EOL . $ahtm['table'] . $caption . $hhtml . $fhtml . PHP_EOL . $ahtm['tbody'] . $html . $ahtm['tbodyc'] . '<!-- end user list body-->' . PHP_EOL . $ahtm['tablec'] . '<!-- end user list table-->' . PHP_EOL . PHP_EOL . '</div><!-- end user list-->' . PHP_EOL . '<div class="userlistfooter">' . $pagetext . $csvtext . $refreshtext . $pformtext . '</div>';
     if (is_admin()) {
         $html = PHP_EOL . '<div class="wrap" >' . $html . '</div>' . PHP_EOL;
     }
     $html = $summary . $html;
     return $html;
 }
function amr_generate_csv($ulist, $strip_endings, $strip_html = false, $suffix, $wrapper, $delimiter, $nextrow, $tofile = false)
{
    /* get the whole cached file - write to file? but security / privacy ? */
    /* how big */
    $c = new adb_cache();
    $rptid = $c->reportid($ulist);
    $total = $c->get_cache_totallines($rptid);
    $lines = $c->get_cache_report_lines($rptid, 1, $total + 1);
    /* we want the heading line (line1), but not the internal nameslines (line 0) , plus all the data lines, so neeed total + 1 */
    if (isset($lines) and is_array($lines)) {
        $t = count($lines);
    } else {
        $t = 0;
    }
    $csv = '';
    if ($t > 0) {
        if ($strip_endings) {
            foreach ($lines as $k => $line) {
                $csv .= apply_filters('amr_users_csv_line', $line['csvcontent']) . $nextrow;
            }
        } else {
            foreach ($lines as $k => $line) {
                $csv .= $line['csvcontent'] . $nextrow;
            }
        }
        $csv = str_replace('","', $wrapper . $delimiter . $wrapper, $csv);
        /* we already have in std csv - allow for other formats */
        $csv = str_replace($nextrow . '"', $nextrow . $wrapper, $csv);
        $csv = str_replace('"' . $nextrow, $wrapper . $nextrow, $csv);
        if ($csv[0] == '"') {
            $csv[0] = $wrapper;
        }
    }
    if (amr_debug()) {
        echo '<br />In Debug only: Csv setup: Report: ' . $ulist . ' ' . $c->reportname($ulist) . ' ' . sprintf(__('%s lines found, 1 heading line, the rest data.', 'amr-users'), $t);
        $bytes = mb_strlen($csv);
        echo ' Size = ' . amru_convert_mem($bytes) . '<br />';
    }
    if ($tofile) {
        $csvfile = amr_users_to_csv($ulist, $csv, $suffix);
        $csvurl = amr_users_get_csv_link($ulist);
        //return ($csvurl);
        $html = '<br />' . __('Public user list csv file: ', 'amr-users') . '<br />' . $csvurl;
    } else {
        echo '<p>' . sprintf(__('List %s, %s lines, plus heading line'), $ulist, $t) . '</p>';
        $html = amr_csv_form($csv, $suffix);
    }
    return $html;
}